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
196 lines (169 loc) · 5.55 KB
/
version.js
File metadata and controls
196 lines (169 loc) · 5.55 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// FIXME: @remix-run/router isn't being automatically versioned
const path = require("path");
const { execSync } = require("child_process");
const fsp = require("fs/promises");
const chalk = require("chalk");
const Confirm = require("prompt-confirm");
const jsonfile = require("jsonfile");
const semver = require("semver");
const rootDir = path.resolve(__dirname, "..");
const examplesDir = path.resolve(rootDir, "examples");
/**
* @param {string} packageName
* @param {string} [directory]
* @returns {string}
*/
function packageJson(packageName, directory = "packages") {
return path.join(rootDir, directory, 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 });
}
/**
* @param {string} example
* @param {(json: string) => any} transform
*/
async function updateExamplesPackageConfig(example, transform) {
let file = packageJson(example, "examples");
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}`)
);
// 4.1 Update react-router-dom-v5-compat version + react-router dep
await updatePackageConfig("react-router-dom-v5-compat", (config) => {
config.version = version;
config.dependencies["react-router"] = version;
});
console.log(
chalk.green(` Updated react-router-dom-v5-compat 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. Update react-router and react-router-dom versions in the examples
let examples = await fsp.readdir(examplesDir);
for (const example of examples) {
let stat = await fsp.stat(path.join(examplesDir, example));
if (!stat.isDirectory()) continue;
await updateExamplesPackageConfig(example, (config) => {
config.dependencies["react-router"] = version;
config.dependencies["react-router-dom"] = version;
});
}
// 7. 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}`));
console.log(
chalk.red(
` 🚨 @remix-run/router isn't handled by this script, do it manually!`
)
);
} catch (error) {
console.log();
console.error(chalk.red(` ${error.message}`));
console.log();
return 1;
}
return 0;
}
run().then((code) => {
process.exit(code);
});