Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ignore-scripts #59

Merged
merged 3 commits into from
Mar 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down
9 changes: 8 additions & 1 deletion lib/local_install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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...'));
}
Expand Down Expand Up @@ -293,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;
Expand Down
8 changes: 5 additions & 3 deletions lib/preinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
6 changes: 6 additions & 0 deletions test/fixtures/ignore-scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ignore-scripts",
"dependencies": {
"kpg": "./pkg"
}
}
1 change: 1 addition & 0 deletions test/fixtures/ignore-scripts/pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello')
14 changes: 14 additions & 0 deletions test/fixtures/ignore-scripts/pkg/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
42 changes: 42 additions & 0 deletions test/ignoreScripts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <[email protected]>
*/

'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' ]);
});
});