Skip to content

Instantly share code, notes, and snippets.

@shamasis
Last active April 5, 2016 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamasis/e2969a1b9505d450586c90689061918d to your computer and use it in GitHub Desktop.
Save shamasis/e2969a1b9505d450586c90689061918d to your computer and use it in GitHub Desktop.
Creates a fake package.json for sending to NSP (adds ability to exclude packages using `exclusions: []` in .nsprc)
#!/usr/bin/env node
const
resolve = require('path').resolve,
loadJSON = function (file) {
return JSON.parse(require('fs').readFileSync(file).toString());
},
dependencySources = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies',
'bundledDependencies'];
var p = '',
packageData,
nsprc,
fakePackage = {
name: 'nsp-fake'
};
// extract the current source path
process.argv[2] && (p = process.argv[2].replace(/\/?package\.json$/, ''));
!p && (p = '.');
// extract data from package and nsprc
packageData = loadJSON(resolve(p + '/package.json'));
try {
nsprc = loadJSON(resolve(p + '/.nsprc'));
}
catch (e) {
nsprc = {};
}
!Array.isArray(nsprc.exclusions) && (nsprc.exclusions = []);
// copy all dependencies to fake data and exclude exceptions
dependencySources.forEach(function (src) {
packageData[src] && (fakePackage[src] = {}) && Object.keys(packageData[src]).forEach(function (dep) {
nsprc.exclusions.indexOf(dep) === -1 && (fakePackage[src][dep] = packageData[src][dep]);
});
});
// stdout
process.stdout.write(JSON.stringify(fakePackage, 0, 2) + '\n');
@shamasis
Copy link
Author

shamasis commented Apr 5, 2016

The script simply makes a temporary package.json for use with nsp. Usage in any script is simple:

FAKE_NSP_PATH=".tmp/nsp";

[ -d "$FAKE_NSP_PATH" ] && rm -rf "$FAKE_NSP_PATH";
mkdir -p "$FAKE_NSP_PATH";
./scripts/test/fake_nsp_package.js > "$FAKE_NSP_PATH/package.json";

pushd "$FAKE_NSP_PATH" &>/dev/null
nsp check;
popd &>/dev/null

The .nsprc can contain exceptions and exclusions.

{
  "exclusions": ["my-package-to-ignore"]
}

As a bonus, it also masks privacy by not sending additional details of package.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment