forked from foolip/mdn-bcd-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
69 lines (61 loc) · 1.71 KB
/
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
//
// mdn-bcd-collector: scripts.js
// A main entry point to run various scripts in a cross-platform friendly way
//
// © Mozilla Corporation, Google LLC, Gooborg Studios
// See the LICENSE file for copyright details
//
import fs from 'node:fs';
import childProcess from 'child_process';
import esMain from 'es-main';
import yargs from 'yargs';
import {hideBin} from 'yargs/helpers';
export const exec = (cmd, env, pipe = true) => {
env = {...process.env, ...env};
if (!pipe) {
console.log(`> ${cmd}`);
}
return childProcess.execSync(cmd, {env, stdio: pipe ? 'pipe' : 'inherit'});
};
const prepare = () => {
// Copy secrets.sample.json to secrets.json if needed
const secretsPath = new URL('./secrets.json', import.meta.url);
const secretsSamplePath = new URL('./secrets.sample.json', import.meta.url);
if (!fs.existsSync(secretsPath)) {
fs.copyFileSync(secretsSamplePath, secretsPath);
}
// Install Firefox for Puppeteer
try {
process.chdir('node_modules/puppeteer');
} catch (e) {
return;
}
exec('node install.js', {PUPPETEER_PRODUCT: 'firefox'}, false);
};
if (esMain(import.meta)) {
const {argv} = yargs(hideBin(process.argv)).command(
'$0 <command>',
'Run an action',
(yargs) => {
yargs.positional('command', {
describe: 'What command to run',
type: 'string',
choices: ['unittest', 'prepare']
});
}
);
switch (argv.command) {
case 'prepare':
prepare();
break;
case 'unittest':
exec(
'c8 mocha --recursive "unittest/**/*.js" --recursive "unittest/**/*.ts"',
{NODE_ENV: 'test'},
false
);
break;
default:
console.error(`Unknown command ${argv.command}!`);
}
}