-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversionBump.js
57 lines (53 loc) · 1.96 KB
/
versionBump.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
const { writeFileSync, readFileSync } = require('fs');
const { spawnSync } = require('child_process');
const baseDir = process.cwd();
const pkgJson = readFileSync(`${baseDir}/package.json`, 'utf-8');
const modified = JSON.parse(pkgJson);
if (modified.workspaces) {
const filterNonBuild = x => !x.startsWith('..') && !x.includes('/build');
if (Array.isArray(modified.workspaces)) {
modified.workspaces = modified.workspaces?.filter(filterNonBuild);
} else if (Array.isArray(modified.workspaces.packages)) {
modified.workspaces.packages = modified.workspaces.packages?.filter(filterNonBuild);
}
}
function restoreWorkspace() {
console.log('Restoring Package.json'); // eslint-disable-line no-console
writeFileSync(`${baseDir}/package.json`, pkgJson);
}
(async () => {
console.log('Removing external workspaces from package.json at: ', baseDir); // eslint-disable-line no-console
if (
!(Array.isArray(modified.workspaces?.packages) && modified.workspaces.packages.length) &&
!(Array.isArray(modified.workspaces) && modified.workspaces.length)
) {
let versionArgs = [process.argv[2] ?? '--patch'];
if (versionArgs[0].match(/^\d/)) {
versionArgs.unshift('--new-version');
}
if (!versionArgs[0].startsWith('--')) versionArgs[0] = '--' + versionArgs[0];
spawnSync(`yarn`, ['version', ...versionArgs], {
cwd: process.cwd(),
stdio: 'inherit',
});
} else {
writeFileSync(`${baseDir}/package.json`, JSON.stringify(modified, null, 2));
process.once('beforeExit', restoreWorkspace);
process.once('exit', restoreWorkspace);
process.once('SIGINT', restoreWorkspace);
process.once('SIGTERM', restoreWorkspace);
spawnSync(
`lerna`,
[
'version',
process.argv[2] ?? 'patch',
'--conventional-commits',
'--no-push',
'--exact',
'--force-publish',
],
{ cwd: process.cwd(), stdio: 'inherit' },
);
restoreWorkspace();
}
})().catch(console.error);