forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
239 lines (206 loc) · 6.24 KB
/
index.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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
const chalk = require('chalk');
const {execSync} = require('child_process');
const inquirer = require('inquirer');
const path = require('path');
const {echo, exec, exit} = require('shelljs');
const yargs = require('yargs');
const alignPackageVersions = require('../align-package-versions');
const {
PUBLISH_PACKAGES_TAG,
GENERIC_COMMIT_MESSAGE,
NO_COMMIT_CHOICE,
COMMIT_WITH_GENERIC_MESSAGE_CHOICE,
COMMIT_WITH_CUSTOM_MESSAGE_CHOICE,
} = require('../constants');
const forEachPackage = require('../for-each-package');
const checkForGitChanges = require('../check-for-git-changes');
const bumpPackageVersion = require('./bump-package-version');
const ROOT_LOCATION = path.join(__dirname, '..', '..', '..');
const {
argv: {releaseBranchCutoff},
} = yargs
.option('release-branch-cutoff', {
type: 'boolean',
describe: 'Should force bump minor version for each public package',
})
.strict();
const buildExecutor =
(packageAbsolutePath, packageRelativePathFromRoot, packageManifest) =>
async () => {
const {name: packageName} = packageManifest;
if (packageManifest.private) {
echo(`\u23ED Skipping private package ${chalk.dim(packageName)}`);
return;
}
if (releaseBranchCutoff) {
const updatedVersion = bumpPackageVersion(
packageAbsolutePath,
packageManifest,
'minor',
);
echo(
`\u2705 Successfully bumped ${chalk.green(
packageName,
)} to ${chalk.green(updatedVersion)}`,
);
return;
}
const hashOfLastCommitInsidePackage = exec(
`git log -n 1 --format=format:%H -- ${packageRelativePathFromRoot}`,
{cwd: ROOT_LOCATION, silent: true},
).stdout.trim();
const hashOfLastCommitThatChangedVersion = exec(
`git log -G\\"version\\": --format=format:%H -n 1 -- ${packageRelativePathFromRoot}/package.json`,
{cwd: ROOT_LOCATION, silent: true},
).stdout.trim();
if (hashOfLastCommitInsidePackage === hashOfLastCommitThatChangedVersion) {
echo(
`\uD83D\uDD0E No changes for package ${chalk.green(
packageName,
)} since last version bump`,
);
return;
}
echo(`\uD83D\uDCA1 Found changes for ${chalk.yellow(packageName)}:`);
exec(
`git log --pretty=oneline ${hashOfLastCommitThatChangedVersion}..${hashOfLastCommitInsidePackage} ${packageRelativePathFromRoot}`,
{
cwd: ROOT_LOCATION,
},
);
echo();
await inquirer
.prompt([
{
type: 'list',
name: 'shouldBumpPackage',
message: `Do you want to bump ${packageName}?`,
choices: ['Yes', 'No'],
filter: val => val === 'Yes',
},
])
.then(({shouldBumpPackage}) => {
if (!shouldBumpPackage) {
echo(`Skipping bump for ${packageName}`);
return;
}
return inquirer
.prompt([
{
type: 'list',
name: 'increment',
message: 'Which version you want to increment?',
choices: ['patch', 'minor'],
},
])
.then(({increment}) => {
const updatedVersion = bumpPackageVersion(
packageAbsolutePath,
packageManifest,
increment,
);
echo(
`\u2705 Successfully bumped ${chalk.green(
packageName,
)} to ${chalk.green(updatedVersion)}`,
);
});
});
};
const buildAllExecutors = () => {
const executors = [];
forEachPackage((...params) => {
executors.push(buildExecutor(...params));
});
return executors;
};
const main = async () => {
if (checkForGitChanges()) {
echo(
chalk.red(
'Found uncommitted changes. Please commit or stash them before running this script',
),
);
exit(1);
}
const executors = buildAllExecutors();
for (const executor of executors) {
await executor()
.catch(() => exit(1))
.then(() => echo());
}
if (!checkForGitChanges()) {
echo('No changes have been made. Finishing the process...');
exit(0);
}
echo('Aligning new versions across monorepo...');
alignPackageVersions();
echo(chalk.green('Done!\n'));
await inquirer
.prompt([
{
type: 'list',
name: 'commitChoice',
message: 'Do you want to submit a commit with these changes?',
choices: [
{
name: 'Yes, with generic message',
value: COMMIT_WITH_GENERIC_MESSAGE_CHOICE,
},
{
name: 'Yes, with custom message',
value: COMMIT_WITH_CUSTOM_MESSAGE_CHOICE,
},
{
name: 'No',
value: NO_COMMIT_CHOICE,
},
],
},
])
.then(({commitChoice}) => {
switch (commitChoice) {
case NO_COMMIT_CHOICE: {
echo('Not submitting a commit, but keeping all changes');
break;
}
case COMMIT_WITH_GENERIC_MESSAGE_CHOICE: {
exec(`git commit -am "${GENERIC_COMMIT_MESSAGE}"`, {
cwd: ROOT_LOCATION,
silent: true,
});
break;
}
case COMMIT_WITH_CUSTOM_MESSAGE_CHOICE: {
// exec from shelljs currently does not support interactive input
// https://github.com/shelljs/shelljs/wiki/FAQ#running-interactive-programs-with-exec
execSync('git commit -a', {cwd: ROOT_LOCATION, stdio: 'inherit'});
const enteredCommitMessage = exec('git log -n 1 --format=format:%B', {
cwd: ROOT_LOCATION,
silent: true,
}).stdout.trim();
const commitMessageWithTag =
enteredCommitMessage + `\n\n${PUBLISH_PACKAGES_TAG}`;
exec(`git commit --amend -m "${commitMessageWithTag}"`, {
cwd: ROOT_LOCATION,
silent: true,
});
break;
}
default:
throw new Error('');
}
})
.then(() => echo());
echo(chalk.green('Successfully finished the process of bumping packages'));
exit(0);
};
main();