From 65b49db4b79918b4a45a2388066a903a15b6fb6b Mon Sep 17 00:00:00 2001 From: nebarf Date: Wed, 20 Apr 2022 16:50:17 +0200 Subject: [PATCH] Refactor before-release script by using simple-git library --- bin/before-release-init.mjs | 76 ++++++++++++++++++++++++++----------- package.json | 1 + yarn.lock | 28 ++++++++++++++ 3 files changed, 83 insertions(+), 22 deletions(-) diff --git a/bin/before-release-init.mjs b/bin/before-release-init.mjs index d50ef11..3061117 100644 --- a/bin/before-release-init.mjs +++ b/bin/before-release-init.mjs @@ -1,31 +1,63 @@ -/* eslint-disable no-console */ -import shell from 'shelljs'; +import simpleGit from 'simple-git'; -const upstream = '@{u}'; +const requiredBranch = 'main'; +const git = simpleGit(); -const isUpstreamConfigured = - shell.exec(`git rev-parse --abbrev-ref --symbolic-full-name ${upstream}`).code === 0; - -if (!isUpstreamConfigured) { - console.error('You must configure upstream for the branch.'); - shell.exit(); +const activeBranch = (await git.branch()).current; +// Check if the active branch match the required one. +if (requiredBranch !== activeBranch) { + throw new Error('You must be on main branch to create a release branch'); } -const local = shell.exec('git rev-parse @'); -const remote = shell.exec(`git rev-parse ${upstream}`); -const base = shell.exec(`git merge-base @ ${upstream}`); +await git.fetch(); +const gitStatus = await git.status(); + +// Check if git directory is clean. +if (!gitStatus.isClean()) { + throw new Error('Git working directory must be in a clean state'); +} -// Branch is up to date with or ahead to remote. -if (local === remote || remote === base) { - shell.exit(0); +// Check if branch is tracking a remote one. +if (!gitStatus.tracking) { + throw new Error('Local branch is not tracking a remote one'); } -// The local branch is behind the remote version, a pull is needed. -if (local === base) { - console.error('The local branch is behind the remote version'); - shell.exit(); +// Check if main branch is up to date with remote version. +if (gitStatus.ahead > 0) { + throw new Error('Local branch is ahead of the remote version'); } +if (gitStatus.behind > 0) { + throw new Error('Local branch is behind of the remote version'); +} + +// /* eslint-disable no-console */ +// import shell from 'shelljs'; + +// const upstream = '@{u}'; + +// const isUpstreamConfigured = +// shell.exec(`git rev-parse --abbrev-ref --symbolic-full-name ${upstream}`).code === 0; + +// if (!isUpstreamConfigured) { +// console.error('You must configure upstream for the branch.'); +// shell.exit(); +// } + +// const local = shell.exec('git rev-parse @'); +// const remote = shell.exec(`git rev-parse ${upstream}`); +// const base = shell.exec(`git merge-base @ ${upstream}`); + +// // Branch is up to date with or ahead to remote. +// if (local === remote || remote === base) { +// shell.exit(0); +// } + +// // The local branch is behind the remote version, a pull is needed. +// if (local === base) { +// console.error('The local branch is behind the remote version'); +// shell.exit(); +// } -// Local and remote diverged. -console.log('Local branch and remote branch diverged!'); -shell.exit(); +// // Local and remote diverged. +// console.log('Local branch and remote branch diverged!'); +// shell.exit(); diff --git a/package.json b/package.json index 4c918ce..0fb417b 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "release-it": "^14.11.6", "shelljs": "^0.8.4", "shx": "^0.3.3", + "simple-git": "^3.7.0", "ts-jest": "^27.0.5", "typescript": "^4.4.3" }, diff --git a/yarn.lock b/yarn.lock index c7864f9..c081506 100644 --- a/yarn.lock +++ b/yarn.lock @@ -543,6 +543,18 @@ "@types/yargs" "^16.0.0" chalk "^4.0.0" +"@kwsites/file-exists@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99" + integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw== + dependencies: + debug "^4.1.1" + +"@kwsites/promise-deferred@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919" + integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw== + "@nodelib/fs.scandir@2.1.5": version "2.1.5" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" @@ -1741,6 +1753,13 @@ debug@^3.2.7: dependencies: ms "^2.1.1" +debug@^4.3.3: + version "4.3.4" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865" + integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ== + dependencies: + ms "2.1.2" + decimal.js@^10.2.1: version "10.3.1" resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.3.1.tgz#d8c3a444a9c6774ba60ca6ad7261c3a94fd5e783" @@ -4738,6 +4757,15 @@ signal-exit@^3.0.2, signal-exit@^3.0.3: resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af" integrity sha512-sDl4qMFpijcGw22U5w63KmD3cZJfBuFlVNbVMKje2keoKML7X2UzWbc4XrmEbDwg0NXJc3yv4/ox7b+JWb57kQ== +simple-git@^3.7.0: + version "3.7.0" + resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-3.7.0.tgz#b0aac4c2e6e8118c115460ed93d072cda966dd42" + integrity sha512-O9HlI83ywqkYqnr7Wh3CqKNNrMkfjzpKQSGtJAhk7+H5P+lAxHBTIPgu/eO/0D9pMciepgs433p0d5S+NYv5Jg== + dependencies: + "@kwsites/file-exists" "^1.1.1" + "@kwsites/promise-deferred" "^1.1.1" + debug "^4.3.3" + sisteransi@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"