-
Notifications
You must be signed in to change notification settings - Fork 6
/
polendina-cli.js
executable file
·124 lines (115 loc) · 3.15 KB
/
polendina-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
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
#!/usr/bin/env node
import path from 'path'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { log } from './lib/log.js'
import { Polendina } from './polendina.js'
const start = Date.now()
const argv = yargs(hideBin(process.argv))
.usage('$0 testfile.js [testfile2.js [tests/**/test*.js ...] ]')
.option('runner', {
alias: 'r',
type: 'string',
describe: 'The test runner to use',
choices: ['mocha', 'tape', 'bare-sync', 'bare-async'],
default: 'mocha'
})
.option('output-dir', {
alias: 'o',
type: 'string',
describe: 'Location for temporary build resources',
default: path.join(process.cwd(), 'build')
})
.option('page', {
type: 'boolean',
describe: 'Run tests in standard browser page',
default: true
})
.option('worker', {
type: 'boolean',
describe: 'Run tests in a WebWorker',
default: false
})
.option('serviceworker', {
type: 'boolean',
describe: 'Run tests in a ServiceWorker',
default: false
})
.option('stats', {
type: 'boolean',
describe: 'Write webpack-stats.json with bundle',
default: false
})
.option('cleanup', {
type: 'boolean',
describe: 'Remove the output-dir after execution',
default: false
})
.option('timeout', {
type: 'number',
describe: 'Number of seconds to wait before auto-failing the test suite',
default: 30
})
.option('coverage', {
type: 'boolean',
describe: 'Enable coverage reporting',
default: false,
hidden: true
})
.option('webpack-config', {
type: 'string',
describe: 'Supply a path to a webpack.config.js to merge into Polendina\'s Webpack config (use with caution)',
requiresArg: true
})
.option('mocha-reporter', {
type: 'string',
describe: 'Specify the Mocha reporter',
default: 'spec',
requiresArg: true
})
.help('help')
.demandCommand(1, 'You must supply at least one test file')
.check((argv) => {
if (!argv.page && !argv.worker && !argv.serviceworker) {
throw new Error('No mode specified, use one or more of `--page`, `--worker`, `--serviceworker`')
}
if (argv.timeout <= 0) {
throw new Error(`Invalid timeout value (${argv.timeout})`)
}
if (!argv.outputDir) {
throw new Error('--output-dir required')
}
return true
})
.argv
;(async () => {
const polendina = new Polendina(argv)
log(`Setting up output directory: ${polendina.outputDir} ...`)
await polendina.build()
log(`Created bundle: ${path.join(polendina.outputDir, polendina.bundleFile)} ...`)
if (polendina.statsFile) {
log(`Created stats: ${polendina.statsFile} ...`)
}
const errors = await polendina.run()
if (argv.cleanup) {
log(`Removing output directory: ${polendina.outputDir}`)
await polendina.cleanup()
}
let time = (Date.now() - start) / 1000
if (time > 10) {
time = Math.round(time)
} else {
time = Math.round(time * 10) / 10
}
log(`Took ${time} second${time === 1 ? '' : 's'}`)
if (errors) {
return process.exit(errors)
}
})()
.catch((err) => {
console.error(err.stack || err)
if (err.details) {
console.error(err.details)
}
process.exit(1)
})