-
Notifications
You must be signed in to change notification settings - Fork 953
Expand file tree
/
Copy pathversion.js
More file actions
113 lines (91 loc) · 2.97 KB
/
Copy pathversion.js
File metadata and controls
113 lines (91 loc) · 2.97 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
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
const path = require('path');
const execSync = require('child_process').execSync;
const chalk = require('chalk');
const Confirm = require('prompt-confirm');
const jsonfile = require('jsonfile');
const semver = require('semver');
const rootDir = path.resolve(__dirname, '..');
function packageJson(packageName) {
return path.join(rootDir, 'packages', packageName, 'package.json');
}
function invariant(cond, message) {
if (!cond) throw new Error(message);
}
function ensureCleanWorkingDirectory() {
let status = execSync(`git status --porcelain`)
.toString()
.trim();
invariant(
status === '',
'Working directory is not clean. Please commit or stash your changes.'
);
}
function getNextVersion(currentVersion, givenVersion, prereleaseId) {
invariant(
givenVersion != null,
`Missing next version. Usage: node version.js [nextVersion]`
);
if (/^pre/.test(givenVersion)) {
invariant(
prereleaseId != null,
`Missing prerelease id. Usage: node version.js ${givenVersion} [prereleaseId]`
);
}
let nextVersion = semver.inc(currentVersion, givenVersion, prereleaseId);
invariant(nextVersion != null, `Invalid version specifier: ${givenVersion}`);
return nextVersion;
}
async function prompt(question) {
let confirm = new Confirm(question);
let answer = await confirm.run();
return answer;
}
async function getPackageVersion(packageName) {
let file = packageJson(packageName);
let json = await jsonfile.readFile(file);
return json.version;
}
async function updatePackageConfig(packageName, transform) {
let file = packageJson(packageName);
let json = await jsonfile.readFile(file);
transform(json);
await jsonfile.writeFile(file, json, { spaces: 2 });
}
async function run() {
try {
let args = process.argv.slice(2);
let givenVersion = args[0];
let prereleaseId = args[1];
// 0. Make sure the working directory is clean
ensureCleanWorkingDirectory();
// 1. Get the next version number
let currentVersion = await getPackageVersion('history');
let version = semver.valid(givenVersion);
if (version == null) {
version = getNextVersion(currentVersion, givenVersion, prereleaseId);
}
// 2. Confirm the next version number
let answer = await prompt(
`Are you sure you want to bump version ${currentVersion} to ${version}? [Yn] `
);
if (answer === false) return 0;
// 3. Update history version
await updatePackageConfig('history', config => {
config.version = version;
});
console.log(chalk.green(` Updated history to version ${version}`));
// 4. Commit and tag
execSync(`git commit --all --message="Version ${version}"`);
execSync(`git tag -a -m "Version ${version}" v${version}`);
console.log(chalk.green(` Committed and tagged version ${version}`));
} catch (error) {
console.log();
console.error(chalk.red(` ${error.message}`));
console.log();
return 1;
}
return 0;
}
run().then(code => {
process.exit(code);
});