Skip to content

Commit

Permalink
fix: install local folder use npm pack (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Jul 7, 2020
1 parent a92ab3b commit 1320f30
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lib/download/local.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const debug = require('debug')('npminstall:download:local');
const cp = require('mz/child_process');
const rimraf = require('mz-modules/rimraf');
const fs = require('mz/fs');
const path = require('path');
const chalk = require('chalk');
Expand Down Expand Up @@ -33,7 +35,22 @@ module.exports = async (pkg, options) => {

async function localFolder(filepath, pkg, options) {
debug(`install ${pkg.name}@${pkg.rawSpec} from local folder ${filepath}`);
return await utils.copyInstall(filepath, options);
try {
// use npm pack to ensure npmignore/gitignore/package.files work fine
const res = await cp.exec('npm pack', { cwd: filepath });
if (res && res[0]) {
const tarball = path.join(filepath, res[0].trim());
try {
return await localTarball(tarball, pkg, options);
} finally {
await rimraf(tarball);
}
}
} catch (err) {
// fallback to copy
debug(`install ${pkg.name}@${pkg.rawSpec} from local folder ${filepath} with npm pack failed(${err.message}), use copy`);
return await utils.copyInstall(filepath, options);
}
}

async function localTarball(filepath, pkg, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/ignoreScripts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('test/ignoreScripts.test.js', () => {
});

const dirs = await fs.readdir(path.join(root, 'node_modules'));
assert.deepEqual(dirs.sort(), [ '_pkg@1.0.0@pkg', '.package_versions.json', 'pkg' ].sort());
assert.deepEqual(dirs.sort(), [ '_pkg@1.0.0@pkg', '.package_versions.json', '.tmp', 'pkg' ].sort());
const files = await fs.readdir(path.join(root, 'node_modules/pkg'));
assert.deepEqual(files, [ 'index.js', 'package.json' ]);
});
Expand Down
14 changes: 14 additions & 0 deletions test/installLocal.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

const mm = require('mm');
const assert = require('assert');
const path = require('path');
const coffee = require('coffee');
const npminstall = require('./npminstall');
const helper = require('./helper');
const cp = require('mz/child_process');

describe('test/installLocal.test.js', () => {
const root = helper.fixtures('local');
Expand All @@ -24,6 +26,18 @@ describe('test/installLocal.test.js', () => {
assert.equal(pkg.name, 'pkg');
});

it('should install local folder with copy ok', async () => {
mm.error(cp, 'exec');
await npminstall({
root,
pkgs: [
{ name: null, version: 'file:pkg' },
],
});
const pkg = await helper.readJSON(path.join(root, 'node_modules/pkg/package.json'));
assert.equal(pkg.name, 'pkg');
});

it('should install local folder with relative path ok', async () => {
await npminstall({
root,
Expand Down

0 comments on commit 1320f30

Please sign in to comment.