-
Notifications
You must be signed in to change notification settings - Fork 23
/
publish.js
executable file
·354 lines (330 loc) · 10.4 KB
/
publish.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// @flow
import semver from 'semver';
import inquirer from 'inquirer';
import { mainStory, chalk } from 'storyboard';
import { readAllSpecs, ROOT_PACKAGE } from './utils/readSpecs';
import writeSpecs from './utils/writeSpecs';
import { exec } from './utils/shell';
import {
gitLastTag,
gitCurBranch,
gitUncommittedChanges,
gitUnpulledChanges,
gitCommitChanges,
gitAddTag,
gitPushWithTags,
} from './utils/git';
import { addVersionLine } from './utils/changelog';
import { masterOrMainBranch } from './utils/helpers';
import { calcGraphAndReturnAsAllSpecs } from './utils/calcGraph';
import bumpDeps from './bump';
const DEBUG_SKIP_CHECKS = false;
const RELEASE_INCREMENTS = ['major', 'minor', 'patch'];
const PRERELEASE_INCREMENTS = ['rc', 'beta', 'alpha'];
const INCREMENTS = [...RELEASE_INCREMENTS, ...PRERELEASE_INCREMENTS];
type Options = {
src: string,
ignoreSrc?: string,
link: ?string,
incrementVersionBy?: string,
master: boolean,
checkUncommitted: boolean,
checkUnpulled: boolean,
checks: boolean,
confirm: boolean,
bump: boolean,
bumpDependentReqs: string,
gitCommit: boolean,
newVersion?: string,
npmPublish: boolean,
publishTag?: string,
otp?: string,
access?: string,
changelog: boolean,
changelogPath: string,
single: boolean,
// For unit tests
_date?: ?Object, // overrides the current date
_masterVersion?: string, // overrides the current master version
};
const run = async ({
src,
ignoreSrc,
link,
master,
checkUncommitted,
checkUnpulled,
checks,
confirm,
bump,
bumpDependentReqs,
gitCommit,
newVersion,
npmPublish,
publishTag,
incrementVersionBy,
changelog,
changelogPath,
single,
otp,
access,
_date,
_masterVersion,
}: Options) => {
const allSpecs = calcGraphAndReturnAsAllSpecs(
await readAllSpecs(src, ignoreSrc)
);
// Confirm that we have run build and run prepublish checks
if (confirm && !(await confirmBuild())) return;
await prepublishChecks({ checks, master, checkUncommitted, checkUnpulled });
// Get list of packages to be updated. This is NOT the list of packages to
// be published, since some of them might be private. But all of them will
// be version-bumped
const pkgList = [];
let numPublic = 0;
Object.keys(allSpecs).forEach(pkgName => {
if (pkgName === ROOT_PACKAGE && !single) return;
pkgList.push(pkgName);
const { specs } = allSpecs[pkgName];
if (!specs.private) numPublic += 1;
});
if (!pkgList.length) {
mainStory.info('No packages found!');
return;
}
if (bump) {
// Determine a suitable new version number
const lastTag = await gitLastTag();
const masterVersion =
_masterVersion || (await getMasterVersion(allSpecs, lastTag));
if (masterVersion == null) return;
if (incrementVersionBy) validateVersionIncrement(incrementVersionBy);
const nextVersion =
newVersion ||
calcNextVersion(masterVersion, incrementVersionBy) ||
(await promptNextVersion(masterVersion));
// Confirm before proceeding
if (confirm && !(await confirmPublish({ pkgList, numPublic, nextVersion })))
return;
// Update package.json's for pkgList packages AND THE ROOT PACKAGE
const pkgListPlusRoot = single ? pkgList : pkgList.concat(ROOT_PACKAGE);
pkgListPlusRoot.forEach(pkgName => {
const { specPath, specs } = allSpecs[pkgName];
specs.version = nextVersion;
writeSpecs(specPath, specs);
});
// Bump dependent requirements
if (!single && bumpDependentReqs !== 'no') {
const bumpList = pkgList.map(pkgName =>
bumpDependentReqs === 'exact'
? `${pkgName}@${nextVersion}`
: `${pkgName}@^${nextVersion}`
);
await bumpDeps(bumpList, { src, ignoreSrc, link });
}
// Update changelog
if (changelog) {
addVersionLine({ changelogPath, version: nextVersion, _date });
}
// Commit, tag and push
if (gitCommit) {
await gitCommitChanges(`v${nextVersion}`);
await gitAddTag(`v${nextVersion}`);
await gitPushWithTags();
}
}
// Publish
if (npmPublish) {
for (let i = 0; i < pkgList.length; i++) {
const pkgName = pkgList[i];
const { pkgPath, specs } = allSpecs[pkgName];
if (specs.private) continue; // we don't want npm to complain :)
let cmd = 'npm publish';
if (publishTag != null) cmd += ` --tag ${publishTag}`;
if (otp != null) cmd += ` --otp ${otp}`;
if (access === 'public' || access === 'restricted')
cmd += ` --access ${access}`;
await exec(cmd, { cwd: pkgPath });
}
}
};
// ------------------------------------------------
// Helpers
// ------------------------------------------------
const prepublishChecks = async ({
checks,
master,
checkUncommitted,
checkUnpulled,
}) => {
if (!checks) return;
if (DEBUG_SKIP_CHECKS) {
mainStory.warn('DEBUG_SKIP_CHECKS should be disabled!!');
}
// Check current branch
const branch = await gitCurBranch();
if (!masterOrMainBranch(branch)) {
if (master) {
mainStory.error(
`Can't publish from current branch: ${chalk.bold(branch)}`
);
if (!DEBUG_SKIP_CHECKS) throw new Error('BRANCH_CHECK_FAILED');
}
mainStory.warn(
`Publishing from a non-master or non-main branch: ${chalk.red.bold(
branch
)}`
);
} else {
mainStory.info(`Current branch: ${chalk.yellow.bold(branch)}`);
}
// Check that the branch is clean
const uncommitted = await gitUncommittedChanges();
if (uncommitted !== '') {
if (checkUncommitted) {
mainStory.error(
`Can't publish with uncommitted changes (stash/commit them): \n${chalk.bold(
uncommitted
)}`
);
if (!DEBUG_SKIP_CHECKS) throw new Error('UNCOMMITTED_CHECK_FAILED');
}
mainStory.warn('Publishing with uncommitted changes');
} else {
mainStory.info('No uncommitted changes');
}
// Check remote history
const unpulled = await gitUnpulledChanges();
if (unpulled !== '0') {
if (checkUnpulled) {
mainStory.error('Remote history differs. Please pull changes');
if (!DEBUG_SKIP_CHECKS) throw new Error('UNPULLED_CHECK_FAILED');
}
mainStory.warn('Publishing with unpulled changes');
} else {
mainStory.info('Remote history matches local history');
}
};
const confirmBuild = async () => {
const { confirmBuild: out } = await inquirer.prompt([
{
name: 'confirmBuild',
type: 'confirm',
message: 'Have you built all your packages for production?',
default: false,
},
]);
return out;
};
const confirmPublish = async ({ pkgList, numPublic, nextVersion }) => {
const { confirmPublish: out } = await inquirer.prompt([
{
name: 'confirmPublish',
type: 'confirm',
message: `Confirm release (${chalk.yellow.bold(
pkgList.length
)} package/s, ${chalk.yellow.bold(numPublic)} public, v${chalk.cyan.bold(
nextVersion
)})?`,
default: false,
},
]);
return out;
};
const validateVersionIncrement = incrementVersionBy => {
if (INCREMENTS.indexOf(incrementVersionBy) < 0) {
mainStory.error(
`Value specified for --increment-version-by: ${chalk.bold(
incrementVersionBy
)} is invalid.`
);
mainStory.error(
`It should be one of (${INCREMENTS.join(', ')}), or not specified.`
);
if (!DEBUG_SKIP_CHECKS) throw new Error('INVALID_INCREMENT_BY_VALUE');
}
};
const getMasterVersion = async (allSpecs, lastTag) => {
let masterVersion = allSpecs[ROOT_PACKAGE].specs.version;
if (lastTag != null) {
const tagVersion = semver.clean(lastTag);
mainStory.info(`Last tag found: ${chalk.yellow.bold(lastTag)}`);
if (tagVersion !== masterVersion) {
mainStory.warn(
`Last tagged version ${chalk.cyan.bold(
tagVersion
)} does not match package.json version ${chalk.cyan.bold(
masterVersion
)}`
);
const { confirm } = await inquirer.prompt([
{
name: 'confirm',
type: 'confirm',
message: 'Continue?',
default: false,
},
]);
if (!confirm) return null;
if (semver.valid(tagVersion) && semver.gt(tagVersion, masterVersion)) {
masterVersion = tagVersion;
}
mainStory.warn(
`Using ${chalk.cyan.bold(
masterVersion
)} as reference (the highest one of both)`
);
}
} else {
mainStory.warn('Repo has no tags yet');
}
if (!semver.valid(masterVersion)) {
mainStory.error(
`Master version ${chalk.cyan.bold(
masterVersion
)} is invalid. Please correct it manually`
);
throw new Error('INVALID_VERSION');
}
return masterVersion;
};
const calcNextVersion = (prevVersion: string, incrementBy = ''): ?string => {
if (!incrementBy) return null;
const isPreRelease = PRERELEASE_INCREMENTS.indexOf(incrementBy) >= 0;
const increment = isPreRelease ? 'prerelease' : incrementBy;
const isNewPreRelease = isPreRelease && prevVersion.indexOf(incrementBy) < 0;
return isNewPreRelease
? `${semver.inc(prevVersion, 'major')}-${incrementBy}.0`
: semver.inc(prevVersion, increment);
};
const promptNextVersion = async (prevVersion: string): Promise<string> => {
const major = semver.inc(prevVersion, 'major');
const minor = semver.inc(prevVersion, 'minor');
const patch = semver.inc(prevVersion, 'patch');
const prerelease = semver.inc(prevVersion, 'prerelease');
const rc = prevVersion.indexOf('rc') < 0 ? `${major}-rc.0` : prerelease;
const beta = prevVersion.indexOf('beta') < 0 ? `${major}-beta.0` : prerelease;
const alpha =
prevVersion.indexOf('alpha') < 0 ? `${major}-alpha.0` : prerelease;
const { nextVersion } = await inquirer.prompt([
{
name: 'nextVersion',
type: 'list',
message: `Current version is ${chalk.cyan.bold(prevVersion)}. Next one?`,
choices: [
{ name: `Major (${chalk.cyan.bold(major)})`, value: major },
{ name: `Minor (${chalk.cyan.bold(minor)})`, value: minor },
{ name: `Patch (${chalk.cyan.bold(patch)})`, value: patch },
{ name: `Release candidate (${chalk.cyan.bold(rc)})`, value: rc },
{ name: `Beta (${chalk.cyan.bold(beta)})`, value: beta },
{ name: `Alpha (${chalk.cyan.bold(alpha)})`, value: alpha },
],
defaultValue: 2,
},
]);
return nextVersion;
};
// ------------------------------------------------
// Public
// ------------------------------------------------
export default run;