Skip to content

Commit

Permalink
feat: allow disable dedupe mode on node project (#279)
Browse files Browse the repository at this point in the history
* test: add node 11
  • Loading branch information
fengmk2 authored Oct 24, 2018
1 parent e15b7e3 commit a36dd1c
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 22 deletions.
7 changes: 7 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const argv = parseArgs(orignalArgv, {
'cache-strict',
'fix-bug-versions',
'prune',
// disable dedupe mode https://docs.npmjs.com/cli/dedupe, back to npm@2 mode
// please don't use on frontend project
'disable-dedupe',
],
alias: {
// npm install [-S|--save|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-d|--detail]
Expand Down Expand Up @@ -220,6 +223,7 @@ co(function* () {
flatten,
proxy,
prune,
disableDedupe: argv['disable-dedupe'],
};
config.strictSSL = getStrictSSL();
config.ignoreScripts = argv['ignore-scripts'] || getIgnoreScripts();
Expand Down Expand Up @@ -294,6 +298,9 @@ co(function* () {
if (pkg.config.npminstall.prune === true) {
config.prune = true;
}
if (pkg.config.npminstall.disableDedupe === true) {
config.disableDedupe = true;
}
}
}
yield installLocal(config);
Expand Down
11 changes: 9 additions & 2 deletions lib/local_install.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const bin = require('./bin');
* - {Array} [forbiddenLicenses] - forbit install packages which used these licenses
* - {Boolean} [trace] - show memory and cpu usages traces of installation
* - {Boolean} [flatten] - flatten dependencies by matching ancestors' dependencies
* - {Boolean} [disableDedupe] - disable dedupe mode, will back to npm@2 node_modules tree
*/
module.exports = function* (options) {
options = formatInstallOptions(options);
Expand Down Expand Up @@ -139,8 +140,14 @@ function* _install(options) {
options.downloadFinished = Date.now();
options.spinner && options.spinner.succeed(`Installed ${tasks.length} packages`);

// link every packages' latest version to target directory
yield linkAllLatestVersion(rootPkgsMap, options);
if (!options.disableDedupe) {
// dedupe mode https://docs.npmjs.com/cli/dedupe
// link every packages' latest version to target directory
yield linkAllLatestVersion(rootPkgsMap, options);
} else {
options.spinner && options.spinner.succeed('disable dedupe mode');
}

// run postinstall script if exist
if (options.installRoot) yield postinstall(rootPkg, options.root, false, displayName, options);
// run dependencies' postinstall scripts
Expand Down
45 changes: 45 additions & 0 deletions test/disable-dedupe.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const assert = require('assert');
const fs = require('fs');
const rimraf = require('rimraf');
const path = require('path');
const coffee = require('coffee');
const npminstall = path.join(__dirname, '..', 'bin', 'install.js');

describe('test/disable-dedupe.test.js', () => {
const root = path.join(__dirname, 'fixtures', 'disable-dedupe');
const root2 = path.join(__dirname, 'fixtures', 'disable-dedupe-config');

function cleanup() {
rimraf.sync(path.join(root, 'node_modules'));
rimraf.sync(path.join(root2, 'node_modules'));
}

beforeEach(cleanup);
afterEach(cleanup);

it('should install --disable-dedupe work', function* () {
yield coffee.fork(npminstall, [ '--disable-dedupe' ], { cwd: root })
.debug()
.expect('code', 0)
.expect('stderr', /disable dedupe mode/)
.end();
const names = fs.readdirSync(path.join(root, 'node_modules'))
.filter(n => !/^[\.\_]/.test(n));
assert(names.length === 1);
assert(names[0] === 'koa');
});

it('should install config.npminstall.disableDedupe=true work', function* () {
yield coffee.fork(npminstall, { cwd: root2 })
.debug()
.expect('code', 0)
.expect('stderr', /disable dedupe mode/)
.end();
const names = fs.readdirSync(path.join(root2, 'node_modules'))
.filter(n => !/^[\.\_]/.test(n));
assert(names.length === 1);
assert(names[0] === 'koa');
});
});
11 changes: 11 additions & 0 deletions test/fixtures/disable-dedupe-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "disable-dedupe-demo",
"dependencies": {
"koa": "2"
},
"config": {
"npminstall": {
"disableDedupe": true
}
}
}
6 changes: 6 additions & 0 deletions test/fixtures/disable-dedupe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "disable-dedupe-demo",
"dependencies": {
"koa": "2"
}
}
12 changes: 6 additions & 6 deletions test/installGit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const npminstall = require('./npminstall');
const coffee = require('coffee');
const installBin = path.join(__dirname, '..', 'bin', 'install.js');

describe('test/installGit.test.js', function() {
describe('test/installGit.test.js', () => {

// here we use a tmp dir for custom testing since `getLastCommitHash` would silently pass
// and return **this** repo's commit hash.
Expand Down Expand Up @@ -123,16 +123,16 @@ describe('test/installGit.test.js', function() {
assert.equal(pkg.version, '1.2.0');
});

it('should also ok on https://github.com/gulpjs/gulp#4.0', function* () {
it('should also ok on https://github.com/node-modules/agentkeepalive#2.x', function* () {
yield npminstall({
root: tmp,
pkgs: [
{ name: null, version: 'git+https://github.com/gulpjs/gulp.git#4.0' },
{ name: null, version: 'git+https://github.com/node-modules/agentkeepalive#2.x' },
],
});

const pkg = yield readJSON(path.join(tmp, 'node_modules/gulp/package.json'));
assert.equal(pkg.name, 'gulp');
const pkg = yield readJSON(path.join(tmp, 'node_modules/agentkeepalive/package.json'));
assert.equal(pkg.name, 'agentkeepalive');
});

it('should fail on some strange hash', function* () {
Expand All @@ -156,7 +156,7 @@ describe('test/installGit.test.js', function() {
cwd: tmp,
})
.debug()
.expect(0)
.expect('code', 0)
.expect('stderr', /Package name unmatched: expected error but found nunjucks/)
.end(err => {
assert(require(path.join(tmp, 'node_modules/error/package.json')).name === 'nunjucks');
Expand Down
12 changes: 6 additions & 6 deletions test/installGlobal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('test/installGlobal.test.js', () => {
`${registry}/egg-bin/-/egg-bin-1.6.0.tgz`,
])
.debug()
.expect(/All packages installed/)
.expect('stdout', /All packages installed/)
.expect('code', 0)
.end();

Expand All @@ -57,7 +57,7 @@ describe('test/installGlobal.test.js', () => {
`${registry}/egg-bin/-/egg-bin-1.7.0.tgz`,
])
.debug()
.expect(/All packages installed/)
.expect('stdout', /All packages installed/)
.expect('code', 0)
.end();

Expand All @@ -74,7 +74,7 @@ describe('test/installGlobal.test.js', () => {
'contributors@0',
])
.debug()
.expect(/All packages installed/)
.expect('stdout', /All packages installed/)
.expect('code', 0)
.end();

Expand All @@ -89,9 +89,9 @@ describe('test/installGlobal.test.js', () => {
'egg-bin',
])
.debug()
.expect(/Downloading egg-bin to /)
.expect(/Installing egg-bin's dependencies to /)
.expect(/All packages installed/)
.expect('stdout', /Downloading egg-bin to /)
.expect('stdout', /Installing egg-bin's dependencies to /)
.expect('stdout', /All packages installed/)
.expect('code', 0)
.end();

Expand Down
4 changes: 2 additions & 2 deletions test/optionalDependencies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const readJSON = require('../lib/utils').readJSON;
const mkdirp = require('mkdirp');
const npminstall = require('./npminstall');

describe('test/optionalDependencies.test.js', function() {
describe('test/optionalDependencies.test.js', () => {
const tmp = path.join(__dirname, 'fixtures', 'tmp');
const root = path.join(__dirname, 'fixtures', 'optional');

Expand All @@ -27,7 +27,7 @@ describe('test/optionalDependencies.test.js', function() {
yield npminstall({
root: tmp,
pkgs: [
{ name: 'koa-redis', version: 'latest' },
{ name: 'koa-redis', version: '3.1.0' },
],
});
const pkg = yield readJSON(path.join(tmp, 'node_modules/koa-redis/package.json'));
Expand Down
2 changes: 1 addition & 1 deletion test/registry-only.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('test/registry-only.test.js', () => {
cwd: root,
})
.debug()
.expect(/Only allow install package from registry/)
.expect('stderr', /Only allow install package from registry/)
.expect('code', 1)
.end();
});
Expand Down
10 changes: 5 additions & 5 deletions test/uninstallGlobal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const rimraf = require('rimraf');
const coffee = require('coffee');
const mkdirp = require('../lib/utils').mkdirp;

describe('test/uninstallGlobal.test.js', function() {
describe('test/uninstallGlobal.test.js', () => {
const tmp = path.join(__dirname, 'fixtures', 'tmp');

function cleanup() {
Expand All @@ -26,7 +26,7 @@ describe('test/uninstallGlobal.test.js', function() {
'mocha',
])
.debug()
.expect(/All packages installed/)
.expect('stdout', /All packages installed/)
.expect('code', 0)
.end(err => {
assert(!err);
Expand All @@ -36,9 +36,9 @@ describe('test/uninstallGlobal.test.js', function() {
'mocha',
])
.debug()
.expect(/- mocha@\d+\.\d+\.\d+ \.\/test\/fixtures\/tmp\/lib\/node_modules\/mocha/)
.expect(/- mocha@\d+\.\d+\.\d+ \.\/test\/fixtures\/tmp\/bin\/mocha/)
.expect(/- mocha@\d+\.\d+\.\d+ \.\/test\/fixtures\/tmp\/bin\/_mocha/)
.expect('stdout', /- mocha@\d+\.\d+\.\d+ \.\/test\/fixtures\/tmp\/lib\/node_modules\/mocha/)
.expect('stdout', /- mocha@\d+\.\d+\.\d+ \.\/test\/fixtures\/tmp\/bin\/mocha/)
.expect('stdout', /- mocha@\d+\.\d+\.\d+ \.\/test\/fixtures\/tmp\/bin\/_mocha/)
.expect('code', 0)
.end(done);
});
Expand Down

0 comments on commit a36dd1c

Please sign in to comment.