From 33fab09b00ec8ec33df1a63130f2d5049ce63e6d Mon Sep 17 00:00:00 2001 From: dead_horse Date: Mon, 28 Mar 2016 16:01:09 +0800 Subject: [PATCH 1/3] feat: support ignore-scripts --- bin/install.js | 13 ++++++ lib/local_install.js | 6 +++ lib/preinstall.js | 8 ++-- test/fixtures/ignore-scripts/package.json | 6 +++ test/fixtures/ignore-scripts/pkg/index.js | 1 + test/fixtures/ignore-scripts/pkg/package.json | 14 +++++++ test/ignoreScripts.test.js | 42 +++++++++++++++++++ 7 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/ignore-scripts/package.json create mode 100644 test/fixtures/ignore-scripts/pkg/index.js create mode 100644 test/fixtures/ignore-scripts/pkg/package.json create mode 100644 test/ignoreScripts.test.js diff --git a/bin/install.js b/bin/install.js index 3ca4c88e..a544b90d 100755 --- a/bin/install.js +++ b/bin/install.js @@ -45,6 +45,7 @@ const argv = parseArgs(process.argv.slice(2), { // Saved dependencies will be configured with an exact version rather than using npm's default semver range operator. 'save-exact', 'china', + 'ignore-scripts', ], alias: { // npm install [-S|--save|-D|--save-dev|-O|--save-optional] [-E|--save-exact] @@ -91,6 +92,7 @@ Options: -g, --global: install devDependencies to global directory which specified in '$npm config get prefix' -r, --registry: specify custom registry -c, --china: specify in china, will automatically using chinses npm registry and other binary's mirrors + --ignore-scripts: ignore all preinstall / install and postinstall scripts during the installation ` ); process.exit(0); @@ -166,6 +168,7 @@ co(function*() { binaryMirrors, }; config.strictSSL = getStrictSSL(); + config.ignoreScripts = argv['ignore-scripts'] || getIgnoreScripts(); // -g install to npm's global prefix if (argv.global) { const npmPrefix = getPrefix(); @@ -229,6 +232,16 @@ function getStrictSSL() { } } +function getIgnoreScripts() { + try { + const ignoreScripts = execSync('npm config get ignore-scripts').toString().trim(); + return ignoreScripts === 'true'; + } catch (err) { + console.error(`exec npm config get ignore-scripts ERROR: ${err.message}`); + return false; + } +} + function* updateDependencies(root, pkgs, propName, saveExact) { const savePrefix = saveExact ? '' : getVersionSavePrefix(); const pkgFile = path.join(root, 'package.json'); diff --git a/lib/local_install.js b/lib/local_install.js index d1ce6ee0..ca84c860 100644 --- a/lib/local_install.js +++ b/lib/local_install.js @@ -47,6 +47,7 @@ const dependencies = require('./dependencies'); * - {String} [cacheDir] - tarball cache store dir, default is `$HOME/.npminstall_tarball`. * if `production` mode enable, `cacheDir` will be disable. * - {Object} [binaryMirrors] - binary mirror config, default is `{}` + * - {Boolean} [ignoreScripts] - ignore pre / post install scripts, default is `false` */ module.exports = function*(options) { options.events = new EventEmitter(); @@ -266,6 +267,11 @@ function* linkRootPackage(name, nodeModulesDir, storeDir) { } function* runPostInstallTasks(options) { + if (options.postInstallTasks.length && options.ignoreScripts) { + console.log(chalk.yellow('ignore all post install scripts')); + return; + } + if (options.postInstallTasks.length) { options.console.log(chalk.yellow('excute post install scripts...')); } diff --git a/lib/preinstall.js b/lib/preinstall.js index 107ea904..8f6d83c0 100644 --- a/lib/preinstall.js +++ b/lib/preinstall.js @@ -21,8 +21,10 @@ module.exports = preinstall; function* preinstall(pkg, root, options) { const scripts = pkg.scripts || {}; if (scripts.preinstall) { - options.console.warn(chalk.yellow('[%s@%s] scripts.preinstall: %j'), - pkg.name, pkg.version, scripts.preinstall); - yield runScript(root, scripts.preinstall, options); + options.console.warn(chalk.yellow('[%s@%s]%s scripts.preinstall: %j'), + pkg.name, pkg.version, options.ignoreScripts ? ' ignore' : '', scripts.preinstall); + if (!options.ignoreScripts) { + yield runScript(root, scripts.preinstall, options); + } } } diff --git a/test/fixtures/ignore-scripts/package.json b/test/fixtures/ignore-scripts/package.json new file mode 100644 index 00000000..09b7f5fc --- /dev/null +++ b/test/fixtures/ignore-scripts/package.json @@ -0,0 +1,6 @@ +{ + "name": "ignore-scripts", + "dependencies": { + "kpg": "./pkg" + } +} diff --git a/test/fixtures/ignore-scripts/pkg/index.js b/test/fixtures/ignore-scripts/pkg/index.js new file mode 100644 index 00000000..371fdfb1 --- /dev/null +++ b/test/fixtures/ignore-scripts/pkg/index.js @@ -0,0 +1 @@ +console.log('hello') diff --git a/test/fixtures/ignore-scripts/pkg/package.json b/test/fixtures/ignore-scripts/pkg/package.json new file mode 100644 index 00000000..5a6b87e0 --- /dev/null +++ b/test/fixtures/ignore-scripts/pkg/package.json @@ -0,0 +1,14 @@ +{ + "name": "pkg", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "preinstall": "echo \"preinstall\" > ./preinstall", + "install": "echo \"install\" > ./install", + "postinstall": "echo \"postinstall\" > ./postinstall" + }, + "author": "", + "license": "ISC" +} diff --git a/test/ignoreScripts.test.js b/test/ignoreScripts.test.js new file mode 100644 index 00000000..726311a5 --- /dev/null +++ b/test/ignoreScripts.test.js @@ -0,0 +1,42 @@ +/** + * Copyright(c) cnpm and other contributors. + * MIT Licensed + * + * Authors: + * dead_horse + */ + +'use strict'; + +/** + * Module dependencies. + */ + +const assert = require('assert'); +const path = require('path'); +const rimraf = require('rimraf'); +const fs = require('mz/fs'); +const npminstall = require('./npminstall'); + +describe('test/ignoreScripts.test.js', function() { + const root = path.join(__dirname, 'fixtures', 'ignore-scripts'); + + function cleanup() { + rimraf.sync(path.join(root, 'node_modules')); + } + + beforeEach(cleanup); + afterEach(cleanup); + + it('should ignore scripts', function*() { + yield npminstall({ + root: root, + ignoreScripts: true, + }); + + const dirs = yield fs.readdir(path.join(root, 'node_modules')); + assert.deepEqual(dirs, [ '.npminstall', 'pkg' ]); + const files = yield fs.readdir(path.join(root, 'node_modules/pkg')); + assert.deepEqual(files, [ '.npminstall.done', 'index.js', 'package.json' ]); + }); +}); From 3a0521b9f2ab3022b3dd3e85da3f95f8031db5c3 Mon Sep 17 00:00:00 2001 From: dead_horse Date: Mon, 28 Mar 2016 16:03:58 +0800 Subject: [PATCH 2/3] chore: add ignore-scripts in readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 98c13d1a..797a61f0 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Options: -g, --global: install devDependencies to global directory which specified in '$ npm config get prefix' -r, --registry: specify custom registry -c, --china: specify in china, will automatically using chinses npm registry and other binary's mirrors + --ignore-scripts: ignore all preinstall / install and postinstall scripts during the installation ``` ## Use as Lib @@ -88,6 +89,7 @@ co(function*() { // registry: 'https://registry.npmjs.org', // debug: false, // storeDir: root + '.npminstall', + // ignoreScripts: true, // ignore pre/post install scripts, default is `false` }); }).catch(function(err) { console.error(err.stack); @@ -117,6 +119,7 @@ co(function*() { - [x] deprecate message - [x] `--production` mode - [x] `save`, `save-dev`, `save-optional` +- [x] support `ignore-scripts` ## Different with NPM From a7eca536876d70556f2b5aa04dec9c1834c73ca1 Mon Sep 17 00:00:00 2001 From: dead_horse Date: Mon, 28 Mar 2016 16:16:19 +0800 Subject: [PATCH 3/3] chore: fix eslint --- lib/local_install.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/local_install.js b/lib/local_install.js index ca84c860..9152b804 100644 --- a/lib/local_install.js +++ b/lib/local_install.js @@ -299,7 +299,8 @@ function* runPostInstallTasks(options) { } } catch (err) { if (task.optional) { - return console.warn(chalk.red('[%s@%s] optional error: %s'), err.stack); + console.warn(chalk.red('[%s@%s] optional error: %s'), err.stack); + return; } err.message = `post install error, please remove node_modules before retry!\n${err.message}`; throw err;