forked from umijs/umi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.ts
215 lines (194 loc) · 5.86 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
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
import * as logger from '@umijs/utils/src/logger';
import { existsSync } from 'fs';
import getGitRepoInfo from 'git-repo-info';
import { join } from 'path';
import rimraf from 'rimraf';
import 'zx/globals';
import { PATHS } from './.internal/constants';
import { assert, eachPkg, getPkgs } from './.internal/utils';
(async () => {
const { branch } = getGitRepoInfo();
logger.info(`branch: ${branch}`);
const pkgs = getPkgs();
logger.info(`pkgs: ${pkgs.join(', ')}`);
// check git status
logger.event('check git status');
const isGitClean = (await $`git status --porcelain`).stdout.trim().length;
assert(!isGitClean, 'git status is not clean');
// check git remote update
logger.event('check git remote update');
await $`git fetch`;
const gitStatus = (await $`git status --short --branch`).stdout.trim();
assert(!gitStatus.includes('behind'), `git status is behind remote`);
// check npm registry
logger.event('check npm registry');
const registry = (await $`npm config get registry`).stdout.trim();
assert(
registry === 'https://registry.npmjs.org/',
'npm registry is not https://registry.npmjs.org/',
);
// check package changed
logger.event('check package changed');
const changed = (await $`lerna changed --loglevel error`).stdout.trim();
assert(changed, `no package is changed`);
// check npm ownership
logger.event('check npm ownership');
const whoami = (await $`npm whoami`).stdout.trim();
try {
await Promise.all(
['umi', '@umijs/core'].map(async (pkg) => {
const owners = (await $`npm owner ls ${pkg}`).stdout
.trim()
.split('\n')
.map((line) => {
return line.split(' ')[0];
});
assert(owners.includes(whoami), `${pkg} is not owned by ${whoami}`);
}),
);
} catch (e: any) {
// only throw ownership error
if (e.message.includes('is not owned by')) {
throw e;
}
}
// check package.json
logger.event('check package.json info');
await $`npm run check:packageFiles`;
// clean
logger.event('clean');
eachPkg(pkgs, ({ dir, name }) => {
logger.info(`clean dist of ${name}`);
rimraf.sync(join(dir, 'dist'));
});
// build packages
logger.event('build packages');
await $`npm run build:release`;
// await $`npm run build:extra`;
//
// logger.event('check client code change');
// const isGitCleanAfterClientBuild = (
// await $`git status --porcelain`
// ).stdout.trim().length;
// assert(!isGitCleanAfterClientBuild, 'client code is updated');
// generate changelog
// TODO
logger.event('generate changelog');
// bump version
logger.event('bump version');
await $`lerna version --exact --no-commit-hooks --no-git-tag-version --no-push --loglevel error`;
const version = require(PATHS.LERNA_CONFIG).version;
let tag = 'latest';
if (
version.includes('-alpha.') ||
version.includes('-beta.') ||
version.includes('-rc.')
) {
tag = 'next';
}
if (version.includes('-canary.')) tag = 'canary';
// update example versions
logger.event('update example versions');
const examplesDir = PATHS.EXAMPLES;
const examples = fs.readdirSync(examplesDir).filter((dir) => {
return (
!dir.startsWith('.') && existsSync(join(examplesDir, dir, 'package.json'))
);
});
examples.forEach((example) => {
const pkg = require(join(examplesDir, example, 'package.json'));
pkg.scripts ||= {};
pkg.scripts['start'] = 'npm run dev';
// change deps version
setDepsVersion({
pkg,
version,
deps: [
'umi',
'@umijs/max',
'@umijs/plugins',
'@umijs/bundler-vite',
'@umijs/preset-vue',
'@umijs/mfsu',
],
});
delete pkg.version;
fs.writeFileSync(
join(examplesDir, example, 'package.json'),
`${JSON.stringify(pkg, null, 2)}\n`,
);
});
// update pnpm lockfile
logger.event('update pnpm lockfile');
$.verbose = false;
await $`pnpm i`;
$.verbose = true;
// commit
logger.event('commit');
await $`git commit --all --message "release: ${version}"`;
// git tag
if (tag !== 'canary') {
logger.event('git tag');
await $`git tag v${version}`;
}
// git push
logger.event('git push');
await $`git push origin ${branch} --tags`;
// npm publish
logger.event('pnpm publish');
$.verbose = false;
const innerPkgs = pkgs.filter((pkg) => !['umi', 'max'].includes(pkg));
// check 2fa config
let otpArg: string[] = [];
if (
(await $`npm profile get "two-factor auth"`).toString().includes('writes')
) {
let code = '';
do {
// get otp from user
code = await question('This operation requires a one-time password: ');
// generate arg for zx command
// why use array? https://github.com/google/zx/blob/main/docs/quotes.md
otpArg = ['--otp', code];
} while (code.length !== 6);
}
await Promise.all(
innerPkgs.map(async (pkg) => {
await $`cd packages/${pkg} && npm publish --tag ${tag} ${otpArg}`;
logger.info(`+ ${pkg}`);
}),
);
await $`cd packages/umi && npm publish --tag ${tag} ${otpArg}`;
logger.info(`+ umi`);
await $`cd packages/max && npm publish --tag ${tag} ${otpArg}`;
logger.info(`+ @umijs/max`);
$.verbose = true;
// sync tnpm
logger.event('sync tnpm');
$.verbose = false;
await Promise.all(
pkgs.map(async (pkg) => {
const { name } = require(path.join(PATHS.PACKAGES, pkg, 'package.json'));
logger.info(`sync ${name}`);
await $`tnpm sync ${name}`;
}),
);
$.verbose = true;
})();
function setDepsVersion(opts: {
deps: string[];
pkg: Record<string, any>;
version: string;
}) {
const { deps, pkg, version } = opts;
pkg.dependencies ||= {};
deps.forEach((dep) => {
if (pkg?.dependencies?.[dep]) {
pkg.dependencies[dep] = version;
}
if (pkg?.devDependencies?.[dep]) {
pkg.devDependencies[dep] = version;
}
});
return pkg;
}