-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
npm-scripts.mjs
331 lines (259 loc) · 7.55 KB
/
npm-scripts.mjs
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import process from 'node:process';
import os from 'node:os';
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
const PKG = JSON.parse(fs.readFileSync('./package.json').toString());
const IS_WINDOWS = os.platform() === 'win32';
const MAYOR_VERSION = PKG.version.split('.')[0];
const PYTHON = getPython();
const PIP_DEPS_DIR = path.resolve('worker/pip_deps');
const PIP_DEV_DEPS_DIR = path.resolve('worker/pip_dev_deps');
// Paths for ESLint to check. Converted to string for convenience.
const ESLINT_PATHS = ['eslint.config.mjs', 'src', 'npm-scripts.mjs'].join(' ');
// Paths for ESLint to ignore. Converted to string argument for convenience.
const ESLINT_IGNORE_PATTERN_ARGS = []
.map(entry => `--ignore-pattern ${entry}`)
.join(' ');
// Paths for Prettier to check/write. Converted to string for convenience.
// NOTE: Prettier ignores paths in .gitignore so we don't need to care about
// node/src/fbs.
const PRETTIER_PATHS = [
'README.md',
'eslint.config.mjs',
'src',
'npm-scripts.mjs',
'package.json',
'tsconfig.json',
].join(' ');
const task = process.argv[2];
const args = process.argv.slice(3).join(' ');
// Set PYTHONPATH env since we use custom locations for locally installed PIP
// deps.
if (IS_WINDOWS) {
process.env.PYTHONPATH = `${PIP_DEPS_DIR};${PIP_DEV_DEPS_DIR};${process.env.PYTHONPATH}`;
} else {
process.env.PYTHONPATH = `${PIP_DEPS_DIR}:${PIP_DEV_DEPS_DIR}:${process.env.PYTHONPATH}`;
}
run();
async function run() {
logInfo(args ? `[args:"${args}"]` : '');
switch (task) {
// As per NPM documentation (https://docs.npmjs.com/cli/v9/using-npm/scripts)
// `prepare` script:
//
// - Runs BEFORE the package is packed, i.e. during `npm publish` and `npm pack`.
// - Runs on local `npm install` without any arguments.
// - NOTE: If a package being installed through git contains a `prepare` script,
// its dependencies and devDependencies will be installed, and the `prepare`
// script will be run, before the package is packaged and installed.
//
// So here we compile TypeScript to JavaScript.
case 'prepare': {
buildTypescript({ force: false });
break;
}
case 'postinstall': {
installPythonDeps();
break;
}
case 'typescript:build': {
installNodeDeps();
buildTypescript({ force: true });
replacePythonVersion();
break;
}
case 'typescript:watch': {
deleteNodeLib();
executeCmd(`tsc --watch ${args}`);
break;
}
case 'lint:node': {
lintNode();
break;
}
case 'lint:python': {
lintPython();
break;
}
case 'format:node': {
formatNode();
break;
}
case 'test': {
buildTypescript({ force: false });
replacePythonVersion();
test();
break;
}
case 'coverage': {
buildTypescript({ force: false });
replacePythonVersion();
executeCmd('jest --coverage');
executeCmd('open-cli coverage/lcov-report/index.html');
break;
}
case 'release:check': {
checkRelease();
break;
}
case 'release': {
checkRelease();
executeCmd(`git commit -am '${PKG.version}'`, /* exitOnError */ false);
executeCmd(`git tag -a ${PKG.version} -m '${PKG.version}'`);
executeCmd(`git push origin v${MAYOR_VERSION}`);
executeCmd(`git push origin '${PKG.version}'`);
executeCmd('npm publish');
break;
}
default: {
logError('unknown task');
exitWithError();
}
}
}
function getPython() {
let python = process.env.PYTHON;
if (!python) {
try {
execSync('python3 --version', { stdio: ['ignore', 'ignore', 'ignore'] });
python = 'python3';
} catch (error) {
python = 'python';
}
}
return python;
}
function replacePythonVersion() {
logInfo('replacePythonVersion()');
const file = 'worker/setup.py';
const text = fs.readFileSync(file, { encoding: 'utf8' });
const result = text.replace(/version=".*"/g, `version="${PKG.version}"`);
fs.writeFileSync(file, result, { encoding: 'utf8' });
}
function deleteNodeLib() {
if (!fs.existsSync('lib')) {
return;
}
logInfo('deleteNodeLib()');
fs.rmSync('node/lib', { recursive: true, force: true });
}
function buildTypescript({ force = false } = { force: false }) {
if (!force && fs.existsSync('lib')) {
return;
}
logInfo('buildTypescript()');
deleteNodeLib();
executeCmd('tsc');
}
function lintNode() {
logInfo('lintNode()');
// Ensure there are no rules that are unnecessary or conflict with Prettier
// rules.
executeCmd('eslint-config-prettier eslint.config.mjs');
executeCmd(
`eslint -c eslint.config.mjs --max-warnings 0 ${ESLINT_IGNORE_PATTERN_ARGS} ${ESLINT_PATHS}`
);
executeCmd(`prettier --check ${PRETTIER_PATHS}`);
}
function lintPython() {
logInfo('lintPython()');
installPythonDevDeps();
executeCmd(`cd worker && "${PYTHON}" -m flake8 --filename *.py && cd ..`);
executeCmd(
`cd worker && "${PYTHON}" -m mypy --exclude pip_deps --exclude pip_dev_deps . && cd ..`
);
}
function formatNode() {
logInfo('formatNode()');
executeCmd(`prettier --write ${PRETTIER_PATHS}`);
}
function test() {
logInfo('test()');
executeCmd(`jest --silent false --detectOpenHandles ${args}`);
}
function installNodeDeps() {
logInfo('installNodeDeps()');
// Install/update deps.
executeCmd('npm ci --ignore-scripts');
// Update package-lock.json.
executeCmd('npm install --package-lock-only --ignore-scripts');
}
function installPythonDeps() {
logInfo('installPythonDeps()');
// Install PIP deps into custom location, so we don't depend on system-wide
// installation.
// However this may fail due to different PIP and OS versions, so let's do a
// best effort.
const res = executeCmd(
`"${PYTHON}" -m pip install --upgrade --no-user --target="${PIP_DEPS_DIR}" ${args} worker/`,
/* exitOnError */ false
);
if (!res) {
executeCmd(
`"${PYTHON}" -m pip install --upgrade --no-user --target="${PIP_DEPS_DIR}" ${args} --break-system-packages worker/`,
/* exitOnError */ true
);
}
}
function installPythonDevDeps() {
logInfo('installPythonDevDeps()');
// Install PIP dev deps into custom location, so we don't depend on system-wide
// installation.
executeCmd(
`"${PYTHON}" -m pip install --upgrade --no-user --target="${PIP_DEV_DEPS_DIR}" flake8 mypy`,
/* exitOnError */ true
);
}
function checkRelease() {
logInfo('checkRelease()');
installNodeDeps();
installPythonDeps();
buildTypescript({ force: true });
replacePythonVersion();
lintNode();
// TODO: Disabled due to
// https://github.com/versatica/mediasoup-client-aiortc/issues/25
// lintPython();
// Tests fail sometimes due to OS/network stuff.
if (process.env.SKIP_TEST !== 'true') {
test();
}
}
/**
* Returns true if the command succeeded, 0 otherwise.
*
* If exitOnError is set and command fails, then process is terminated with
* error.
*/
function executeCmd(command, exitOnError = true) {
logInfo(`executeCmd(): ${command}`);
try {
execSync(command, { stdio: ['ignore', process.stdout, process.stderr] });
return true;
} catch (error) {
if (exitOnError) {
logError(`executeCmd() failed, exiting: ${error}`);
exitWithError();
} else {
logInfo(`executeCmd() failed, ignoring: ${error}`);
return false;
}
}
}
function logInfo(message) {
// eslint-disable-next-line no-console
console.log(`npm-scripts \x1b[36m[INFO] [${task}]\x1b[0m`, message);
}
// eslint-disable-next-line no-unused-vars
function logWarn(message) {
// eslint-disable-next-line no-console
console.warn(`npm-scripts \x1b[33m[WARN] [${task}]\x1b[0m`, message);
}
function logError(message) {
// eslint-disable-next-line no-console
console.error(`npm-scripts \x1b[31m[ERROR] [${task}]\x1b[0m`, message);
}
function exitWithError() {
process.exit(1);
}