diff --git a/lib/download/local.js b/lib/download/local.js index 200d49da..7e9c772b 100644 --- a/lib/download/local.js +++ b/lib/download/local.js @@ -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'); @@ -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) { diff --git a/test/ignoreScripts.test.js b/test/ignoreScripts.test.js index 07819b0f..e5f53568 100644 --- a/test/ignoreScripts.test.js +++ b/test/ignoreScripts.test.js @@ -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' ]); }); diff --git a/test/installLocal.test.js b/test/installLocal.test.js index 6c1f17d1..8a49c426 100644 --- a/test/installLocal.test.js +++ b/test/installLocal.test.js @@ -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'); @@ -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,