Skip to content

Commit

Permalink
feat(cli): Add input CLI option!
Browse files Browse the repository at this point in the history
ref #25
  • Loading branch information
nprail committed Sep 18, 2019
1 parent 3759b8f commit 5622854
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,62 @@

const program = require('commander')
const updateNotifier = require('update-notifier')
const fs = require('fs-extra')

const reporter = require('./lib/reporter')
const pkg = require('./package.json')

updateNotifier({ pkg }).notify()

let stdin = ''

program
.version(pkg.version)
.option('-o, --output [output]', 'output file')
.option('-i, --input [input]', 'input file')
.option('-t, --template [handlebars file]', 'handlebars template file')
.action(async (cmd, env) => {
try {
let data
if (cmd.input) {
data = await fs.readJson(cmd.input)
} else if (stdin) {
data = JSON.parse(stdin)
} else {
console.log('No input')
return process.exit(1)
}

const genReport = (stdin, output = 'npm-audit.html', template) => {
if (!stdin) {
console.log('No JSON')
return process.exit(1)
}
await genReport(data, cmd.output, cmd.template)
} catch (err) {
console.error('Failed to parse NPM Audit JSON!')
return process.exit(1)
}
})

let json
const genReport = async (data, output = 'npm-audit.html', template) => {
try {
json = JSON.parse(stdin)
if (!data) {
console.log('No JSON')
return process.exit(1)
}

const templateFile = template || `${__dirname}/templates/template.hbs`

await reporter(data, templateFile, output)

console.log(`Vulnerability snapshot saved at ${output}`)
process.exit(0)
} catch (err) {
console.error('Failed to parse NPM Audit JSON!')
return process.exit(1)
console.log('An error occurred!')
console.log(err)
process.exit(1)
}

const templateFile = template || `${__dirname}/templates/template.hbs`

reporter(json, templateFile, output)
.then(() => {
console.log(`Vulnerability snapshot saved at ${output}`)
process.exit(0)
})
.catch(err => {
console.log('An error occurred!')
console.log(err)
process.exit(1)
})
}

if (process.stdin.isTTY) {
program.parse(process.argv)
} else {
let stdin = ''
process.stdin.on('readable', function () {
const chunk = this.read()
if (chunk !== null) {
Expand All @@ -53,6 +66,5 @@ if (process.stdin.isTTY) {
})
process.stdin.on('end', function () {
program.parse(process.argv)
genReport(stdin, program.output, program.template)
})
}

0 comments on commit 5622854

Please sign in to comment.