Skip to content
This repository has been archived by the owner on May 8, 2018. It is now read-only.

Commit

Permalink
add support for input preprocessing, closes #187
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Oct 26, 2017
1 parent a1ef4b2 commit 41f967f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 20 deletions.
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -31,6 +31,34 @@ $ nsp check --reporter checkstyle
```
Please note that in case of naming conflicts built-in reporters (as listed above) take precedence. For instance, `nsp-reporter-json` would never be used since nsp ships with a `json` formatter.

## Input Preprocessors

You may also alter a project's `package.json`, `npm-shrinkwrap.json` and/or `package-lock.json` by using an input preprocessor.

The default, built in, preprocessor simply reads these files and returns their JSON parsed content as-is. You can use a third party preprocessor like so: `nsp check --preprocessor example` which, much like third party reporters
would attempt to require the module `nsp-preprocessor-example`. If the given preprocessor is not found, the default will be used.

### Creating a preprocessor

A custom preprocessor should be a module named with the prefix `nsp-preprocessor-`. It must export an object where each property is the name of a command executable by the `nsp` script. The value of each of these properties must
be a function that accepts a single argument `args` which represents the command line arguments passed at execution time, it must return a promise modifying or extending the `args` object.


Example:
```js
module.exports = {
check: function (args) {

// do something to read or generate package.json, npm-shrinkwrap.json and package-lock.json
// the path to the project can be found as `args.path`
// `pkg` must be the JSON parsed contents of package.json
// `shrinkwrap` must be the JSON parsed contents of npm-shrinkwrap.json, if it exists. this may be left out.
// `packagelock` must be the JSON parsed contents of package-lock.json, if it exists. this may also be left out.
return Object.assign(args, { pkg, shrinkwrap, packagelock });
}
};
```

## Exceptions

The Node Security CLI supports adding exceptions. These are advisories that you have evaluated and personally deemed unimportant for your project.
Expand Down
4 changes: 4 additions & 0 deletions bin/nsp
Expand Up @@ -32,6 +32,10 @@ Yargs
default: 'table',
group: 'Output:'
})
.option('preprocessor', {
description: 'project preprocessor',
group: 'Input:'
})
.option('verbose', {
description: 'provide more verbose output',
boolean: true,
Expand Down
22 changes: 3 additions & 19 deletions commands/check.js
Expand Up @@ -59,26 +59,10 @@ exports.builder = {

exports.handler = Command.wrap('check', (args) => {

let pkg;
try {
pkg = JSON.parse(Fs.readFileSync(Path.join(args.path, 'package.json')));
}
catch (err) {
return Promise.reject(new Error(`Unable to load package.json for project: ${Path.basename(args.path)}`));
}
pkg = Package.sanitize(pkg);
let pkg = args.pkg;
const { shrinkwrap, packagelock } = args;

This comment has been minimized.

Copy link
@ralphtheninja

ralphtheninja Jan 17, 2018

@nlf Any chance we could remove this destructuring? It breaks users of node 4, which is still in LTS until the end of april 2018.

This comment has been minimized.

Copy link
@ralphtheninja

ralphtheninja Jan 17, 2018

I can make a PR for it.


let shrinkwrap;
try {
shrinkwrap = JSON.parse(Fs.readFileSync(Path.join(args.path, 'npm-shrinkwrap.json')));
}
catch (err) {}

let packagelock;
try {
packagelock = JSON.parse(Fs.readFileSync(Path.join(args.path, 'package-lock.json')));
}
catch (err) {}
pkg = Package.sanitize(pkg);

if (!pkg.name) {
pkg.name = Path.basename(args.path);
Expand Down
11 changes: 10 additions & 1 deletion lib/command.js
Expand Up @@ -4,6 +4,7 @@ const Fs = require('fs');
const Os = require('os');
const Path = require('path');

const Preprocessor = require('./preprocessor');
const Reporters = require('../reporters');

const internals = {};
Expand Down Expand Up @@ -59,7 +60,15 @@ exports.wrap = function (name, handler) {
Object.assign(args, config);
}

return handler(args).then((result) => {
return Promise.resolve().then(() => {

const preprocessor = Preprocessor.load(args.preprocessor);

return preprocessor.hasOwnProperty(name) ? preprocessor[name](args) : Promise.resolve(args);
}).then((res) => {

return handler(res);
}).then((result) => {

let maxCvss;
if (args.filter ||
Expand Down
42 changes: 42 additions & 0 deletions lib/preprocessor.js
@@ -0,0 +1,42 @@
'use strict';

const Fs = require('fs');
const Path = require('path');

const internals = {};
internals.default = {
check: (args) => {

let pkg;
try {
pkg = JSON.parse(Fs.readFileSync(Path.join(args.path, 'package.json')));
}
catch (err) {
return Promise.reject(new Error(`Unable to load package.json for project: ${Path.basename(args.path)}`));
}

let shrinkwrap;
try {
shrinkwrap = JSON.parse(Fs.readFileSync(Path.join(args.path, 'npm-shrinkwrap.json')));
}
catch (err) {}

let packagelock;
try {
packagelock = JSON.parse(Fs.readFileSync(Path.join(args.path, 'package-lock.json')));
}
catch (err) {}

return Object.assign(args, { pkg, shrinkwrap, packagelock });
}
};

exports.load = (name) => {

try {
return require(`nsp-preprocessor-${name}`);
}
catch (err) {
return internals.default;
}
};

0 comments on commit 41f967f

Please sign in to comment.