Skip to content

Commit

Permalink
Refactor before-release script by using simple-git library
Browse files Browse the repository at this point in the history
  • Loading branch information
nebarf committed Apr 20, 2022
1 parent 0c493f3 commit 65b49db
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 22 deletions.
76 changes: 54 additions & 22 deletions bin/before-release-init.mjs
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 65b49db

Please sign in to comment.