diff --git a/install-npm.js b/install-npm.js index 7227baf..06d3ab4 100644 --- a/install-npm.js +++ b/install-npm.js @@ -1,21 +1,42 @@ // do it inline in sync way // to make it work in non-npm environment -var npmModule +var npmBin + , executioner , path = require('path') + , node = process.argv[0] ; -if (process.env['npm_execpath'] && process.env['npm_execpath'].match(/\/node_modules\/npm\/bin\/npm-cli\.js$/)) { - npmModule = require(path.resolve(process.env['npm_execpath'], '..', '..')); +if (process.env['npm_execpath']) { + var execPath = process.env['npm_execpath']; + var expectedPath = path.join('bin', 'npm-cli.js'); + + if (execPath.slice(-1 * expectedPath.length) === expectedPath) { + npmBin = path.resolve(execPath); + } } // if no npm module found, don't expose any function // to allow upstream modules find alternatives module.exports = null; -if (npmModule) { - module.exports = function(packages, options, done) { - npmModule.load(options, function() { - npmModule.commands.install(packages, done); +if (npmBin) { + executioner = require('executioner'); + + module.exports = function(packages, config, done) { + var options = { + node : node, + npm : npmBin, + // escape package name@versions + packages: packages.map((pkg) => '"' + pkg + '"').join(' ') + }; + + executioner('"${node}" "${npm}" install --no-save --no-package-lock ${packages}', options, function (error, result) { + if (error) { + console.error('Unable to install peerDependencies', error); + process.exit(1); + return; + } + done(result); }); } } diff --git a/install-yarn.js b/install-yarn.js index 23f1b97..db25812 100644 --- a/install-yarn.js +++ b/install-yarn.js @@ -1,39 +1,42 @@ // do it inline in sync way // to make it work in non-npm environment -var yarnModule +var yarnBin , executioner , path = require('path') , node = process.argv[0] ; -if (process.env['npm_execpath'] && process.env['npm_execpath'].match(/node_modules\/yarn\/bin\/yarn\.js$/)) { - yarnModule = path.resolve(process.env['npm_execpath'], '..', '..', 'lib', 'cli'); +if (process.env['npm_execpath'] && process.env['npm_execpath'].match(/node_modules[\/\\]yarn[\/\\]bin[\/\\]yarn\.js$/)) { + var execPath = process.env['npm_execpath']; + var expectedPath = path.join('yarn', 'bin', 'yarn.js'); + + if (execPath.slice(-1 * expectedPath.length) === expectedPath) { + yarnBin = path.resolve(execPath, '..', '..', 'lib', 'cli'); + } } // if no yarn module found, don't expose any function // to allow upstream modules find alternatives module.exports = null; -if (yarnModule) { - +if (yarnBin) { executioner = require('executioner'); module.exports = function(packages, extra, done) { - var options = { node : node, - yarn : yarnModule, + yarn : yarnBin, // escape package names@versions packages: packages.map((pkg) => '"' + pkg + '"').join(' ') }; - executioner('${node} ${yarn} add --peer --no-lockfile ${packages}', options, function(error, result) { + executioner('"${node}" "${yarn}" add --peer --no-lockfile ${packages}', options, function(error, result) { if (error) { console.error('Unable to install peerDependencies', error); process.exit(1); return; } - done(); + done(result); }); // Looks like yarn shows last line from the output of sub-scripts diff --git a/install.js b/install.js index 731e6d8..c488ab8 100644 --- a/install.js +++ b/install.js @@ -3,7 +3,7 @@ var fs = require('fs') , installNpm = require('./install-npm.js') , installYarn = require('./install-yarn.js') - , rootPath = path.resolve(__dirname, '..', '..') + , rootPath = process.env.INIT_CWD || path.resolve(process.cwd(), '..', '..') , envLabel = 'skip_install_peers_as_dev' @@ -56,16 +56,19 @@ function installPeerDeps() { installYarn(peerDeps, peerInstallOptions, installDone.bind(null, 'yarn')); } else if (installNpm) { installNpm(peerDeps, peerInstallOptions, installDone.bind(null, 'npm')); + } else { + console.error('Did not find a viable package manager to install dependencies with.'); } }); } -function installDone(tool) { - +function installDone(tool, result) { // cleanup env process.env[envLabel] = ''; console.log('Installed peerDependencies as devDependencies via ' + tool + '.'); + + console.log(result); } function getPeerDeps(config) { diff --git a/package.json b/package.json index b213c34..c748aa0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "install-peers", - "version": "1.0.2", + "version": "1.0.3", "description": "Automatically installs project's peerDependencies (as devDependencies)", "main": "index.js", "scripts": {