chore(package): Make clean-shrinkwrap remove optional dependencies (#1236)

Previously dependencies weren't actually removed from `node_modules`,
this runs `npm rm` on the optional dependencies, effectively excluding
them, and their dependencies from the shrinkwrap file.

Also the script has been hooked to the `preshrinkwrap` hook,
to remove the need of having to run it manually.

Change-Type: patch
This commit is contained in:
Jonas Hermsmeier 2017-03-31 20:35:50 +02:00 committed by Juan Cruz Viotti
parent 8fff29224d
commit d5ec71c5da
3 changed files with 23 additions and 13 deletions

View File

@ -44,10 +44,6 @@ Use the following steps to ensure everything goes flawlessly:
- Install the new version of the dependency. For example: `npm install --save
<package>@<version>`. This will update the `npm-shrinkwrap.json` file.
- Run `npm run clean-shrinkwrap`. This is a small script that ensures that
operating system specific dependencies that could get included in the
previous step are removed from `npm-shrinkwrap.json`.
- Commit *both* `package.json` and `npm-shrinkwrap.json`.
Testing

View File

@ -20,7 +20,7 @@
"lint": "npm run jslint && npm run sasslint && npm run codespell && npm run htmllint",
"changelog": "versionist",
"start": "electron lib/start.js",
"clean-shrinkwrap": "node ./scripts/clean-shrinkwrap.js"
"preshrinkwrap": "node ./scripts/clean-shrinkwrap.js"
},
"author": "Juan Cruz Viotti <juan@resin.io>",
"license": "Apache-2.0",
@ -115,7 +115,6 @@
"eslint-plugin-lodash": "^2.3.5",
"file-exists": "^1.0.0",
"html-angular-validate": "^0.1.9",
"jsonfile": "^2.3.1",
"mochainon": "^1.0.0",
"node-sass": "^3.8.0",
"sass-lint": "^1.10.2",

View File

@ -17,13 +17,28 @@
const _ = require('lodash');
const path = require('path');
const jsonfile = require('jsonfile');
const packageJSON = require('../package.json');
const spawn = require('child_process').spawn;
const shrinkwrapIgnore = _.union(packageJSON.shrinkwrapIgnore, _.keys(packageJSON.optionalDependencies));
const SHRINKWRAP_PATH = path.join(__dirname, '..', 'npm-shrinkwrap.json');
const shrinkwrapContents = jsonfile.readFileSync(SHRINKWRAP_PATH);
shrinkwrapContents.dependencies = _.omit(shrinkwrapContents.dependencies, shrinkwrapIgnore);
jsonfile.writeFileSync(SHRINKWRAP_PATH, shrinkwrapContents, {
spaces: 2
});
console.log('Removing:', shrinkwrapIgnore.join(', '));
/**
* Run an npm command
* @param {Array} command - list of arguments
* @returns {ChildProcess}
*/
const npm = (command) => {
return spawn('npm', command, {
cwd: path.join(__dirname, '..'),
env: process.env,
stdio: [ process.stdin, process.stdout, process.stderr ]
});
};
npm([ 'rm', '--ignore-scripts' ].concat(shrinkwrapIgnore))
.once('close', () => {
npm([ 'prune' ]).once('close', () => {
console.log('Done.');
});
});