Skip to content

Commit

Permalink
feat: link root packages to storeDir/node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Mar 16, 2016
1 parent ad89eaf commit 8cf6c2c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 14 deletions.
28 changes: 20 additions & 8 deletions lib/local_install.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ const parallel = require('co-parallel');
const semver = require('semver');
const bytes = require('bytes');
const Module = require('module');
const fs = require('mz/fs');
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');

/**
* npm install
* @param {Object} options - install options
Expand Down Expand Up @@ -123,14 +123,18 @@ module.exports = function*(options) {

const linkTasks = [];
for (const item of options.latestVersions) {
// don't link root package to `storeDir/node_modules`
if (rootPkgsMap.has(item[0])) {
continue;
const name = item[0];
const version = item[1];
if (rootPkgsMap.has(name)) {
// link root package to `storeDir/node_modules`
linkTasks.push(linkRootPackage(name, nodeModulesDir, options.storeDir));
} else {
// link latest package to `storeDir/node_modules`
linkTasks.push(linkLatestVersion({
name: name,
version: version,
}, options.storeDir));
}
linkTasks.push(linkLatestVersion({
name: item[0],
version: item[1],
}, options.storeDir));
}
yield linkTasks;

Expand Down Expand Up @@ -253,6 +257,14 @@ function* linkLatestVersion(pkg, storeDir) {
debug('%s@%s link %s => %s', pkg.name, pkg.version, linkDir, relative);
}

function* linkRootPackage(name, nodeModulesDir, storeDir) {
const linkDir = path.join(storeDir, 'node_modules', name);
const realDir = yield fs.realpath(path.join(nodeModulesDir, name));
yield utils.mkdirp(path.dirname(linkDir));
const relative = yield utils.forceSymlink(realDir, linkDir);
debug('root pacakge %s link %s => %s', name, linkDir, relative);
}

function* runPostInstallTasks(options) {
if (options.postInstallTasks.length) {
options.console.log(chalk.yellow('excute post install scripts...'));
Expand Down
2 changes: 1 addition & 1 deletion test/bundleDependencies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('test/bundleDependencies.test.js', function() {

// only node-pre-gyp dir exists
const dirs = yield fs.readdir(path.join(tmp, 'node_modules/.npminstall'));
assert.deepEqual(dirs, [ 'node-pre-gyp' ]);
assert.deepEqual(dirs.sort(), [ 'node-pre-gyp', 'node_modules' ].sort());
});

if (process.platform !== 'win32') {
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ describe('test/index.test.js', function() {
});
// node_modules/.npminstall/node_modules/ms should exists
assert(yield fs.exists(path.join(root, 'node_modules', '.npminstall', 'node_modules', 'ms')));
// node_modules/.npminstall/node_modules/debug should not exists
assert(!(yield fs.exists(path.join(root, 'node_modules', '.npminstall', 'node_modules', 'debug'))));
// node_modules/.npminstall/node_modules/debug should exists
assert(yield fs.exists(path.join(root, 'node_modules', '.npminstall', 'node_modules', 'debug')));
assert(yield fs.exists(path.join(root, 'node_modules', 'pedding')));

const debugPkg = yield readJSON(path.join(root, 'node_modules', 'debug', 'package.json'));
Expand Down
2 changes: 1 addition & 1 deletion test/installRemote.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('test/installRemote.test.js', function() {
assert.equal(pkg.name, 'taffydb');

const dirs = yield fs.readdir(path.join(root, 'node_modules/.npminstall'));
assert.deepEqual(dirs.sort(), [ '.tmp', 'pedding', 'taffydb' ].sort());
assert.deepEqual(dirs.sort(), [ '.tmp', 'pedding', 'taffydb', 'node_modules' ].sort());
});

it('should install http://r.cnpmjs.org/taffydb/download/taffydb-2.7.2.tgz', function*() {
Expand Down
4 changes: 2 additions & 2 deletions test/linkLatestVersion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ describe('test/linkLatestVersion.test.js', function() {
'node_modules', '.npminstall', 'node_modules', 'iconv-lite', 'package.json'));
assert.equal(pkg2.name, 'iconv-lite');

// debug should not link
// debug also need should not link
const exists = yield fs.exists(path.join(root, 'node_modules', '.npminstall', 'node_modules', 'debug'));
assert(!exists);
assert(exists);
});
});
48 changes: 48 additions & 0 deletions test/linkRoot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
*/

'use strict';

/**
* Module dependencies.
*/

const npminstall = require('./npminstall');
const rimraf = require('rimraf');
const assert = require('assert');
const path = require('path');
const mkdirp = require('mkdirp');

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

function cleanup() {
rimraf.sync(tmp);
}

beforeEach(() => {
cleanup();
mkdirp.sync(tmp);
});

afterEach(cleanup);

it('should display error when post install', function*() {
yield npminstall({
root: tmp,
pkgs: [
{ name: 'es3ify', version: '0.1.2' },
{ name: 'es3ify-loader', version: '0.1.0' },
],
});
let pkg = require(path.join(tmp, 'node_modules/.npminstall/node_modules/es3ify/package.json'));
assert.equal(pkg.version, '0.1.2');
pkg = require(path.join(tmp, 'node_modules/.npminstall/node_modules/es3ify-loader/package.json'));
assert.equal(pkg.version, '0.1.0');
});
});

0 comments on commit 8cf6c2c

Please sign in to comment.