Skip to content

Commit

Permalink
Merge pull request #38 from cnpm/npm_cache_env
Browse files Browse the repository at this point in the history
fix: add prepublish script after postinstall
  • Loading branch information
fengmk2 committed Mar 3, 2016
2 parents 724b398 + 1fc644e commit 1b8e541
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 35 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ app/

## Benchmarks

### cnpmjs.org install

- [email protected]
- [email protected]
- [email protected]
Expand All @@ -179,6 +181,25 @@ pnpm | 0m13.509s | 0m11.650s | 0m4.443s
npm | 0m28.171s | 0m26.085s | 0m8.219s
npm with cache | 0m20.939s | 0m19.415s | 0m6.302s

### pnpm benchmark

see https://github.com/rstacruz/pnpm#benchmark

```bash
npminstall babel-preset-es2015 browserify chalk debug minimist mkdirp
real 0m8.929s user 0m5.606s sys 0m2.913s
```

```bash
pnpm i babel-preset-es2015 browserify chalk debug minimist mkdirp
real 0m12.998s user 0m8.653s sys 0m3.362s
```

```bash
npm i babel-preset-es2015 browserify chalk debug minimist mkdirp
real 1m4.729s user 0m55.589s sys 0m23.135s
```

## License

[MIT](LICENSE)
12 changes: 12 additions & 0 deletions benchmark/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ cnpmjs.org:
@cd cnpmjs.org && rm -rf node_modules
@cd cnpmjs.org && time npm install --registry=https://registry.npm.taobao.org

pnpm:
# see https://github.com/rstacruz/pnpm#benchmark
@echo "npminstall babel-preset-es2015 browserify chalk debug minimist mkdirp"
@cd cnpmjs.org && rm -rf node_modules
@cd cnpmjs.org && time ../../bin/install.js -c babel-preset-es2015 browserify chalk debug minimist mkdirp
@echo "pnpm i babel-preset-es2015 browserify chalk debug minimist mkdirp"
@cd cnpmjs.org && rm -rf node_modules
@cd cnpmjs.org && time npm_registry=https://registry.npm.taobao.org pnpm i babel-preset-es2015 browserify chalk debug minimist mkdirp
@echo "npm i babel-preset-es2015 browserify chalk debug minimist mkdirp"
@cd cnpmjs.org && rm -rf node_modules
@cd cnpmjs.org && time npm i babel-preset-es2015 browserify chalk debug minimist mkdirp --registry=https://registry.npm.taobao.org

.PHONY: cnpmjs.org
2 changes: 1 addition & 1 deletion bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ if (production) {
}

// if in china, will automatic using chines registry and mirros.
const inChina = argv.china;
const inChina = argv.china || !!process.env.npm_china;

