-
Notifications
You must be signed in to change notification settings - Fork 34
/
publish.js
288 lines (263 loc) · 8.53 KB
/
publish.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* eslint-disable no-console */
const fs = require('fs')
const childProcess = require('child_process')
const writeFileAndGitAdd = (p, content) => {
fs.writeFileSync(p, content)
if (childProcess.spawnSync('git', ['add', p]).status !== 0) {
throw new Error(`failed to execute git add on ${p}`)
}
}
// check arguments
const version = process.argv[2]
if (!version) {
throw new Error('version not given in argv')
}
if (!/[0-9]+\.[0-9]+\.[0-9]+/.test(version)) {
throw new Error('version illegal')
}
// avoid rust warnings
console.info('Run cargo check')
if (
childProcess.spawnSync('cargo', ['check'], {
env: { RUSTFLAGS: '-D warnings', ...process.env },
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to check rust modules (are there rust warnings or errors?)')
}
// force rust formatting
console.info('Run cargo fmt --check')
if (
childProcess.spawnSync('cargo', ['fmt', '--check'], {
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to check formatting of rust modules')
}
// avoid eslint warnings
;[
'glass-easel',
'glass-easel-miniprogram-adapter',
'glass-easel-miniprogram-webpack-plugin',
].forEach((p) => {
console.info(`Run eslint on ${p}`)
if (
childProcess.spawnSync('npx', ['eslint', '-c', '../.eslintrc.js', '.'], {
cwd: p,
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to lint modules (are there eslint warnings or errors?)')
}
})
console.info('Run eslint on glass-easel-miniprogram-template')
if (
childProcess.spawnSync('npx', ['eslint', '-c', '.eslintrc.js', '.'], {
cwd: 'glass-easel-miniprogram-template',
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to lint modules (are there eslint warnings or errors?)')
}
// check git status
const gitStatusRes = childProcess.spawnSync('git', ['diff', '--name-only'], { encoding: 'utf8' })
if (gitStatusRes.status !== 0 || gitStatusRes.stdout.length > 0) {
throw new Error('failed to check git status (are there uncommitted changes?)')
}
// change npm version
;[
'glass-easel/package.json',
'glass-easel-miniprogram-adapter/package.json',
'glass-easel-miniprogram-webpack-plugin/package.json',
'glass-easel-miniprogram-template/package.json',
'glass-easel-template-compiler/package.json',
'glass-easel-stylesheet-compiler/package.json',
].forEach((p) => {
let content = fs.readFileSync(p, { encoding: 'utf8' })
let oldVersion
const refVersions = []
content = content.replace(/"version": "(.+)"/, (_, v) => {
oldVersion = v
return `"version": "${version}"`
})
if (!oldVersion) {
throw new Error(`version segment not found in ${p}`)
}
console.info(`Update ${p} version from "${oldVersion}" to "${version}"`)
refVersions.forEach(({ mod, v }) => {
console.info(` + dependency ${mod} version from "${v}" to "${version}"`)
})
writeFileAndGitAdd(p, content)
})
// change cargo version
;['glass-easel-template-compiler/Cargo.toml', 'glass-easel-stylesheet-compiler/Cargo.toml'].forEach(
(p) => {
let content = fs.readFileSync(p, { encoding: 'utf8' })
let oldVersion
content = content.replace(/\nversion = "(.+)"/, (_, v) => {
oldVersion = v
return `\nversion = "${version}"`
})
if (!oldVersion) {
throw new Error(`version segment not found in ${p}`)
}
console.info(`Update ${p} version from "${oldVersion}" to "${version}"`)
writeFileAndGitAdd(p, content)
},
)
// pnpm install
console.info('Run pnpm install')
if (childProcess.spawnSync('pnpm', ['install'], { stdio: 'inherit' }).status !== 0) {
throw new Error('failed to clean glass-easel dist')
}
// generate cbindgen files for template compiler
console.info('Run cbindgen')
const cbindgenRes = childProcess.spawnSync('cbindgen', [], { cwd: 'glass-easel-template-compiler' })
if (cbindgenRes.status !== 0) {
throw new Error('failed to execute cbindgen for the template compiler')
}
writeFileAndGitAdd(
'glass-easel-template-compiler/glass_easel_template_compiler.h',
cbindgenRes.stdout,
)
// execute wasm-pack
console.info('Run wasm-pack')
;['glass-easel-template-compiler', 'glass-easel-stylesheet-compiler'].forEach((p) => {
if (
childProcess.spawnSync(
'wasm-pack',
['build', p, '--target', 'nodejs', '--out-dir', 'pkg-nodejs'],
{ stdio: 'inherit' },
).status !== 0
) {
throw new Error('failed to execute wasm-pack on the template compiler')
}
})
// compile glass-easel
console.info('Compile glass-easel')
if (childProcess.spawnSync('rm', ['-rf', 'dist'], { cwd: 'glass-easel' }).status !== 0) {
throw new Error('failed to clean glass-easel dist')
}
if (
childProcess.spawnSync('npm', ['run', 'build'], {
cwd: 'glass-easel',
env: { GLASS_EASEL_ARGS: '', ...process.env },
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to compile glass-easel')
}
// compile glass-easel-miniprogram-adapter
console.info('Compile glass-easel-miniprogram-adapter')
if (
childProcess.spawnSync('rm', ['-rf', 'dist'], { cwd: 'glass-easel-miniprogram-adapter' })
.status !== 0
) {
throw new Error('failed to clean glass-easel dist')
}
if (
childProcess.spawnSync('npm', ['run', 'build'], {
cwd: 'glass-easel-miniprogram-adapter',
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to compile glass-easel-miniprogram-adapter')
}
// compile glass-easel-miniprogram-webpack-plugin
console.info('Compile glass-easel-miniprogram-webpack-plugin')
if (
childProcess.spawnSync('npm', ['run', 'build'], {
cwd: 'glass-easel-miniprogram-webpack-plugin',
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to compile glass-easel-miniprogram-webpack-plugin')
}
// compile glass-easel-miniprogram-template
console.info('Compile glass-easel-miniprogram-template')
if (
childProcess.spawnSync('rm', ['-rf', 'dist'], { cwd: 'glass-easel-miniprogram-template' })
.status !== 0
) {
throw new Error('failed to clean glass-easel dist')
}
if (
childProcess.spawnSync('npm', ['run', 'build'], {
cwd: 'glass-easel-miniprogram-template',
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to compile glass-easel-miniprogram-template')
}
// cargo test
console.info('Run cargo test')
if (childProcess.spawnSync('cargo', ['test'], { stdio: 'inherit' }).status !== 0) {
throw new Error('failed to clean glass-easel dist')
}
// npm test
console.info('Run pnpm test')
if (childProcess.spawnSync('pnpm', ['test', '-r'], { stdio: 'inherit' }).status !== 0) {
throw new Error('failed to clean glass-easel dist')
}
// add lock files
;['Cargo.lock', 'pnpm-lock.yaml'].forEach((p) => {
if (childProcess.spawnSync('git', ['add', p]).status !== 0) {
throw new Error(`failed to execute git add on ${p}`)
}
})
// git commit
if (
childProcess.spawnSync('git', ['commit', '--message', `version: ${version}`], {
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to execute git commit')
}
// cargo publish
;['glass-easel-template-compiler', 'glass-easel-stylesheet-compiler'].forEach((p) => {
console.info(`Publish ${p} to crates.io`)
if (childProcess.spawnSync('cargo', ['publish', '-p', p], { stdio: 'inherit' }).status !== 0) {
throw new Error('failed to execute wasm-pack on the template compiler')
}
})
// publish wasm-pack modules
;['glass-easel-template-compiler', 'glass-easel-stylesheet-compiler'].forEach((p) => {
console.info(`Publish wasm-pack generated ${p} to npmjs`)
if (
childProcess.spawnSync('pnpm', ['publish', '--registry', 'https://registry.npmjs.org'], {
cwd: `${p}/pkg-nodejs`,
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to execute wasm-pack on the template compiler')
}
})
// publish js modules
;[
'glass-easel',
'glass-easel-miniprogram-adapter',
'glass-easel-miniprogram-webpack-plugin',
'glass-easel-miniprogram-template',
].forEach((p) => {
console.info(`Publish ${p} to npmjs`)
if (
childProcess.spawnSync('pnpm', ['publish', '--registry', 'https://registry.npmjs.org'], {
cwd: p,
stdio: 'inherit',
}).status !== 0
) {
throw new Error('failed to execute wasm-pack on the template compiler')
}
})
// add a git tag and push
console.info('Push to git origin')
if (childProcess.spawnSync('git', ['tag', `v${version}`]).status !== 0) {
throw new Error('failed to execute git tag')
}
if (childProcess.spawnSync('git', ['push'], { stdio: 'inherit' }).status !== 0) {
throw new Error('failed to execute git push')
}
if (childProcess.spawnSync('git', ['push', '--tags'], { stdio: 'inherit' }).status !== 0) {
throw new Error('failed to execute git push --tags')
}
console.info('All done!')