From b2ffe520915c67c06118d0fefe56ce0183a6ad5e Mon Sep 17 00:00:00 2001 From: Anthony Nandaa Date: Tue, 18 Sep 2018 22:21:54 +0300 Subject: [PATCH] chore(bin): rewrite to async I/O - Replace mkdirp with the new fs.mkdir with `recursive` option - Rewrite all top level IO to async (mkdir and rimraf) Remaining work: to rewrite lib/*.js to async I/O Ref: #9 --- bin/c8.js | 36 +++++++++++++++++++++--------------- package-lock.json | 1 + package.json | 1 - test/integration.js.snap | 6 +++--- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/bin/c8.js b/bin/c8.js index ad0ca7f2a..97d684948 100755 --- a/bin/c8.js +++ b/bin/c8.js @@ -1,8 +1,10 @@ #!/usr/bin/env node 'use strict' +const fs = require('fs') +const util = require('util') + const foreground = require('foreground-child') -const mkdirp = require('mkdirp') const report = require('../lib/report') const rimraf = require('rimraf') const { @@ -14,20 +16,7 @@ const { const instrumenterArgs = hideInstrumenteeArgs() let argv = yargs.parse(instrumenterArgs) -if (argv._[0] === 'report') { - argv = yargs.parse(process.argv) // support flag arguments after "report". - outputReport() -} else { - if (argv.clean) { - rimraf.sync(argv.tempDirectory) - mkdirp.sync(argv.tempDirectory) - } - process.env.NODE_V8_COVERAGE = argv.tempDirectory - - foreground(hideInstrumenterArgs(argv), () => { - outputReport() - }) -} +const _p = util.promisify function outputReport () { report({ @@ -40,3 +29,20 @@ function outputReport () { omitRelative: argv.omitRelative }) } + +(async function run () { + if (argv._[0] === 'report') { + argv = yargs.parse(process.argv) // support flag arguments after "report". + outputReport() + } else { + if (argv.clean) { + await _p(rimraf)(argv.tempDirectory) + await _p(fs.mkdir)(argv.tempDirectory, { recursive: true }) + } + process.env.NODE_V8_COVERAGE = argv.tempDirectory + + foreground(hideInstrumenterArgs(argv), () => { + outputReport() + }) + } +})() diff --git a/package-lock.json b/package-lock.json index 25f84c0b5..238957230 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2814,6 +2814,7 @@ "version": "0.5.1", "resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, "requires": { "minimist": "0.0.8" } diff --git a/package.json b/package.json index 76dca9b73..389ab7ee9 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "istanbul-lib-coverage": "^2.0.1", "istanbul-lib-report": "^2.0.1", "istanbul-reports": "^2.0.0", - "mkdirp": "^0.5.1", "rimraf": "^2.6.2", "test-exclude": "^5.0.0", "uuid": "^3.3.2", diff --git a/test/integration.js.snap b/test/integration.js.snap index 92ca6c630..c4ad80af9 100644 --- a/test/integration.js.snap +++ b/test/integration.js.snap @@ -8,9 +8,9 @@ second --------------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 92.5 | 69.23 | 0 | 92.5 | | - bin | 83.72 | 57.14 | 100 | 83.72 | | - c8.js | 83.72 | 57.14 | 100 | 83.72 |... 22,40,41,42,43 | +All files | 93.01 | 69.81 | 0 | 93.01 | | + bin | 87.76 | 62.5 | 100 | 87.76 | | + c8.js | 87.76 | 62.5 | 100 | 87.76 | 35,39,46,47,48,49 | lib | 93.71 | 62.96 | 100 | 93.71 | | parse-args.js | 97.47 | 44.44 | 100 | 97.47 | 55,56 | report.js | 90.63 | 72.22 | 100 | 90.63 |... 70,71,85,86,87 |