-
-
Notifications
You must be signed in to change notification settings - Fork 350
/
instrument.js
72 lines (58 loc) · 2.24 KB
/
instrument.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
'use strict'
const NYC = require('../../index.js')
const path = require('path')
const { promisify } = require('util')
const resolveFrom = require('resolve-from')
const rimraf = promisify(require('rimraf'))
const { cliWrapper, setupOptions } = require('./helpers.js')
exports.command = 'instrument <input> [output]'
exports.describe = 'instruments a file or a directory tree and writes the instrumented code to the desired output location'
exports.builder = function (yargs) {
yargs
.demandCommand(0, 0)
.example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')
setupOptions(yargs, 'instrument')
}
exports.handler = cliWrapper(async argv => {
if (argv.output && !argv.inPlace && (path.resolve(argv.cwd, argv.input) === path.resolve(argv.cwd, argv.output))) {
throw new Error('cannot instrument files in place, <input> must differ from <output>. Set \'--in-place\' to force')
}
if (path.relative(argv.cwd, path.resolve(argv.cwd, argv.input)).startsWith('..')) {
throw new Error('cannot instrument files outside project root directory')
}
if (argv.delete && argv.inPlace) {
throw new Error('cannot use \'--delete\' when instrumenting files in place')
}
if (argv.delete && argv.output && argv.output.length !== 0) {
const relPath = path.relative(process.cwd(), path.resolve(argv.output))
if (relPath !== '' && !relPath.startsWith('..')) {
await rimraf(argv.output)
} else {
throw new Error(`attempt to delete '${process.cwd()}' or containing directory.`)
}
}
// If instrument is set to false enable a noop instrumenter.
argv.instrumenter = (argv.instrument)
? './lib/instrumenters/istanbul'
: './lib/instrumenters/noop'
if (argv.inPlace) {
argv.output = argv.input
argv.completeCopy = false
}
const nyc = new NYC(argv)
if (!argv.useSpawnWrap) {
nyc.require.forEach(requireModule => {
const mod = resolveFrom.silent(nyc.cwd, requireModule) || requireModule
require(mod)
})
}
if (argv.baseline) {
if (argv.clean) {
await nyc.reset()
} else {
await nyc.createTempDirectory()
}
await nyc.addAllFiles('baseline/coverage')
}
await nyc.instrumentAllFiles(argv.input, argv.output)
})