From c6aa3996f735a880c9b8b8c97dfcf856cc432b89 Mon Sep 17 00:00:00 2001 From: Noah Prail Date: Thu, 22 Oct 2020 15:13:29 -0400 Subject: [PATCH] feat(cli): Add option to automatically open the report when done --- index.js | 14 +++++++++++--- package-lock.json | 22 ++++++++++++++++++++++ package.json | 1 + 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e8bbae3..5097479 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,8 @@ const program = require('commander') const updateNotifier = require('update-notifier') const fs = require('fs-extra') +const open = require('open') +const path = require('path') const reporter = require('./lib/reporter') const pkg = require('./package.json') @@ -15,6 +17,7 @@ program .version(pkg.version) .option('-o, --output [output]', 'output file') .option('-i, --input [input]', 'input file') + .option('-O, --open', 'open report in default browser automatically') .option( '-c, --theme [theme name]', 'template theme `dark` or `light` (defaults to `light`)' @@ -32,7 +35,7 @@ program return process.exit(1) } - await genReport(data, cmd.output, cmd.template, cmd.theme) + await genReport(data, cmd.output, cmd.template, cmd.theme, cmd.open) } catch (err) { console.error('Failed to parse NPM Audit JSON!') return process.exit(1) @@ -43,7 +46,8 @@ const genReport = async ( data, output = 'npm-audit.html', template, - theme = 'light' + theme = 'light', + openBrowser = false ) => { try { if (!data) { @@ -56,7 +60,11 @@ const genReport = async ( await reporter(data, templateFile, output, theme) console.log(`Vulnerability snapshot saved at ${output}`) - process.exit(0) + + if (openBrowser) { + console.log('Opening report in default browser...') + await open(path.resolve(output)) + } } catch (err) { console.log('An error occurred!') console.log(err) diff --git a/package-lock.json b/package-lock.json index 17acf96..c451aec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5323,6 +5323,11 @@ } } }, + "is-docker": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.1.1.tgz", + "integrity": "sha512-ZOoqiXfEwtGknTiuDEy8pN2CfE3TxMHprvNer1mXiqwkOT77Rw3YVrUQ52EqAOU3QAWDQ+bQdx7HJzrv7LS2Hw==" + }, "is-extendable": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/is-extendable/-/is-extendable-0.1.1.tgz", @@ -5494,6 +5499,14 @@ "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==", "dev": true }, + "is-wsl": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-2.2.0.tgz", + "integrity": "sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==", + "requires": { + "is-docker": "^2.0.0" + } + }, "is-yarn-global": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/is-yarn-global/-/is-yarn-global-0.3.0.tgz", @@ -10485,6 +10498,15 @@ "mimic-fn": "^1.0.0" } }, + "open": { + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/open/-/open-7.3.0.tgz", + "integrity": "sha512-mgLwQIx2F/ye9SmbrUkurZCnkoXyXyu9EbHtJZrICjVAJfyMArdHp3KkixGdZx1ZHFPNIwl0DDM1dFFqXbTLZw==", + "requires": { + "is-docker": "^2.0.0", + "is-wsl": "^2.1.1" + } + }, "opencollective-postinstall": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz", diff --git a/package.json b/package.json index 9056085..245daea 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "marked": "^1.1.1", "moment": "^2.27.0", "numeral": "^2.0.6", + "open": "^7.3.0", "terminal-link": "^2.1.1", "update-notifier": "^4.1.0" },