better-npm-audit
Advanced tools
Comparing version 1.9.1 to 1.9.2
62
index.js
@@ -25,6 +25,26 @@ #!/usr/bin/env node | ||
/** | ||
* Handle the analyzed result | ||
* Handle the analyzed result and log display | ||
* @param {Array} vulnerabilities List of found vulerabilities | ||
* @param {String} logData Logs | ||
* @param {Boolean} fullLog If it should display all logs | ||
* @param {Integer} maxLength Maxiumum characters allowed to display | ||
*/ | ||
function handleFinish(vulnerabilities) { | ||
function handleFinish(vulnerabilities, logData = '', fullLog = false, maxLength = DEFAULT_MESSSAGE_LIMIT) { | ||
let toDisplay = logData.substring(0, maxLength); | ||
// Display an additional information if we not displaying the full logs | ||
if (toDisplay.length < logData.length) { | ||
toDisplay += '\n\n'; | ||
toDisplay += '...'; | ||
toDisplay += '\n\n'; | ||
toDisplay += RESPONSE_MESSAGE.LOGS_EXCEEDED; | ||
toDisplay += '\n\n'; | ||
} | ||
if (fullLog) { | ||
console.info(logData); | ||
} else { | ||
console.info(toDisplay); | ||
} | ||
// Display the error if found vulnerabilities | ||
@@ -42,26 +62,2 @@ if (vulnerabilities.length > 0) { | ||
/** | ||
* Handle the log display on user's console | ||
* @param {String} data String logs | ||
* @param {Boolean} fullLog If it should display all logs | ||
* @param {Integer} maxLength Maxiumum characters allowed to display | ||
*/ | ||
function handleLogDisplay(data, fullLog, maxLength = DEFAULT_MESSSAGE_LIMIT) { | ||
if (fullLog) { | ||
consoleUtil.info(data); | ||
} else { | ||
const toDisplay = data.substring(0, maxLength); | ||
consoleUtil.info(toDisplay); | ||
// Display additional info if it is not the full message | ||
if (toDisplay.length < data.length) { | ||
consoleUtil.info(''); | ||
consoleUtil.info('...'); | ||
consoleUtil.info(''); | ||
consoleUtil.info(RESPONSE_MESSAGE.LOGS_EXCEEDED); | ||
consoleUtil.info(''); | ||
} | ||
} | ||
} | ||
/** | ||
* Re-runs the audit in human readable form | ||
@@ -76,6 +72,10 @@ * @param {String} auditCommand The NPM audit command to use (with flags) | ||
audit.stdout.on('data', data => handleLogDisplay(data, fullLog)); | ||
// Set a temporary string | ||
// Note: collect all buffers' data before displaying it later to avoid unintentional line breaking in the report display | ||
let bufferData = ''; | ||
audit.stdout.on('data', data => bufferData += data); | ||
// Once the stdout has completed | ||
audit.stderr.on('close', () => handleFinish(vulnerabilities)); | ||
audit.stderr.on('close', () => handleFinish(vulnerabilities, bufferData, fullLog)); | ||
@@ -138,11 +138,14 @@ // stderr | ||
if (Array.isArray(exceptionIds) && exceptionIds.length) { | ||
consoleUtil.info('Exception vulnerabilities ID(s): ', exceptionIds); | ||
consoleUtil.info(`Exception vulnerabilities ID(s): ${exceptionIds}`); | ||
} | ||
if (options && options.level) { | ||
console.info(`[level: ${options.level}]`); | ||
auditLevel = mapLevelToNumber(options.level); | ||
} | ||
if (options && options.production) { | ||
console.info('[production mode enabled]'); | ||
auditCommand += ' --production'; | ||
} | ||
if (options && options.full) { | ||
console.info('[full log mode enabled]'); | ||
fullLog = true; | ||
@@ -168,3 +171,2 @@ } | ||
module.exports = { | ||
handleLogDisplay, | ||
handleFinish, | ||
@@ -171,0 +173,0 @@ handleUserInput, |
{ | ||
"name": "better-npm-audit", | ||
"version": "1.9.1", | ||
"version": "1.9.2", | ||
"author": "Jee Mok <jee.ict@hotmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "Made to allow skipping certain vulnerabilities, and any extra handling that are not supported by the default npm audit in the future.", |
const sinon = require('sinon'); | ||
const chai = require('chai'); | ||
const { expect } = chai; | ||
const V6_LOG_REPORT = require('./__mocks__/v6-log-data'); | ||
const V6_JSON_BUFFER = require('./__mocks__/v6-json-buffer.json'); | ||
@@ -10,5 +11,5 @@ const V6_JSON_BUFFER_EMPTY = require('./__mocks__/v6-json-buffer-empty.json'); | ||
const { isWholeNumber, mapLevelToNumber, getVulnerabilities, isJsonString, filterValidException } = require('../utils/common'); | ||
const { handleLogDisplay, handleFinish, handleUserInput, BASE_COMMAND, SUCCESS_MESSAGE, LOGS_EXCEEDED_MESSAGE } = require('../index'); | ||
const { handleFinish, handleUserInput, BASE_COMMAND, SUCCESS_MESSAGE, LOGS_EXCEEDED_MESSAGE } = require('../index'); | ||
const { FG_WHITE, BG_BLACK, RESET_COLOR } = consoleUtil; | ||
const { FG_WHITE, RESET_COLOR } = consoleUtil; | ||
@@ -24,3 +25,4 @@ describe('console utils', () => { | ||
expect(stub.called).to.equal(true); | ||
expect(stub.calledWith(`${FG_WHITE}${BG_BLACK}${message}${RESET_COLOR}`)).to.equal(true); | ||
expect(stub.calledWith(`${FG_WHITE}${message}${RESET_COLOR}`)).to.equal(true); | ||
stub.restore(); | ||
}); | ||
@@ -37,2 +39,3 @@ | ||
expect(stub.calledWith(`${FG_WHITE}${message}${RESET_COLOR}`)).to.equal(true); | ||
stub.restore(); | ||
}); | ||
@@ -167,2 +170,3 @@ }); | ||
const stub = sinon.stub(); | ||
const consoleStub = sinon.stub(console, 'info'); | ||
const options = { | ||
@@ -180,2 +184,3 @@ level: 'info', | ||
expect(stub.calledWith(auditCommand, 0, fullLog, exceptionIds)).to.equal(true); | ||
expect(consoleStub.calledWith('[level: info]')).to.equal(true); | ||
@@ -186,2 +191,3 @@ // low | ||
expect(stub.calledWith(auditCommand, 1, fullLog, exceptionIds)).to.equal(true); | ||
expect(consoleStub.calledWith('[level: low]')).to.equal(true); | ||
@@ -192,2 +198,3 @@ // moderate | ||
expect(stub.calledWith(auditCommand, 2, fullLog, exceptionIds)).to.equal(true); | ||
expect(consoleStub.calledWith('[level: moderate]')).to.equal(true); | ||
@@ -198,2 +205,3 @@ // high | ||
expect(stub.calledWith(auditCommand, 3, fullLog, exceptionIds)).to.equal(true); | ||
expect(consoleStub.calledWith('[level: high]')).to.equal(true); | ||
@@ -204,2 +212,5 @@ // critical | ||
expect(stub.calledWith(auditCommand, 4, fullLog, exceptionIds)).to.equal(true); | ||
expect(consoleStub.calledWith('[level: critical]')).to.equal(true); | ||
consoleStub.restore(); | ||
}); | ||
@@ -209,2 +220,3 @@ | ||
const stub = sinon.stub(); | ||
const consoleStub = sinon.stub(console, 'info'); | ||
const options = { | ||
@@ -223,2 +235,5 @@ production: true, | ||
expect(stub.calledWith(auditCommand, auditLevel, fullLog, exceptionIds)).to.equal(true); | ||
expect(consoleStub.calledWith('[production mode enabled]')).to.equal(true); | ||
consoleStub.restore(); | ||
}); | ||
@@ -228,2 +243,3 @@ | ||
const stub = sinon.stub(); | ||
const consoleStub = sinon.stub(console, 'info'); | ||
const options = { | ||
@@ -242,2 +258,5 @@ full: true, | ||
expect(stub.calledWith(auditCommand, auditLevel, fullLog, exceptionIds)).to.equal(true); | ||
expect(consoleStub.calledWith('[full log mode enabled]')).to.equal(true); | ||
consoleStub.restore(); | ||
}); | ||
@@ -292,44 +311,60 @@ | ||
it('should be able to handle normal log display correctly', () => { | ||
const stub = sinon.stub(consoleUtil, 'info'); | ||
const data = '123456789'; | ||
const stub = sinon.stub(console, 'info'); | ||
const smallLog = '123456789'; | ||
const fullLog = true; | ||
const maxLength = 50; | ||
const vulnerabilities = []; | ||
expect(stub.called).to.equal(false); | ||
handleLogDisplay(data, fullLog, maxLength); | ||
handleFinish(vulnerabilities, smallLog, fullLog, maxLength); | ||
expect(stub.called).to.equal(true); | ||
expect(stub.calledWith('123456789')).to.equal(true); | ||
expect(stub.calledWith(smallLog)).to.equal(true); | ||
stub.restore(); | ||
}); | ||
it('should be able to handle overlength log display properly', () => { | ||
const stub = sinon.stub(consoleUtil, 'info'); | ||
const data = '123456789'; | ||
it('should display overlength log properly', () => { | ||
const stub = sinon.stub(console, 'info'); | ||
const fullLog = true; | ||
const maxLength = 500; | ||
const vulnerabilities = []; | ||
expect(stub.called).to.equal(false); | ||
handleFinish(vulnerabilities, V6_LOG_REPORT, fullLog, maxLength); | ||
expect(stub.called).to.equal(true); | ||
// Full log | ||
expect(stub.calledWith(V6_LOG_REPORT)).to.equal(true); | ||
stub.restore(); | ||
}); | ||
it('should display an additional message on overlength log', () => { | ||
const stub = sinon.stub(console, 'info'); | ||
const fullLog = false; | ||
const maxLength = 5; | ||
const maxLength = 500; | ||
const vulnerabilities = []; | ||
let expectedDisplay = V6_LOG_REPORT.substring(0, maxLength); | ||
expectedDisplay += '\n\n'; | ||
expectedDisplay += '...'; | ||
expectedDisplay += '\n\n'; | ||
expectedDisplay += LOGS_EXCEEDED_MESSAGE; | ||
expectedDisplay += '\n\n'; | ||
expect(stub.called).to.equal(false); | ||
handleLogDisplay(data, fullLog, maxLength); | ||
handleFinish(vulnerabilities, V6_LOG_REPORT, fullLog, maxLength); | ||
expect(stub.called).to.equal(true); | ||
expect(stub.calledWith('12345')).to.equal(true); | ||
expect(stub.calledWith('')).to.equal(true); | ||
expect(stub.calledWith('...')).to.equal(true); | ||
expect(stub.calledWith(LOGS_EXCEEDED_MESSAGE)).to.equal(true); | ||
expect(stub.calledWith(expectedDisplay)).to.equal(true); | ||
stub.restore(); | ||
}); | ||
it('should be able to handle log display properly', () => { | ||
const stub = sinon.stub(consoleUtil, 'info'); | ||
it('should be able to handle log display within maximum length properly', () => { | ||
const stub = sinon.stub(console, 'info'); | ||
const data = '123456789'; | ||
const fullLog = false; | ||
const maxLength = 9; | ||
const vulnerabilities = []; | ||
expect(stub.called).to.equal(false); | ||
handleLogDisplay(data, fullLog, maxLength); | ||
handleFinish(vulnerabilities, data, fullLog, maxLength); | ||
expect(stub.called).to.equal(true); | ||
expect(stub.calledWith('123456789')).to.equal(true); | ||
// This time when it is exactly the display limit, it should not show the exceeded message | ||
expect(stub.calledWith('')).to.equal(false); | ||
expect(stub.calledWith('...')).to.equal(false); | ||
expect(stub.calledWith(LOGS_EXCEEDED_MESSAGE)).to.equal(false); | ||
stub.restore(); | ||
@@ -336,0 +371,0 @@ }); |
const RESET_COLOR = '\x1b[0m'; | ||
const FG_WHITE = '\x1b[37m'; | ||
const BG_BLACK = '\x1b[40m'; | ||
@@ -10,3 +9,3 @@ /** | ||
function error(string) { | ||
console.error(`${FG_WHITE}${BG_BLACK}${string}${RESET_COLOR}`); | ||
console.error(`${FG_WHITE}${string}${RESET_COLOR}`); | ||
return true; | ||
@@ -29,3 +28,2 @@ } | ||
FG_WHITE, | ||
BG_BLACK, | ||
}; |
241923
17
3080