forked from versatica/mediasoup
-
Notifications
You must be signed in to change notification settings - Fork 5
/
npm-scripts.js
325 lines (255 loc) · 5.81 KB
/
npm-scripts.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
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
/* eslint-disable no-console */
const process = require('process');
const os = require('os');
const fs = require('fs');
const { execSync } = require('child_process');
const { version } = require('./package.json');
const isFreeBSD = os.platform() === 'freebsd';
const isWindows = os.platform() === 'win32';
const task = process.argv.slice(2).join(' ');
// mediasoup mayor version.
const MAYOR_VERSION = version.split('.')[0];
// make command to use.
const MAKE = process.env.MAKE || (isFreeBSD ? 'gmake' : 'make');
console.log(`npm-scripts.js [INFO] running task "${task}"`);
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 and flatbuffers to JavaScript.
case 'prepare':
{
buildTypescript(/* force */ false);
// TODO: Compile flatbuffers.
break;
}
case 'postinstall':
{
if (!process.env.MEDIASOUP_WORKER_BIN)
{
buildWorker();
if (!process.env.MEDIASOUP_LOCAL_DEV)
{
cleanWorker();
}
}
break;
}
case 'typescript:build':
{
installNodeDeps();
buildTypescript(/* force */ true);
replaceVersion();
break;
}
case 'typescript:watch':
{
deleteNodeLib();
const { TscWatchClient } = require('tsc-watch/client');
const watch = new TscWatchClient();
watch.on('success', replaceVersion);
watch.start('--project', 'node', '--pretty');
break;
}
case 'worker:build':
{
buildWorker();
break;
}
case 'lint:node':
{
lintNode();
break;
}
case 'lint:worker':
{
lintWorker();
break;
}
case 'format:worker':
{
executeCmd(`${MAKE} format -C worker`);
break;
}
case 'test:node':
{
buildTypescript(/* force */ false);
replaceVersion();
testNode();
break;
}
case 'test:worker':
{
testWorker();
break;
}
case 'coverage:node':
{
buildTypescript(/* force */ false);
replaceVersion();
executeCmd('jest --coverage');
executeCmd('open-cli coverage/lcov-report/index.html');
break;
}
case 'install-deps:node':
{
installNodeDeps();
break;
}
case 'install-clang-tools':
{
executeCmd('npm ci --prefix worker/scripts');
break;
}
case 'release:check':
{
checkRelease();
break;
}
case 'release':
{
checkRelease();
executeCmd(`git commit -am '${version}'`);
executeCmd(`git tag -a ${version} -m '${version}'`);
executeCmd(`git push origin v${MAYOR_VERSION}`);
executeCmd(`git push origin '${version}'`);
executeCmd('npm publish');
break;
}
default:
{
throw new TypeError(`unknown task "${task}"`);
}
}
function replaceVersion()
{
console.log('npm-scripts.js [INFO] replaceVersion()');
const files =
[
'node/lib/index.js',
'node/lib/index.d.ts',
'node/lib/Worker.js'
];
for (const file of files)
{
const text = fs.readFileSync(file, { encoding: 'utf8' });
const result = text.replace(/__MEDIASOUP_VERSION__/g, version);
fs.writeFileSync(file, result, { encoding: 'utf8' });
}
}
function deleteNodeLib()
{
if (!fs.existsSync('node/lib'))
{
return;
}
console.log('npm-scripts.js [INFO] deleteNodeLib()');
if (!isWindows)
{
executeCmd('rm -rf node/lib');
}
else
{
// NOTE: This command fails in Windows if the dir doesn't exist.
executeCmd('rmdir /s /q "node/lib"', /* exitOnError */ false);
}
}
function buildTypescript(force = false)
{
if (!force && fs.existsSync('node/lib'))
{
return;
}
console.log('npm-scripts.js [INFO] buildTypescript()');
deleteNodeLib();
executeCmd('tsc --project node');
}
function buildWorker()
{
console.log('npm-scripts.js [INFO] buildWorker()');
executeCmd(`${MAKE} -C worker`);
}
function cleanWorker()
{
console.log('npm-scripts.js [INFO] cleanWorker()');
// Clean build artifacts except `mediasoup-worker`.
executeCmd(`${MAKE} clean-build -C worker`);
// Clean downloaded dependencies.
executeCmd(`${MAKE} clean-subprojects -C worker`);
// Clean PIP/Meson/Ninja.
executeCmd(`${MAKE} clean-pip -C worker`);
}
function lintNode()
{
console.log('npm-scripts.js [INFO] lintNode()');
executeCmd('eslint -c node/.eslintrc.js --max-warnings 0 node/src node/.eslintrc.js npm-scripts.js worker/scripts/gulpfile.js');
}
function lintWorker()
{
console.log('npm-scripts.js [INFO] lintWorker()');
executeCmd(`${MAKE} lint -C worker`);
}
function testNode()
{
console.log('npm-scripts.js [INFO] testNode()');
if (!process.env.TEST_FILE)
{
executeCmd('jest');
}
else
{
executeCmd(`jest --testPathPattern ${process.env.TEST_FILE}`);
}
}
function testWorker()
{
console.log('npm-scripts.js [INFO] testWorker()');
executeCmd(`${MAKE} test -C worker`);
}
function installNodeDeps()
{
console.log('npm-scripts.js [INFO] installNodeDeps()');
// Install/update Node deps.
executeCmd('npm ci --ignore-scripts');
// Update package-lock.json.
executeCmd('npm install --package-lock-only --ignore-scripts');
}
function checkRelease()
{
console.log('npm-scripts.js [INFO] checkRelease()');
installNodeDeps();
buildTypescript(/* force */ true);
replaceVersion();
buildWorker();
lintNode();
lintWorker();
testNode();
testWorker();
}
function executeCmd(command, exitOnError = true)
{
console.log(`npm-scripts.js [INFO] executeCmd(): ${command}`);
try
{
execSync(command, { stdio: [ 'ignore', process.stdout, process.stderr ] });
}
catch (error)
{
if (exitOnError)
{
console.error(`npm-scripts.js [ERROR] executeCmd() failed, exiting: ${error}`);
process.exit(1);
}
else
{
console.log(`npm-scripts.js [INFO] executeCmd() failed, ignoring: ${error}`);
}
}
}