let registry = argv.registry || process.env.npm_registry;
if (inChina) {
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const os = require('os');
const utils = require('./utils');
const postinstall = require('./postinstall');
const preinstall = require('./preinstall');
const prepublish = require('./prepublish');
const install = require('./install');
const dependencies = require('./dependencies');

Expand Down Expand Up @@ -129,6 +130,7 @@ module.exports = function*(options) {
if (installRoot) yield postinstall(rootPkg, options.root, false, options);

yield runPostInstallTasks(options);
if (installRoot) yield prepublish(rootPkg, options.root, options);

if (options.peerDependencies.length > 0) {
yield options.peerDependencies.map(item => validatePeerDependencies(item, options));
Expand Down
29 changes: 29 additions & 0 deletions lib/prepublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.com)
*/

'use strict';

/**
* Module dependencies.
*/

const chalk = require('chalk');
const runScript = require('./utils').runScript;

module.exports = prepublish;

// @see https://docs.npmjs.com/misc/scripts
// Run BEFORE the package is published. (Also run on local npm install without any arguments.)
function* prepublish(pkg, root, options) {
const scripts = pkg.scripts || {};
if (scripts.prepublish) {
options.console.warn(chalk.yellow('[%s@%s] scripts.prepublish: %j'),
pkg.name, pkg.version, scripts.prepublish);
yield runScript(root, scripts.prepublish, options);
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"scripts": {
"test": "mocha -r thunk-mocha -t 120000 test/*.test.js",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -r thunk-mocha -t 120000 test/*.test.js",
"test-local": "local=true mocha -r thunk-mocha -t 120000 test/*.test.js",
"test-local-cov": "local=true istanbul cover node_modules/mocha/bin/_mocha -- -r thunk-mocha -t 120000 test/*.test.js",
"test-local": "npm_china=true local=true mocha -r thunk-mocha -t 120000 test/*.test.js",
"test-local-cov": "npm_china=true local=true istanbul cover node_modules/mocha/bin/_mocha -- -r thunk-mocha -t 120000 test/*.test.js",
"lint": "eslint lib test bin",
"ci": "npm run lint && npm run test-cov",
"autod": "autod -w --prefix '~'"
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/postinstall/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"scripts": {
"preinstall": "node index.js preinstall",
"install": "node index.js install",
"postinstall": "node index.js postinstall"
"postinstall": "node index.js postinstall",
"prepublish": "node index.js prepublish"
},
"dependencies": {
"utility": "1.6.0",
"bignum": "*"
"bignum": "*",
"a-dep-b": "*"
}
}
62 changes: 34 additions & 28 deletions test/git.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
#!/usr/bin/env sh

rm -rf .tmp

git clone https://github.com/webpack/webpack.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test || exit $?
cd ..
rm -rf .tmp

git clone https://github.com/rstacruz/pnpm.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test || exit $?
cd ..
rm -rf .tmp

git clone https://github.com/chaijs/chai.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test
cd ..
rm -rf .tmp

git clone https://github.com/substack/node-browserify.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test
cd ..
rm -rf .tmp

git clone https://github.com/chalk/chalk.git --depth=1 .tmp
cd .tmp
../bin/install.js
Expand Down Expand Up @@ -325,12 +355,7 @@ npm test
cd ..
rm -rf .tmp

git clone https://github.com/webpack/webpack.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test
cd ..
rm -rf .tmp


git clone https://github.com/request/request.git --depth=1 .tmp
cd .tmp
Expand All @@ -346,37 +371,18 @@ npm test
cd ..
rm -rf .tmp

git clone https://github.com/rstacruz/pnpm.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test
cd ..
rm -rf .tmp

git clone https://github.com/eslint/eslint.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test
cd ..
rm -rf .tmp

# git clone https://github.com/ant-design/ant-design.git --depth=1 .tmp
# cd .tmp
# ../bin/install.js
# npm test || exit $?
# cd ..
# rm -rf .tmp

git clone https://github.com/substack/node-browserify.git --depth=1 .tmp
git clone https://github.com/eslint/eslint.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test
cd ..
rm -rf .tmp

git clone https://github.com/chaijs/chai.git --depth=1 .tmp
git clone https://github.com/ant-design/ant-design.git --depth=1 .tmp
cd .tmp
../bin/install.js
npm test
npm test || exit $?
cd ..
rm -rf .tmp
2 changes: 1 addition & 1 deletion test/installLocal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('test/installLocal.test.js', function() {
root: root,
pkgs: [
{ name: null, version: 'file:pkg.tar' },
{ name: null, version: 'file:pkg.tar' },
// { name: null, version: 'file:pkg.tar' },
],
});
pkg = yield readJSON(path.join(root, 'node_modules/pkg/package.json'));
Expand Down
4 changes: 3 additions & 1 deletion test/postinstall.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('test/postinstall.test.js', () => {
beforeEach(cleanup);
afterEach(cleanup);

it('should run preinstall, install and postinstall', function*() {
it('should run preinstall, install, postinstall and prepublish', function*() {
yield npminstall({
root: root,
});
Expand All @@ -46,6 +46,8 @@ describe('test/postinstall.test.js', () => {
assert.equal(fs.readFileSync(path.join(root, 'node_modules', '.install.txt'), 'utf8'), 'success: install');
// postinstall pass
assert.equal(fs.readFileSync(path.join(root, 'node_modules', '.postinstall.txt'), 'utf8'), 'success: postinstall');
// prepublish pass
assert.equal(fs.readFileSync(path.join(root, 'node_modules', '.prepublish.txt'), 'utf8'), 'success: prepublish');
});
});

Expand Down

0 comments on commit 1b8e541

Please sign in to comment.