This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrelease-npm.js
87 lines (87 loc) · 4.08 KB
/
release-npm.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
"use strict";
(function () {
const gitFactory = require("simple-git"), spawn = requireModule("spawn"), gulp = requireModule("gulp"), gutil = requireModule("gulp-util"), ZarroError = requireModule("zarro-error"), { ask } = requireModule("ask"), readGitInfo = requireModule("read-git-info"), env = requireModule("env"), readPackageVersion = requireModule("read-package-version"), { runTask } = requireModule("run-task"), { withEnvironment } = requireModule("temporary-environment"), alterPackageJsonVersion = requireModule("alter-package-json-version");
async function rollBackPackageJson() {
await alterPackageJsonVersion({ loadUnsetFromEnvironment: true, incrementBy: -1 });
}
gulp.task("release-npm", ["increment-package-json-version"], async () => {
const dryRun = env.resolveFlag("DRY_RUN"), git = gitFactory(), version = await readPackageVersion(), isBeta = env.resolveFlag("BETA"), releaseTag = env.resolve("TAG"), tag = `v${version}`, gitInfo = await readGitInfo();
if (version === undefined) {
throw new ZarroError(`unable to read version from package.json`);
}
try {
if (gitInfo.isGitRepository) {
const branchInfo = await git.branch();
// ignore this error: couldn't find remote ref HEAD
// -> means this is an unknown (new) branch: we should push -u
if (gitInfo.remotes.length) {
try {
await git.pull(gitInfo.primaryRemote, branchInfo.current, {
"--rebase": true,
"--autostash": true
});
}
catch (ex) {
const e = ex;
const isNewBranch = (e.message || e.toString()).indexOf("couldn't find remote ref HEAD") > -1;
if (!isNewBranch) {
// noinspection ExceptionCaughtLocallyJS
throw e;
}
}
}
}
if (dryRun) {
gutil.log(gutil.colors.yellow(`would publish ${version}`));
}
else {
const access = env.resolve("NPM_PUBLISH_ACCESS"), args = ["publish", "--access", access];
if (!!releaseTag) {
args.push("--tag", releaseTag);
}
else if (isBeta) {
args.push("--tag", "beta");
}
const skipOTP = env.resolveFlag(env.NPM_PUBLISH_SKIP_OTP);
if (!skipOTP && await npmSupportsOtpSwitch()) {
const otp = await ask("Please enter your 2FA OTP for NPM");
args.push("--otp");
args.push(otp);
}
await spawn("npm", args, {
interactive: true
});
}
}
catch (e) {
await rollBackPackageJson();
throw e;
}
if (dryRun) {
gutil.log(gutil.colors.yellow(`would commit all updated files`));
await rollBackPackageJson();
}
else if (gitInfo.isGitRepository) {
const messageTemplate = env.resolve(env.GIT_VERSION_INCREMENT_MESSAGE), message = messageTemplate.replace(/%VERSION%/g, version);
if (dryRun) {
gutil.log(`would have committed all outstanding changes with message: '${message}'`);
}
else {
await git.add(":/");
await git.commit(`:bookmark: bump package version to ${version}`);
}
await withEnvironment({
[env.GIT_TAG]: tag
}).run(async () => {
await runTask("git-tag-and-push");
});
}
});
async function npmSupportsOtpSwitch() {
const result = await spawn("npm", ["publish", "--help"], {
suppressOutput: true
});
const allStdOut = result.stdout.join("\n");
return allStdOut.indexOf("--otp") > -1;
}
})();