forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.js
More file actions
157 lines (134 loc) · 4.18 KB
/
version.js
File metadata and controls
157 lines (134 loc) · 4.18 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
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
147
148
149
150
151
152
153
154
155
156
157
import path from "path";
import { fileURLToPath } from "url";
import { execSync } from "child_process";
import chalk from "chalk";
import Confirm from "prompt-confirm";
import jsonfile from "jsonfile";
import semver from "semver";
const dirname = path.dirname(fileURLToPath(import.meta.url));
const rootDir = path.resolve(dirname, "..");
/**
* @param {string} packageName
* @returns {string}
*/
function packageJson(packageName) {
return path.join(rootDir, "packages", packageName, "package.json");
}
/**
* @param {*} cond
* @param {string} message
* @returns {asserts cond}
*/
function invariant(cond, message) {
if (!cond) throw new Error(message);
}
function ensureCleanWorkingDirectory() {
let status = execSync(`git status --porcelain`).toString().trim();
let lines = status.split("\n");
invariant(
lines.every(line => line === "" || line.startsWith("?")),
"Working directory is not clean. Please commit or stash your changes."
);
}
/**
* @param {string} currentVersion
* @param {string} givenVersion
* @param {string} [prereleaseId]
* @returns {string}
*/
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;
}
/**
* @param {string} question
* @returns {Promise<string>}
*/
async function prompt(question) {
let confirm = new Confirm(question);
let answer = await confirm.run();
return answer;
}
/**
* @param {string} packageName
*/
async function getPackageVersion(packageName) {
let file = packageJson(packageName);
let json = await jsonfile.readFile(file);
return json.version;
}
/**
* @param {string} packageName
* @param {(json: string) => any} transform
*/
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("react-router");
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 react-router version
await updatePackageConfig("react-router", config => {
config.version = version;
});
console.log(chalk.green(` Updated react-router to version ${version}`));
// 4. Update react-router-dom version + react-router dep
await updatePackageConfig("react-router-dom", config => {
config.version = version;
config.dependencies["react-router"] = version;
});
console.log(
chalk.green(` Updated react-router-dom to version ${version}`)
);
// 5. Update react-router-native version + react-router dep
await updatePackageConfig("react-router-native", config => {
config.version = version;
config.dependencies["react-router"] = version;
});
console.log(
chalk.green(` Updated react-router-native to version ${version}`)
);
// 6. 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);
});