From d5ec71c5da0d0ea071b0cb0a03eb2d2b1be8a6c0 Mon Sep 17 00:00:00 2001 From: Jonas Hermsmeier Date: Fri, 31 Mar 2017 20:35:50 +0200 Subject: [PATCH] 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 --- docs/CONTRIBUTING.md | 4 ---- package.json | 3 +-- scripts/clean-shrinkwrap.js | 29 ++++++++++++++++++++++------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index f9ec7203..90194844 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -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 @`. 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 diff --git a/package.json b/package.json index c840c310..6b78f9a8 100644 --- a/package.json +++ b/package.json @@ -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 ", "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", diff --git a/scripts/clean-shrinkwrap.js b/scripts/clean-shrinkwrap.js index e47de67a..bbaccf87 100644 --- a/scripts/clean-shrinkwrap.js +++ b/scripts/clean-shrinkwrap.js @@ -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.'); + }); + });