-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrelease.ts
107 lines (87 loc) · 2.66 KB
/
release.ts
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
// pnpm release --patch -> 1.0.1 (default)
// pnpm release --minor -> 1.1.0
// pnpm release --major -> 2.0.0
// pnpm release --beta -> 1.0.1-beta-1645598740512.0
import fs from 'node:fs';
import chalk from 'chalk';
import semver from 'semver';
import minimist from 'minimist';
import bumpPrompt from '@jsdevtools/version-bump-prompt';
type ReleaseType = 'prerelease' | 'patch' | 'minor' | 'major';
const args = minimist(process.argv.slice(2));
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
const log = (msg: string) => console.log(chalk.cyan(msg));
let type: ReleaseType = 'patch';
async function run(bin: string, args: Array<string>, opts = {}) {
const { execa } = await import('execa');
const result = await execa(bin, args, {
stdio: 'pipe',
cwd: process.cwd(),
...opts,
});
if (result.exitCode) {
log(`${result.stdout}\n${result.stderr}\n`);
console.log(
chalk.red(`\nCommand execution failed, Code "${result.exitCode}"`),
);
process.exit(1);
}
return result;
}
async function release(releaseType: ReleaseType) {
if (!pkg.version) return;
log('\nBuilding...');
await build();
const data = await bumpVersion(releaseType);
log('\nCommitting changes...');
await commit(data.newVersion);
log('\nPublish...');
await publish(data.newVersion);
log('\nPush to GitHub...');
await run('git', ['push']);
await run('git', ['push', '--tags']);
log(`\nUpdated: "${data.oldVersion}" -> "${data.newVersion}"`);
}
function build() {
return run('pnpm', ['run', 'build:core']);
}
async function publish(version: string) {
let publishArgs = ['publish', '--access', 'public', '--no-git-checks'];
if (version) {
let releaseTag = 'latest';
if (version.includes('alpha')) {
releaseTag = 'alpha';
} else if (version.includes('beta')) {
releaseTag = 'beta';
} else if (version.includes('rc')) {
releaseTag = 'rc';
}
publishArgs = publishArgs.concat(['--tag', releaseTag]);
}
return run('pnpm', publishArgs);
}
async function commit(version: string) {
await run('git', ['add', '--all']);
await run('git', ['commit', '-m', `release: v${version}`]);
await run('git', ['tag', `v${version}`]);
}
function bumpVersion(releaseType: ReleaseType) {
if (releaseType === 'prerelease') {
const identifier = `beta-${Number(new Date())}`;
releaseType = semver.inc(pkg.version, releaseType, identifier) as any;
}
return bumpPrompt({
tag: false,
push: false,
release: releaseType,
files: ['./package.json'],
});
}
if (args.beta) {
type = 'prerelease';
} else if (args.minor) {
type = 'minor';
} else if (args.major) {
type = 'major';
}
release(type);