|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const commander = require('commander') |
| 4 | +const { doStuff, logger } = require('./runCoverage') |
| 5 | +const { toJson, toLcov } = require('./serialize') |
| 6 | + |
| 7 | +const STATUS_CODE = { |
| 8 | + ERROR: 111, |
| 9 | + OK: 0 |
| 10 | +} |
| 11 | + |
| 12 | +function parseFileName (filePath) { |
| 13 | + return path.resolve(process.cwd(), filePath) |
| 14 | +} |
| 15 | + |
| 16 | +function parseTokenList (tokenString) { |
| 17 | + return tokenString.split(',').map(token => token.trim().toLowerCase()) |
| 18 | +} |
| 19 | + |
| 20 | +commander |
| 21 | + // .usage('[options]') |
| 22 | + .description(`Generate coverage info for a CSS file against an HTML file. |
| 23 | +
|
| 24 | +This supports loading sourcemaps by using the sourceMappingURL=FILENAME.map CSS comment. |
| 25 | +
|
| 26 | +Use the LOG_LEVEL environment variable for more verbose logging. Values: error,warn,info,debug,trace .`) |
| 27 | + .option('--html [path/to/file.html]', 'path to a local HTML file', parseFileName) // TODO: Support multiple |
| 28 | + .option('--css [path/to/file.css]', 'path to a local CSS file', parseFileName) |
| 29 | + .option('--lcov [path/to/output.lcov]', 'the LCOV output file', parseFileName) |
| 30 | + .option('--json [path/to/coverage.json]', 'the coverage.json file', parseFileName) |
| 31 | + .option('--ignore-source-map', 'disable loading the sourcemap if one is found') |
| 32 | + .option('--ignore-declarations [move-to,content]', 'A comma-separated list of declarations to ignore', parseTokenList) |
| 33 | + .parse(process.argv) |
| 34 | + |
| 35 | +// Validate args |
| 36 | +if (!commander.html && !commander.css) { |
| 37 | + commander.help() |
| 38 | +} |
| 39 | +if (commander.html) { |
| 40 | + if (!fs.statSync(commander.html).isFile()) { |
| 41 | + console.error('ERROR: Invalid argument. HTML file not found at ' + commander.html) |
| 42 | + process.exit(STATUS_CODE.ERROR) |
| 43 | + } |
| 44 | +} else { |
| 45 | + console.error('ERROR: Missing argument. At least 1 HTML file must be specified') |
| 46 | + process.exit(STATUS_CODE.ERROR) |
| 47 | +} |
| 48 | +if (commander.css) { |
| 49 | + if (!fs.statSync(commander.css).isFile()) { |
| 50 | + console.error('ERROR: Invalid argument. CSS file not found at ' + commander.css) |
| 51 | + process.exit(STATUS_CODE.ERROR) |
| 52 | + } |
| 53 | +} else { |
| 54 | + console.error('ERROR: Missing argument. A CSS file must be specified') |
| 55 | + process.exit(STATUS_CODE.ERROR) |
| 56 | +} |
| 57 | + |
| 58 | +doStuff(commander.css, commander.html, commander.ignoreDeclarations, commander.ignoreSourceMap) |
| 59 | + .then(({ coverage, sourceMapPath }) => { |
| 60 | + if (commander.lcov) { |
| 61 | + const str = toLcov(coverage, sourceMapPath, commander.lcov) |
| 62 | + fs.writeFileSync(commander.lcov, str) |
| 63 | + } |
| 64 | + if (commander.json) { |
| 65 | + const str = toJson(coverage, sourceMapPath, commander.json) |
| 66 | + fs.writeFileSync(commander.json, str) |
| 67 | + } |
| 68 | + |
| 69 | + logger.debug('Done writing LCOV string') |
| 70 | + }, err => { |
| 71 | + logger.fatal(err) |
| 72 | + process.exit(STATUS_CODE.ERROR) |
| 73 | + }) |
0 commit comments