-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrelease.mjs
More file actions
70 lines (61 loc) · 2.19 KB
/
Copy pathrelease.mjs
File metadata and controls
70 lines (61 loc) · 2.19 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
import path from 'path';
import * as url from 'url';
import { $ } from 'execa';
import fs from 'fs-extra';
import { inc } from 'semver';
const RELEASE_TAG = process.env.TAG || 'beta';
const RELEASE_DRY_RUN = process.env.DRY_RUN || 'true';
const RELEASE_VERSION_TYPE = process.env.VERSION || 'prerelease';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const PKG_PATH = path.resolve(__dirname, '../package.json');
const pkg = fs.readJsonSync(PKG_PATH);
const currentVersion = pkg.version;
const nextVersion = inc(currentVersion, RELEASE_VERSION_TYPE);
if (!nextVersion) {
throw new Error(
`Failed to generate next version from "${currentVersion}" with type "${RELEASE_VERSION_TYPE}"`,
);
}
console.info(`Release ${RELEASE_TAG} version ${nextVersion}`);
// Update pkg version
console.info(`Updating version from ${currentVersion} to ${nextVersion}`);
pkg.version = nextVersion;
fs.writeJsonSync(PKG_PATH, pkg, { spaces: 2 });
// Publish to npm
console.info(`Publishing to npm with tag ${RELEASE_TAG}`);
const dryRun = RELEASE_DRY_RUN === 'true' ? ['--dry-run'] : [];
try {
await $`pnpm publish ${dryRun} --tag ${RELEASE_TAG} --no-git-checks`;
console.info(`Published successfully`);
} catch (e) {
console.error(`Publish failed: ${e.message}`);
process.exit(1);
}
// Push tag to github
if (RELEASE_DRY_RUN !== 'true') {
console.info(`Pushing tag to github`);
const tagName = `v${nextVersion}`;
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);
}
try {
await $`git add --all`;
const commitMsg = `release ${tagName}`;
await $`git commit -m ${commitMsg}`;
await $`git push`;
console.info(`Pushed branch successfully`);
} catch (e) {
console.error(`Update branch failed: ${e.message}`);
process.exit(1);
}
}
console.info(`Release completed`);