forked from vime-js/vime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
272 lines (231 loc) · 7.03 KB
/
release.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Thanks: https://github.com/vuejs/vue-next/blob/master/scripts/release.js
*/
import kleur from 'kleur';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import execa from 'execa';
import fs from 'fs';
import minimist from 'minimist';
import path from 'path';
import prompt from 'enquirer';
import semver from 'semver';
const require = createRequire(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const args = minimist(process.argv.slice(2));
const isDryRun = args.dry;
const skipBuild = args.skipBuild;
const skippedPackages = [];
const currentVersion = require('../package.json').version;
if (isDryRun) console.log(kleur.cyan('\n☂️ Running in dry mode...\n'));
const packages = fs
.readdirSync(path.resolve(__dirname, '../packages'))
.filter(p => !p.startsWith('.'));
const examples = fs
.readdirSync(path.resolve(__dirname, '../examples'))
.filter(p => !p.startsWith('.') && !p.startsWith('react'));
const preId =
args.preid ||
(semver.prerelease(currentVersion) && semver.prerelease(currentVersion)[0]);
const versionIncrements = [
'patch',
'minor',
'major',
...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : []),
];
function inc(i) {
return semver.inc(currentVersion, i, preId);
}
async function run(bin, args, opts = {}) {
return execa(bin, args, { stdio: 'inherit', ...opts });
}
async function dryRun(bin, args, opts = {}) {
console.info(kleur.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts);
}
const runIfNotDry = isDryRun ? dryRun : run;
function getPkgRoot(pkgName) {
return path.resolve(__dirname, '../packages/' + pkgName);
}
function getExampleRoot(pkgName) {
return path.resolve(__dirname, '../examples/' + pkgName);
}
function step(msg) {
console.info('\n✨ ' + kleur.cyan(msg) + '\n');
}
async function main() {
let targetVersion = args._[0];
if (!targetVersion) {
const { release } = /** @type {{ release: string }} */ (
await prompt.prompt({
type: 'select',
name: 'release',
message: 'Select release type',
choices: versionIncrements
.map(i => `${i} (${inc(i)})`)
.concat(['custom']),
})
);
if (release === 'custom') {
targetVersion = /** @type {{ version: string }} */ (
await prompt.prompt({
type: 'input',
name: 'version',
message: 'Input custom version',
initial: currentVersion,
})
).version;
} else {
targetVersion = /** @type {string[]} */ (release.match(/\((.*)\)/))[1];
}
}
if (!semver.valid(targetVersion)) {
throw new Error(kleur.red(`🚨 invalid target version: ${targetVersion}`));
}
const { yes } = /** @type {{ yes: boolean }} */ (
await prompt.prompt({
type: 'confirm',
name: 'yes',
message: `Releasing v${targetVersion}. Confirm?`,
})
);
if (!yes) {
return;
}
// update all package versions and inter-dependencies
step('Updating cross dependencies...');
updateVersions(targetVersion);
// update lockfile
step('Updating lockfile...');
await run(`pnpm`, ['install']);
// build all packages
step('Building all packages...');
if (!skipBuild && !isDryRun) {
await run('npm', ['run', 'build:all']);
} else {
console.log(`(skipped)`);
}
// generate changelog
step('Generating changelog...');
await run(`npm`, ['run', 'changelog']);
const { stdout } = await run('git', ['diff'], { stdio: 'pipe' });
if (stdout) {
step('Committing changes...');
await runIfNotDry('git', ['add', '-A']);
await runIfNotDry('git', [
'commit',
'-m',
`chore(release): v${targetVersion}`,
]);
} else {
console.log('No changes to commit.');
}
// publish packages
for (const pkg of packages) {
await publishPackage(pkg, targetVersion, runIfNotDry);
}
// push to GitHub
step('Pushing to GitHub...');
await runIfNotDry('git', ['tag', `v${targetVersion}`]);
await runIfNotDry('git', ['push', 'origin', `refs/tags/v${targetVersion}`]);
await runIfNotDry('git', ['push']);
if (isDryRun) {
console.log(`\nDry run finished - run git diff to see package changes.`);
}
if (skippedPackages.length) {
console.log(
kleur.yellow(
`The following packages are skipped and NOT published:\n- ${skippedPackages.join(
'\n- ',
)}`,
),
);
}
console.log();
}
function updateVersions(version) {
// 1. update root package.json
updatePackageVersion(path.resolve(__dirname, '..'), version);
// 2. update all packages
packages.forEach(p => updatePackageVersion(getPkgRoot(p), version));
// 3. update examples
examples.forEach(p => updatePackageVersion(getExampleRoot(p), version, true));
}
function updatePackageVersion(pkgRoot, version, includeDevDeps = false) {
const pkgPath = path.resolve(pkgRoot, 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
pkg.version = version;
updatePackageDeps(pkg, 'dependencies', version);
updatePackageDeps(pkg, 'peerDependencies', version);
if (includeDevDeps) updatePackageDeps(pkg, 'devDependencies', version);
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
}
function updatePackageDeps(pkg, depType, version) {
const deps = pkg[depType];
if (!deps) return;
Object.keys(deps).forEach(dep => {
if (
dep.startsWith('@vime') &&
packages.includes(dep.replace(/^@vime\//, ''))
) {
console.log(
kleur.yellow(`🦠 ${pkg.name} -> ${depType} -> ${dep}@${version}`),
);
deps[dep] = version;
}
});
}
async function publishPackage(pkgName, version, runIfNotDry) {
if (skippedPackages.includes(pkgName)) {
return;
}
const pkgRoot = getPkgRoot(pkgName);
const pkgPath = path.resolve(pkgRoot, 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
if (pkg.private) {
return;
}
let releaseTag = null;
if (args.tag) {
releaseTag = args.tag;
} else if (version.includes('alpha')) {
releaseTag = 'alpha';
} else if (version.includes('beta')) {
releaseTag = 'beta';
} else if (version.includes('rc')) {
releaseTag = 'rc';
} else {
// releaseTag = 'next';
}
step(`Publishing ${pkgName}...`);
const publishDir = pkg.publishConfig?.directory;
try {
await runIfNotDry(
// use of yarn is intentional here as we rely on its publishing behavior.
'yarn',
[
...(publishDir ? ['publish', publishDir] : ['publish']),
'--new-version',
version,
...(releaseTag ? ['--tag', releaseTag] : []),
'--access',
'public',
...(publishDir ? ['--no-git-tag-version'] : []),
],
{
cwd: pkgRoot,
stdio: 'pipe',
},
);
console.log(kleur.green(`✅ Successfully published ${pkgName}@${version}`));
} catch (e) {
if (/** @type {any} */ (e).stderr.match(/previously published/)) {
console.log(kleur.red(`🚫 Skipping already published: ${pkgName}`));
} else {
throw e;
}
}
}
main().catch(err => {
console.error(err);
process.exit(1);
});