-
Notifications
You must be signed in to change notification settings - Fork 926
/
bump-bit-legacy-ver.js
146 lines (128 loc) · 5.18 KB
/
bump-bit-legacy-ver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { execSync } = require('child_process');
const { SemVer } = require('semver');
const path = require('path');
const { platform } = require('process');
const cwd = path.resolve(__dirname, '..');
const WAIT_FOR_NPM_IN_SEC = 10;
const MAX_NPM_ATTEMPTS = 50;
gitStatus(); // to debug errors with git-pull
backupPnpmLockFile();
resetPnpmLockChanges();
gitPull(); // this way, if the script is re-running after another commit, it has the correct data
revertPnpmLockChanges();
const shouldBump = shouldBumpBitLegacy();
if (!shouldBump) {
console.log('there was no change on legacy @teambit/legacy that requires bumping its version');
return;
}
const currentBitLegacyVersionInCode = require('../package.json').version;
console.log('currentBitLegacyVersionInCode', currentBitLegacyVersionInCode);
const currentBitLegacyVersionInNpm = getCurrentBitLegacyVerFromNpm();
const nextBitLegacyVersion = getNextBitLegacyVersion();
replaceVersionOccurrencesInCode();
gitCommitChanges();
gitPull();
gitPush();
publishBitLegacy();
waitUntilShownInNpm().then(() => console.log('bump has completed!'));
async function waitUntilShownInNpm() {
let shouldWait = true;
let numOfAttempts = 0;
while (shouldWait) {
const currentVer = getCurrentBitLegacyVerFromNpm();
if (currentVer == nextBitLegacyVersion) {
console.log('NPM is up to date!');
shouldWait = false;
} else if (numOfAttempts < MAX_NPM_ATTEMPTS) {
numOfAttempts++;
console.log(`NPM is still showing version ${currentVer}. will try again in ${WAIT_FOR_NPM_IN_SEC} seconds`);
// sleep X seconds. it takes time to get the results from npm anyway.
await sleep(WAIT_FOR_NPM_IN_SEC * 1000);
} else {
throw new Error(
`something is wrong with NPM. wait time of ${WAIT_FOR_NPM_IN_SEC * MAX_NPM_ATTEMPTS} seconds was not enough`
);
}
}
}
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function getCurrentBitLegacyVerFromNpm() {
return exec('npm view @teambit/legacy version').trim();
}
function publishBitLegacy() {
exec('npm publish');
}
function shouldBumpBitLegacy() {
// run git log and grab the first line
const gitLogCmd = 'git log --oneline | grep "bump @teambit/legacy version to" | head -n 1';
const gitLogResult = execSync(gitLogCmd, { cwd }).toString();
if (!gitLogResult) throw new Error('failed running bit-log');
console.log('git log message', gitLogResult);
const lastVer = gitLogResult.split(' ').filter((s) => s.startsWith('1.'));
console.log('last version extracted from the log message', lastVer);
const commitOfLastVer = gitLogResult.split(' ')[0];
// diff since the last version only for "src" and "package.json", we don't care about the rest.
const gitDiff = `git diff --quiet ${commitOfLastVer} HEAD -- src package.json`;
try {
// if there is no diff, it exits with code 0, in which case, we don't want to bump @teambit/legacy.
const diffResult = execSync(gitDiff, { cwd });
return false;
} catch (err) {
// if there is a diff, it exits with code 1, in which case, we want to bump @teambit/legacy.
return true;
}
}
function getNextBitLegacyVersion() {
console.log('currentBitLegacyVersionInNpm', currentBitLegacyVersionInNpm);
const currentBitLegacyVersionInNpmSemver = new SemVer(currentBitLegacyVersionInNpm);
const nextBitLegacySemVer = currentBitLegacyVersionInNpmSemver.inc('patch');
const nextBitLegacyVersion = nextBitLegacySemVer.version;
console.log('nextBitLegacyVersion', nextBitLegacyVersion);
return nextBitLegacyVersion;
}
function replaceVersionOccurrencesInCode() {
const isMac = platform === 'darwin';
const sedBase = isMac ? `sed -i ''` : `sed -i`;
const sed = `${sedBase} "s/${currentBitLegacyVersionInCode}/${nextBitLegacyVersion}/g"`;
const sedPackageJson = `s/\\"version\\": \\"${currentBitLegacyVersionInCode}\\",/\\"version\\": \\"${nextBitLegacyVersion}\\",/g`;
const sedWorkspaceJson = `s/legacy\\": \\"${currentBitLegacyVersionInCode}\\"/legacy\\": \\"${nextBitLegacyVersion}\\"/g`;
execSync(`${sedBase} "${sedPackageJson}" package.json`, { cwd });
execSync(`${sedBase} "${sedWorkspaceJson}" workspace.jsonc`, { cwd });
execSync(`find scopes -name component.json -exec ${sedBase} "${sedWorkspaceJson}" {} \\;`, { cwd });
console.log(`completed changing all occurrences of "${currentBitLegacyVersionInCode}" to "${nextBitLegacyVersion}"`);
}
function gitCommitChanges() {
exec(`git commit -am "bump @teambit/legacy version to ${nextBitLegacyVersion} [skip ci]"`);
}
function gitPush() {
exec('git push origin master');
}
function gitPull() {
exec('GIT_MERGE_AUTOEDIT=no git pull origin master');
}
function backupPnpmLockFile() {
exec('cp pnpm-lock.yaml pnpm-lock.yaml.bak');
}
function revertPnpmLockChanges() {
exec('mv pnpm-lock.yaml.bak pnpm-lock.yaml');
}
function resetPnpmLockChanges() {
exec('git checkout pnpm-lock.yaml');
}
function gitStatus() {
exec('git status');
}
function exec(command) {
console.log(`$ ${command}`);
try {
const results = execSync(command, { cwd });
const resultsStr = results.toString();
console.log('SUCCESS: ', resultsStr);
return resultsStr;
} catch (err) {
console.log('FAILED: ', err.toString());
throw err;
}
}