-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.js
executable file
·91 lines (77 loc) · 2.82 KB
/
cli.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
#!/usr/bin/env node
'use strict';
const { spawn } = require('child_process');
const path = require('path');
const yargs = require('yargs');
const { clean: cleanBbcA11y, build: buildBbcA11y } = require('./lib/bbcA11y');
const { run: runLighthouse } = require('./lib/lighthouse');
const logger = require('./lib/colourfulLog');
yargs
.usage('Usage: A11Y_CONFIG=<config-name> $0 <tool> [options] -- [tool options]')
.command('bbc-a11y', 'Runs bbc-a11y', runBbcA11yCli)
.command('lighthouse', 'Runs Google Lighthouse', runLighthouseCli)
.demandCommand()
.alias('h', 'help')
.alias('m', 'mode')
.describe('m', 'The mode to run the specified tool in')
.choices('m', ['interactive', 'headless', 'junit', 'junit-headless', 'ci'])
.demandOption('m', 'You must specify a mode to run the tool in, e.g. junit-headless')
.argv;
async function runBbcA11yCli({ argv }) {
buildBbcA11y();
const { mode } = argv;
const runBbcA11y = mode === 'ci' ? runBbcA11yInCiMode : runBbcA11yOnHost;
const bbcA11yExitCode = await runBbcA11y(argv);
cleanBbcA11y();
process.exit(bbcA11yExitCode);
}
function runLighthouseCli({ argv }) {
const { mode } = argv;
const validModes = ['junit', 'junit-headless', 'headless'];
if (validModes.includes(mode)) {
if (mode === 'junit-headless' || mode === 'headless') {
process.env.A11Y_HEADLESS = true;
}
if (mode === 'headless') {
process.env.A11Y_PRETTY = 'true';
}
return runLighthouse();
}
logger.error(`Unknown mode. Lighthouse is only supported in the following modes: ${validModes.join(', ')}`);
}
function getBbcA11yArgs(mode) {
switch (mode) {
case 'headless':
return [];
case 'junit':
return ['--interactive', '--reporter', './lib/bbcA11YJUnitReporter.js'];
case 'junit-headless':
return ['--reporter', './lib/bbcA11YJUnitReporter.js'];
default:
return ['--interactive'];
}
}
function runBbcA11yOnHost(argv) {
return new Promise((resolve) => {
const { mode } = argv;
const bbcA11yArgs = getBbcA11yArgs(mode);
const configFile = path.resolve(`${__dirname}/a11y.js`);
const bbcA11y = spawn('bbc-a11y', ['--config', configFile, ...bbcA11yArgs, ...argv._.slice(1)]);
bbcA11y.stdout.pipe(process.stdout);
bbcA11y.stderr.pipe(process.stderr);
bbcA11y.on('close', (exitCode) => {
resolve(exitCode);
});
});
}
function runBbcA11yInCiMode(argv) {
const dockerArgs = ['run', '--rm', '--tty', '--volume', `${__dirname}:/ws`, 'bbca11y/bbc-a11y-docker', '--config', '/ws/a11y.js', '--reporter', '/ws/lib/bbcA11YJUnitReporter.js'];
return new Promise((resolve) => {
const bbcA11yDocker = spawn('docker', [...dockerArgs, ...argv._.slice(1)]);
bbcA11yDocker.stdout.pipe(process.stdout);
bbcA11yDocker.stderr.pipe(process.stderr);
bbcA11yDocker.on('close', () => {
resolve(0);
});
});
}