forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-release.js
executable file
·286 lines (237 loc) · 7.43 KB
/
prepare-release.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env node
'use strict';
const chalk = require('chalk');
const {exec} = require('child-process-promise');
const {readFileSync, writeFileSync} = require('fs');
const {readJsonSync, writeJsonSync} = require('fs-extra');
const inquirer = require('inquirer');
const {join, relative} = require('path');
const semver = require('semver');
const {
CHANGELOG_PATH,
DRY_RUN,
MANIFEST_PATHS,
PACKAGE_PATHS,
PULL_REQUEST_BASE_URL,
RELEASE_SCRIPT_TOKEN,
ROOT_PATH,
} = require('./configuration');
const {
checkNPMPermissions,
clear,
confirmContinue,
execRead,
} = require('./utils');
// This is the primary control function for this script.
async function main() {
clear();
await checkNPMPermissions();
const sha = await getPreviousCommitSha();
const [shortCommitLog, formattedCommitLog] = await getCommitLog(sha);
console.log('');
console.log(
'This release includes the following commits:',
chalk.gray(shortCommitLog)
);
console.log('');
const releaseType = await getReleaseType();
const path = join(ROOT_PATH, PACKAGE_PATHS[0]);
const previousVersion = readJsonSync(path).version;
const {major, minor, patch} = semver(previousVersion);
const nextVersion =
releaseType === 'minor'
? `${major}.${minor + 1}.0`
: `${major}.${minor}.${patch + 1}`;
updateChangelog(nextVersion, formattedCommitLog);
await reviewChangelogPrompt();
updatePackageVersions(previousVersion, nextVersion);
updateManifestVersions(previousVersion, nextVersion);
console.log('');
console.log(
`Packages and manifests have been updated from version ${chalk.bold(
previousVersion
)} to ${chalk.bold(nextVersion)}`
);
console.log('');
await commitPendingChanges(previousVersion, nextVersion);
printFinalInstructions();
}
async function commitPendingChanges(previousVersion, nextVersion) {
console.log('');
console.log('Committing revision and changelog.');
console.log(chalk.dim(' git add .'));
console.log(
chalk.dim(
` git commit -m "React DevTools ${previousVersion} -> ${nextVersion}"`
)
);
if (!DRY_RUN) {
await exec(`
git add .
git commit -m "React DevTools ${previousVersion} -> ${nextVersion}"
`);
}
console.log('');
console.log(`Please push this commit before continuing:`);
console.log(` ${chalk.bold.green('git push')}`);
await confirmContinue();
}
async function getCommitLog(sha) {
let shortLog = '';
let formattedLog = '';
const hasGh = await hasGithubCLI();
const rawLog = await execRead(`
git log --topo-order --pretty=format:'%s' ${sha}...HEAD -- packages/react-devtools*
`);
const lines = rawLog.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].replace(/^\[devtools\] */i, '');
const match = line.match(/(.+) \(#([0-9]+)\)/);
if (match !== null) {
const title = match[1];
const pr = match[2];
let username;
if (hasGh) {
const response = await execRead(
`gh api /repos/facebook/react/pulls/${pr}`
);
const {user} = JSON.parse(response);
username = `[${user.login}](${user.html_url})`;
} else {
username = '[USERNAME](https://github.com/USERNAME)';
}
formattedLog += `\n* ${title} (${username} in [#${pr}](${PULL_REQUEST_BASE_URL}${pr}))`;
shortLog += `\n* ${title}`;
} else {
formattedLog += `\n* ${line}`;
shortLog += `\n* ${line}`;
}
}
return [shortLog, formattedLog];
}
async function hasGithubCLI() {
try {
await exec('which gh');
return true;
} catch (_) {}
return false;
}
async function getPreviousCommitSha() {
const choices = [];
const lines = await execRead(`
git log --max-count=5 --topo-order --pretty=format:'%H:::%s:::%as' HEAD -- ${join(
ROOT_PATH,
PACKAGE_PATHS[0]
)}
`);
lines.split('\n').forEach((line, index) => {
const [hash, message, date] = line.split(':::');
choices.push({
name: `${chalk.bold(hash)} ${chalk.dim(date)} ${message}`,
value: hash,
short: date,
});
});
const {sha} = await inquirer.prompt([
{
type: 'list',
name: 'sha',
message: 'Which of the commits above marks the last DevTools release?',
choices,
default: choices[0].value,
},
]);
return sha;
}
async function getReleaseType() {
const {releaseType} = await inquirer.prompt([
{
type: 'list',
name: 'releaseType',
message: 'Which type of release is this?',
choices: [
{
name: 'Minor (new user facing functionality)',
value: 'minor',
short: 'Minor',
},
{name: 'Patch (bug fixes only)', value: 'patch', short: 'Patch'},
],
default: 'patch',
},
]);
return releaseType;
}
function printFinalInstructions() {
const buildAndTestcriptPath = join(__dirname, 'build-and-test.js');
const pathToPrint = relative(process.cwd(), buildAndTestcriptPath);
console.log('');
console.log('Continue by running the build-and-test script:');
console.log(chalk.bold.green(' ' + pathToPrint));
}
async function reviewChangelogPrompt() {
console.log('');
console.log(
'The changelog has been updated with commits since the previous release:'
);
console.log(` ${chalk.bold(CHANGELOG_PATH)}`);
console.log('');
console.log('Please review the new changelog text for the following:');
console.log(' 1. Filter out any non-user-visible changes (e.g. typo fixes)');
console.log(' 2. Organize the list into Features vs Bugfixes');
console.log(' 3. Combine related PRs into a single bullet list');
console.log(
' 4. Replacing the "USERNAME" placeholder text with the GitHub username(s)'
);
console.log('');
console.log(` ${chalk.bold.green(`open ${CHANGELOG_PATH}`)}`);
await confirmContinue();
}
function updateChangelog(nextVersion, commitLog) {
const path = join(ROOT_PATH, CHANGELOG_PATH);
const oldChangelog = readFileSync(path, 'utf8');
const [beginning, end] = oldChangelog.split(RELEASE_SCRIPT_TOKEN);
const dateString = new Date().toLocaleDateString('en-us', {
year: 'numeric',
month: 'long',
day: 'numeric',
});
const header = `---\n\n### ${nextVersion}\n${dateString}`;
const newChangelog = `${beginning}${RELEASE_SCRIPT_TOKEN}\n\n${header}\n${commitLog}${end}`;
console.log(chalk.dim(' Updating changelog: ' + CHANGELOG_PATH));
if (!DRY_RUN) {
writeFileSync(path, newChangelog);
}
}
function updateManifestVersions(previousVersion, nextVersion) {
MANIFEST_PATHS.forEach(partialPath => {
const path = join(ROOT_PATH, partialPath);
const json = readJsonSync(path);
json.version = nextVersion;
if (json.hasOwnProperty('version_name')) {
json.version_name = nextVersion;
}
console.log(chalk.dim(' Updating manifest JSON: ' + partialPath));
if (!DRY_RUN) {
writeJsonSync(path, json, {spaces: 2});
}
});
}
function updatePackageVersions(previousVersion, nextVersion) {
PACKAGE_PATHS.forEach(partialPath => {
const path = join(ROOT_PATH, partialPath);
const json = readJsonSync(path);
json.version = nextVersion;
for (let key in json.dependencies) {
if (key.startsWith('react-devtools')) {
const version = json.dependencies[key];
json.dependencies[key] = version.replace(previousVersion, nextVersion);
}
}
console.log(chalk.dim(' Updating package JSON: ' + partialPath));
if (!DRY_RUN) {
writeJsonSync(path, json, {spaces: 2});
}
});
}
main();