-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrelease.js
More file actions
76 lines (67 loc) · 2.13 KB
/
release.js
File metadata and controls
76 lines (67 loc) · 2.13 KB
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
import path from 'path';
import { fileURLToPath } from 'url';
import cac from 'cac';
import { $ } from 'execa';
import fs from 'fs-extra';
let cli = cac('release');
cli.option(
'--dry-run <run>',
'Perform a dry run without publishing or pushing tags',
{
default: 'false',
},
);
cli.option('--tag <tag>', 'The npm tag to publish under (default: canary)', {
default: 'canary',
});
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const PKG_PATH = path.resolve(__dirname, '../package.json');
const pkg = fs.readJsonSync(PKG_PATH);
const publishVersion = pkg.version;
const parsed = cli.parse();
const npmTag = parsed.options.tag;
const isDryRun = parsed.options.dryRun === 'true';
const allowedTags = ['latest', 'canary', 'alpha', 'beta', 'rc', 'nightly'];
if (!allowedTags.includes(npmTag)) {
throw new Error(
`Invalid npm tag: ${npmTag}. Allowed tags: ${allowedTags.join(', ')}`,
);
}
const prereleaseTags = ['alpha', 'beta', 'rc', 'canary', 'nightly'];
if (
npmTag === 'latest' &&
prereleaseTags.some((tag) => publishVersion.includes(tag))
) {
throw Error(`Can't release ${publishVersion} to latest tag`);
}
console.info(
`Release ${npmTag} version ${publishVersion}${isDryRun ? '(dry-run)' : ''}`,
);
try {
const flags = isDryRun
? ['--dry-run', `--tag`, npmTag, `--no-git-checks`]
: [`--tag`, npmTag, `--no-git-checks`];
await $`pnpm publish ${flags}`;
console.info(`Published successfully`);
} catch (e) {
console.error(`Publish failed: ${e.message}`);
process.exit(1);
}
// Push tag to GitHub
if (!isDryRun) {
console.info(`Pushing tag to github`);
const tagName = `v${publishVersion}`;
try {
await $`git config --global --add safe.directory /github/workspace`;
await $`git config --global user.name "github-actions[bot]"`;
await $`git config --global user.email "github-actions[bot]@users.noreply.github.com"`;
await $`git status`;
await $`git tag ${tagName}`;
await $`git push origin ${tagName}`;
console.info(`Pushed tag successfully`);
} catch (e) {
console.error(`Push tag failed: ${e.message}`);
process.exit(1);
}
}
console.info(`Release completed`);