Skip to content

Instantly share code, notes, and snippets.

@tlrobinson
Last active March 22, 2016 04:48
Show Gist options
  • Save tlrobinson/d7ab970a10a5c3c81cf5 to your computer and use it in GitHub Desktop.
Save tlrobinson/d7ab970a10a5c3c81cf5 to your computer and use it in GitHub Desktop.
Bring sanity to npm 2's shrinkwrap. Makes "npm shrinkwrap" deterministic. Similar but simpler than Uber's npm-shrinkwrap. From https://github.com/metabase/metabase/blob/master/bin/clean-shrinkwrap
#!/usr/bin/env node
var fs = require('fs');
function clean(module) {
// remove module.from
delete module.from;
// remove module.resolved unless it points to a git repo
if (module.resolved && module.resolved.indexOf("https://registry.npmjs.org/") === 0) {
delete module.resolved;
}
// sort module.dependencies keys, and recursively clean them
if (module.dependencies) {
module.dependencies = Object.keys(module.dependencies).sort().reduce(function(deps, name) {
deps[name] = clean(module.dependencies[name]);
return deps;
}, {});
}
return module;
}
var path = require('path').join(__dirname, '..', 'npm-shrinkwrap.json');
fs.writeFileSync(path, JSON.stringify(clean(JSON.parse(fs.readFileSync(path))), null, 2) + "\n");
{
...
"scripts": {
"shrinkwrap": "npm prune && npm shrinkwrap --dev && ./bin/clean-shrinkwrap.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment