Skip to content

Commit 65b49db

Browse files
committed
Refactor before-release script by using simple-git library
1 parent 0c493f3 commit 65b49db

File tree

3 files changed

+83
-22
lines changed

3 files changed

+83
-22
lines changed

bin/before-release-init.mjs

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,63 @@
1-
/* eslint-disable no-console */
2-
import shell from 'shelljs';
1+
import simpleGit from 'simple-git';
32

4-
const upstream = '@{u}';
3+
const requiredBranch = 'main';
4+
const git = simpleGit();
55

6-
const isUpstreamConfigured =
7-
shell.exec(`git rev-parse --abbrev-ref --symbolic-full-name ${upstream}`).code === 0;
8-
9-
if (!isUpstreamConfigured) {
10-
console.error('You must configure upstream for the branch.');
11-
shell.exit();
6+
const activeBranch = (await git.branch()).current;
7+
// Check if the active branch match the required one.
8+
if (requiredBranch !== activeBranch) {
9+
throw new Error('You must be on main branch to create a release branch');
1210
}
1311

14-
const local = shell.exec('git rev-parse @');
15-
const remote = shell.exec(`git rev-parse ${upstream}`);
16-
const base = shell.exec(`git merge-base @ ${upstream}`);
12+
await git.fetch();
13+
const gitStatus = await git.status();
14+
15+
// Check if git directory is clean.
16+
if (!gitStatus.isClean()) {
17+
throw new Error('Git working directory must be in a clean state');
18+
}
1719

18-
// Branch is up to date with or ahead to remote.
19-
if (local === remote || remote === base) {
20-
shell.exit(0);
20+
// Check if branch is tracking a remote one.
21+
if (!gitStatus.tracking) {
22+
throw new Error('Local branch is not tracking a remote one');
2123
}
2224

23-
// The local branch is behind the remote version, a pull is needed.
24-
if (local === base) {
25-
console.error('The local branch is behind the remote version');
26-
shell.exit();
25+
// Check if main branch is up to date with remote version.
26+
if (gitStatus.ahead > 0) {
27+
throw new Error('Local branch is ahead of the remote version');
2728
}
29+
if (gitStatus.behind > 0) {
30+
throw new Error('Local branch is behind of the remote version');
31+
}
32+
33+
// /* eslint-disable no-console */
34+
// import shell from 'shelljs';
35+
36+
// const upstream = '@{u}';
37+
38+
// const isUpstreamConfigured =
39+
// shell.exec(`git rev-parse --abbrev-ref --symbolic-full-name ${upstream}`).code === 0;
40+
41+
// if (!isUpstreamConfigured) {
42+
// console.error('You must configure upstream for the branch.');
43+
// shell.exit();
44+
// }
45+
46+
// const local = shell.exec('git rev-parse @');
47+
// const remote = shell.exec(`git rev-parse ${upstream}`);
48+
// const base = shell.exec(`git merge-base @ ${upstream}`);
49+
50+
// // Branch is up to date with or ahead to remote.
51+
// if (local === remote || remote === base) {
52+
// shell.exit(0);
53+
// }
54+
55+
// // The local branch is behind the remote version, a pull is needed.
56+
// if (local === base) {
57+
// console.error('The local branch is behind the remote version');
58+
// shell.exit();
59+
// }
2860

29-
// Local and remote diverged.
30-
console.log('Local branch and remote branch diverged!');
31-
shell.exit();
61+
// // Local and remote diverged.
62+
// console.log('Local branch and remote branch diverged!');
63+
// shell.exit();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"release-it": "^14.11.6",
5353
"shelljs": "^0.8.4",
5454
"shx": "^0.3.3",
55+
"simple-git": "^3.7.0",
5556
"ts-jest": "^27.0.5",
5657
"typescript": "^4.4.3"
5758
},

yarn.lock

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,18 @@
543543
"@types/yargs" "^16.0.0"
544544
chalk "^4.0.0"
545545

546+
"@kwsites/file-exists@^1.1.1":
547+
version "1.1.1"
548+
resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
549+
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
550+
dependencies:
551+
debug "^4.1.1"
552+
553+
"@kwsites/promise-deferred@^1.1.1":
554+
version "1.1.1"
555+
resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
556+
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==
557+
546558
"@nodelib/fs.scandir@2.1.5":
547559
version "2.1.5"
548560
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
@@ -1741,6 +1753,13 @@ debug@^3.2.7:
17411753
dependencies:
17421754
ms "^2.1.1"
17431755

1756+
debug@^4.3.3:
1757+
version "4.3.4"
1758+
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
1759+
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
1760+
dependencies:
1761+
ms "2.1.2"
1762+
17441763
decimal.js@^10.2.1:
17451764
version "10.3.1"
17461765
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:
47384757
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af"
47394758
integrity sha512-sDl4qMFpijcGw22U5w63KmD3cZJfBuFlVNbVMKje2keoKML7X2UzWbc4XrmEbDwg0NXJc3yv4/ox7b+JWb57kQ==
47404759

4760+
simple-git@^3.7.0:
4761+
version "3.7.0"
4762+
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-3.7.0.tgz#b0aac4c2e6e8118c115460ed93d072cda966dd42"
4763+
integrity sha512-O9HlI83ywqkYqnr7Wh3CqKNNrMkfjzpKQSGtJAhk7+H5P+lAxHBTIPgu/eO/0D9pMciepgs433p0d5S+NYv5Jg==
4764+
dependencies:
4765+
"@kwsites/file-exists" "^1.1.1"
4766+
"@kwsites/promise-deferred" "^1.1.1"
4767+
debug "^4.3.3"
4768+
47414769
sisteransi@^1.0.5:
47424770
version "1.0.5"
47434771
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"

0 commit comments

Comments
 (0)