From 93e1271f031048a1b2976de0bb9829eed170515b Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Tue, 5 Jul 2016 10:23:58 -0700 Subject: [PATCH] chore: add a script to create the shrinkwrap file (and transform) --- scripts/publish/shrinkwrap.js | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 scripts/publish/shrinkwrap.js diff --git a/scripts/publish/shrinkwrap.js b/scripts/publish/shrinkwrap.js new file mode 100755 index 000000000000..156d46748ae7 --- /dev/null +++ b/scripts/publish/shrinkwrap.js @@ -0,0 +1,45 @@ +#!/usr/bin/env node +'use strict'; + +const fs = require('fs'); +const npm = require('npm'); +const path = require('path'); + +const packageJson = require(path.join(__dirname, '../../package.json')); + +npm.load(packageJson, function(err) { + if (err) { + throw err; + } + + npm.shrinkwrap(function(err) { + if (err) { + throw err; + } + + const shrinkwrapPath = path.join(__dirname, '../../npm-shrinkwrap.json'); + const shrinkwrapJson = require(shrinkwrapPath); + + /** + * Recursively remove the `resolved` and `_resolved` fields of packages. + */ + function cleanDependency(dep) { + if (dep.resolved) { + delete dep.resolved; + } + if (dep._resolved) { + delete dep._resolved; + } + + if (dep.dependencies) { + Object.keys(dep.dependencies).forEach(function (key) { + cleanDependency(dep.dependencies[key]) + }); + } + } + cleanDependency(shrinkwrapJson); + + + fs.writeFileSync(shrinkwrapPath, JSON.stringify(shrinkwrapJson, null, 2), 'utf-8'); + }); +});