-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpublish.mjs
executable file
·59 lines (48 loc) · 1.66 KB
/
publish.mjs
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
#!/usr/bin/env node
/* eslint flowtype/require-valid-file-annotation: off */
import { env } from "process";
import { $, question } from "zx";
try {
await $`git diff-files --quiet`;
await $`git diff-index --quiet --cached HEAD`;
} catch (err) {
throw new Error(
`Cannot continue with unstaged or uncommitted changes\nExit code: ${err.exitCode}`
);
}
await $`npm test`;
// This will determine the type of release based on the git branch. When the default branch is used, it will be a `patch` that's published to npm under the `latest` dist-tag. Any other branch will be a `prelease` that's published to npm under the `alpha` dist-tag.
let BUMP = "patch";
let TAG = "latest";
const { NPM_TOKEN } = env;
let { stdout: CURRENT_BRANCH } = await $`git rev-parse --abbrev-ref HEAD`;
CURRENT_BRANCH = CURRENT_BRANCH.trim();
let { stdout: DEFAULT_BRANCH } =
await $`git remote show origin | sed -n '/HEAD branch/s/.*: //p'`;
DEFAULT_BRANCH = DEFAULT_BRANCH.trim();
if (CURRENT_BRANCH !== DEFAULT_BRANCH) {
BUMP = "prerelease";
TAG = "alpha";
await $`npm --no-git-tag-version version ${BUMP} --preid=${TAG}`;
} else {
await $`npm version ${BUMP}`;
}
// Push and publish!
await $`git push`;
let twoFactorCode;
if (NPM_TOKEN) {
if (TAG === "alpha") {
await $`NPM_TOKEN=${NPM_TOKEN} npm publish --tag ${TAG}`;
} else {
await $`git push --tags`;
await $`NPM_TOKEN=${NPM_TOKEN} npm publish --tag ${TAG}`;
}
} else {
twoFactorCode = await question("NPM 2FA Code: ");
if (TAG === "alpha") {
await $`npm publish --tag ${TAG} --otp ${twoFactorCode}`;
} else {
await $`git push --tags`;
await $`npm publish --tag ${TAG} --otp ${twoFactorCode}`;
}
}