-
Notifications
You must be signed in to change notification settings - Fork 7
/
release.js
162 lines (141 loc) · 4.19 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
const args = require('minimist')(process.argv.slice(2));
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const semver = require('semver');
const execa = require('execa');
const currentVersion = require('./package.json').version;
const { prompt } = require('enquirer');
const preId =
args.preid ||
(semver.prerelease(currentVersion) && semver.prerelease(currentVersion)[0]);
const skipBuild = args.skipBuild;
const versionIncrements = [
'patch',
'minor',
'major',
...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : []),
];
const inc = (i) => semver.inc(currentVersion, i, preId);
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts });
const step = (msg) => console.log(chalk.cyan(msg));
const updatePackage = (pkgRoot, version) => {
const pkgPath = path.resolve(pkgRoot, 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
pkg.version = version;
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
};
const publishPackage = async (version, tag) => {
const pkgPath = path.resolve(path.resolve(__dirname, ''), 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
const pkgName = pkg.name;
step(`Publishing ${pkgName}...`);
try {
await run('yarn', [
'publish',
'--new-version',
version,
...(tag ? ['--tag', tag] : []),
'--access',
'public',
]);
console.log(chalk.green(`Successfully published ${pkgName}@${version}`));
} catch (e) {
if (e.stderr.match(/previously published/)) {
console.log(chalk.red(`Skipping already published: ${pkgName}`));
} else {
throw e;
}
}
};
const main = async () => {
let targetVersion = args._[0];
if (!targetVersion) {
// no explicit version, offer suggestions
const { release } = await prompt({
type: 'select',
name: 'release',
message: 'Select release type',
choices: versionIncrements
.map((i) => `${i} (${inc(i)})`)
.concat(['custom']),
});
if (release === 'custom') {
targetVersion = (
await prompt({
type: 'input',
name: 'version',
message: 'Input custom version',
initial: currentVersion,
})
).version;
} else {
targetVersion = release.match(/\((.*)\)/)[1];
}
if (!semver.valid(targetVersion)) {
throw new Error(`invalid target version: ${targetVersion}`);
}
const { yes } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing v${targetVersion}. Confirm?`,
});
if (!yes) {
return;
}
let releaseTag = args.tag || null;
if (!releaseTag) {
const { tag } = await prompt({
type: 'select',
name: 'tag',
message: 'Select release type',
choices: ['next', 'latest'].map((i) => `${i}`),
});
releaseTag = tag;
const { yes } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing with --tag ${releaseTag}. Confirm?`,
});
if (!yes) {
return;
}
}
// update package versions
step('\nUpdate package.json...');
updatePackage(path.resolve(__dirname, ''), targetVersion);
// build package
step('\nBuilding package...');
if (!skipBuild) {
await run('yarn', ['build:lib']);
await run('yarn', ['build:demo']);
} else {
console.log(`(skipped)`);
}
const { stdout } = await run('git', ['diff'], { stdio: 'pipe' });
if (stdout) {
step('\nCommitting changes...');
await run('git', ['add', '-A']);
await run('git', [
'commit',
'-m',
`:tada: :rocket: release: v${targetVersion}`,
]);
} else {
console.log('No changes to commit.');
}
// publish package
step('\nPublishing package...');
await publishPackage(targetVersion, releaseTag);
// push to GitHub
step('\nPushing to GitHub...');
await run('git', ['tag', `v${targetVersion}`]);
await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`]);
await run('git', ['push', 'origin', 'HEAD']);
console.log();
}
};
main().catch((err) => {
console.error(err);
});