-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathversion.mjs
More file actions
46 lines (37 loc) · 1.31 KB
/
Copy pathversion.mjs
File metadata and controls
46 lines (37 loc) · 1.31 KB
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
import { readFileSync, writeFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { cac } from 'cac';
import semver from 'semver';
const cli = cac('version');
cli.option('--pre [prerelease]', 'Set the version to a pre-release version');
cli.command('version <bump_version>');
const parsed = cli.parse();
const __dirname = dirname(fileURLToPath(import.meta.url));
const pkgPath = join(__dirname, '../package.json');
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
const currentVersion = pkg.version;
const allowedVersion = ['major', 'minor', 'patch'];
const allowPretags = ['alpha', 'beta', 'rc'];
const version = parsed.args[0];
if (!allowedVersion.includes(version)) {
throw new Error(
`version must be one of ${allowedVersion}, but you passed ${version}`,
);
}
const hasPre = !!parsed.options.pre;
const pre = parsed.options.pre;
if (hasPre && !allowPretags.includes(pre)) {
throw new Error(
`pre tag must be one of ${allowPretags}, but you passed ${pre}`,
);
}
let newVersion;
if (hasPre) {
newVersion = semver.inc(currentVersion, `prerelease`, pre);
} else {
newVersion = semver.inc(currentVersion, version);
}
pkg.version = newVersion;
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
console.log('package.json updated with new version.');