From 5b434be1b65aafbe7ecd7f9a4fec5910de09637c Mon Sep 17 00:00:00 2001 From: Dror Ben-Gai Date: Wed, 17 May 2017 17:35:19 +0300 Subject: [PATCH] feat: process stdin when no file is given with -i --- package.json | 4 +-- snyk-to-html.js | 77 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 28 deletions(-) mode change 100644 => 100755 snyk-to-html.js diff --git a/package.json b/package.json index 2ad6d1e..030f2e5 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,7 @@ "handlebars": "^4.0.8", "marked": "^0.3.6", "moment": "^2.18.1", - "snyk": "^1.30.1" - }, - "devDependencies": { + "snyk": "^1.30.1", "minimist": "^1.2.0" }, "bin": { diff --git a/snyk-to-html.js b/snyk-to-html.js old mode 100644 new mode 100755 index f97d78b..77325a2 --- a/snyk-to-html.js +++ b/snyk-to-html.js @@ -1,7 +1,7 @@ #!/usr/bin/env node var fs = require("fs"); -var Handlebars = require("Handlebars"); +var Handlebars = require("handlebars"); var marked = require('marked'); var moment = require('moment'); var argv = require('minimist')(process.argv.slice(2)); @@ -14,16 +14,11 @@ if(argv['t']){ //template } if(argv['i']){ //input source source = argv['i']; //grab the next item -} else { - console.error("\x1b[31mNo json specified. Please make sure you've provided a path to your JSON using the `-i` flag."); - process.exit(1); } - if(argv['o']){ //output destination output = argv['o']; //grab the next item } - var htmlTemplate = fs.readFileSync(template, 'utf8'); var hbTemplate = Handlebars.compile(htmlTemplate); var severityMap = {low: 0, medium: 1, high: 2}; @@ -56,26 +51,57 @@ function groupVulns(vulns) { return result; } -fs.readFile(source, 'utf8', (err, data) => { - if (err) throw err; - data = JSON.parse(data); - data.vulnerabilities = groupVulns(data.vulnerabilities); -// console.log(JSON.stringify(data)); - var result = hbTemplate(data); - - if (output) { - fs.writeFile(output, result, function(err) { - if (err) { - return console.log(err); +function generateTemplate(data) { + data.vulnerabilities = groupVulns(data.vulnerabilities); + return hbTemplate(data); +} + +function onDataCallback(data) { + var template = generateTemplate(data); + if (output) { + fs.writeFile(output, template, function(err) { + if (err) { + return console.log(err); + } + console.log('Vulnerability snapshot saved at ' + output); + }); + } else { + console.log(template); + } +} + +function readInputFromFile(source) { + fs.readFile(source, 'utf8', function(err, data) { + if (err) throw err; + onDataCallback(JSON.parse(data)); + }); +} + +function readInputFromStdin() { + var data = ''; + process.stdin.setEncoding('utf8'); + process.stdin.on('readable', function() { + var chunk = process.stdin.read(); + if (chunk !== null) { + data += chunk; } - console.log('Vulnerability snapshot saved at ' + output); }); - } else { - console.log(result); - } - -}); + process.stdin.on('end', function() { + onDataCallback(JSON.parse(data)); + }); +} +function run() { + if (source) { + readInputFromFile(source, onDataCallback); + } else { + readInputFromStdin(); + } +} + +run(); + +// handlebar helpers Handlebars.registerHelper('markdown', function (source) { return marked(source); }); @@ -104,11 +130,12 @@ Handlebars.registerHelper('if_any', function () { // important: not an arrow fn var args = [].slice.call(arguments); var opts = args.pop(); - return args.some(v => !!v) ? opts.fn(this) : opts.inverse(this); + return args.some(function(v) {return !!v;}) ? + opts.fn(this) : + opts.inverse(this); }); Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { - switch (operator) { case '==': { return (v1 == v2) ? options.fn(this) // jshint ignore:line