forked from umijs/umi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.js
More file actions
189 lines (168 loc) · 5.5 KB
/
release.js
File metadata and controls
189 lines (168 loc) · 5.5 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
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
const { yParser, execa, chalk } = require('@umijs/utils');
const { join } = require('path');
const { writeFileSync } = require('fs');
const getRepoInfo = require('git-repo-info');
const inquirer = require('inquirer');
const newGithubReleaseUrl = require('new-github-release-url');
const open = require('open');
const exec = require('./utils/exec');
const syncTNPM = require('./syncTNPM');
const getPackages = require('./utils/getPackages');
const isNextVersion = require('./utils/isNextVersion');
const { getChangelog } = require('./utils/changelog');
const cwd = process.cwd();
const args = yParser(process.argv.slice(2));
const lernaCli = require.resolve('lerna/cli');
function printErrorAndExit(message) {
console.error(chalk.red(message));
process.exit(1);
}
function logStep(name) {
console.log(`${chalk.gray('>> Release:')} ${chalk.magenta.bold(name)}`);
}
async function release() {
// Check git status
if (!args.skipGitStatusCheck && !args.publishOnly) {
const gitStatus = execa.sync('git', ['status', '--porcelain']).stdout;
if (gitStatus.length) {
printErrorAndExit(`Your git status is not clean. Aborting.`);
}
} else {
logStep(
'git status check is skipped, since --skip-git-status-check is supplied',
);
}
// get release notes
let releaseNotes;
if (!args.publishOnly) {
logStep('get release notes');
releaseNotes = await getChangelog();
console.log(releaseNotes(''));
}
// Check npm registry
logStep('check npm registry');
const userRegistry = execa.sync('npm', ['config', 'get', 'registry']).stdout;
if (userRegistry.includes('https://registry.yarnpkg.com/')) {
printErrorAndExit(
`Release failed, please use ${chalk.blue('npm run release')}.`,
);
}
if (!userRegistry.includes('https://registry.npmjs.org/')) {
const registry = chalk.blue('https://registry.npmjs.org/');
printErrorAndExit(`Release failed, npm registry must be ${registry}.`);
}
let updated = null;
if (!args.publishOnly) {
// Get updated packages
logStep('check updated packages');
const updatedStdout = execa.sync(lernaCli, ['changed']).stdout;
updated = updatedStdout
.split('\n')
.map((pkg) => {
if (pkg === 'umi') return pkg;
else return pkg.split('/')[1];
})
.filter(Boolean);
if (!updated.length) {
printErrorAndExit('Release failed, no updated package is updated.');
}
// Clean
logStep('clean');
// Build
if (!args.skipBuild) {
logStep('build');
await exec('npm', ['run', 'build']);
// 为 defineConfig.d.ts 添加 ts-ignore
require('./tsIngoreDefineConfig');
} else {
logStep('build is skipped, since args.skipBuild is supplied');
}
// Bump version
logStep('bump version with lerna version');
await exec(lernaCli, [
'version',
'--exact',
'--no-commit-hooks',
'--no-git-tag-version',
'--no-push',
]);
const currVersion = require('../lerna').version;
// Sync version to root package.json
logStep('sync version to root package.json');
const rootPkg = require('../package');
Object.keys(rootPkg.devDependencies).forEach((name) => {
if (name.startsWith('@umijs/') && !name.startsWith('@umijs/p')) {
rootPkg.devDependencies[name] = currVersion;
}
});
writeFileSync(
join(__dirname, '..', 'package.json'),
JSON.stringify(rootPkg, null, 2) + '\n',
'utf-8',
);
// Commit
const commitMessage = `release: v${currVersion}`;
logStep(`git commit with ${chalk.blue(commitMessage)}`);
await exec('git', ['commit', '--all', '--message', commitMessage]);
// Git Tag
logStep(`git tag v${currVersion}`);
await exec('git', ['tag', `v${currVersion}`]);
// Push
logStep(`git push`);
const { branch } = getRepoInfo();
await exec('git', ['push', 'origin', branch, '--tags']);
}
// Publish
// Umi must be the latest.
const pkgs = args.publishOnly ? getPackages() : updated;
logStep(`publish packages: ${chalk.blue(pkgs.join(', '))}`);
const currVersion = require('../lerna').version;
const isNext = isNextVersion(currVersion);
const releasePkgs = pkgs.sort((a) => {
return a === 'umi' ? 1 : -1;
});
for (const [index, pkg] of releasePkgs.entries()) {
const pkgPath = join(cwd, 'packages', pkg);
const { name, version } = require(join(pkgPath, 'package.json'));
if (version === currVersion) {
console.log(
`[${index + 1}/${pkgs.length}] Publish package ${name} ${
isNext ? 'with next tag' : ''
}`,
);
let cliArgs = isNext ? ['publish', '--tag', 'next'] : ['publish'];
// one-time password from your authenticator
if (args.otp) {
const { otp } = await inquirer.prompt({
name: 'otp',
type: 'input',
message: 'This operation requires a one-time password:',
validate: (msg) => !!msg,
});
cliArgs = cliArgs.concat(['--otp', otp]);
}
const { stdout } = execa.sync('npm', cliArgs, {
cwd: pkgPath,
});
console.log(stdout);
}
}
logStep('create github release');
const tag = `v${currVersion}`;
const changelog = releaseNotes(tag);
console.log(changelog);
const url = newGithubReleaseUrl({
repoUrl: 'https://github.com/umijs/umi',
tag,
body: changelog,
isPrerelease: isNext,
});
await open(url);
logStep('sync packages to tnpm');
syncTNPM(pkgs);
logStep('done');
}
release().catch((err) => {
console.error(err);
process.exit(1);
});