From a671eca9650da876837543ba78ddfad1c8a6fa2c Mon Sep 17 00:00:00 2001 From: nathanhleung Date: Wed, 1 Aug 2018 22:02:52 -0400 Subject: [PATCH] fix: -0 bug described in issue #33 --- .gitignore | 4 ++++ src/cli.test.js | 13 ++++++++++++- src/install-peerdeps.js | 10 +++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f98bc93..11dcafe 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,10 @@ /lib/ /test/ +# Sandbox (for tests) +fixtures/sandbox/node_modules/* + + ## From https://raw.githubusercontent.com/github/gitignore/master/Node.gitignore # Logs logs diff --git a/src/cli.test.js b/src/cli.test.js index 5c2e5b8..d6bb99e 100644 --- a/src/cli.test.js +++ b/src/cli.test.js @@ -1,4 +1,5 @@ import { spawn } from "child_process"; +import path from "path"; import test from "tape"; /** @@ -9,7 +10,8 @@ import test from "tape"; function spawnCli(extraArgs) { return spawn( "node", - ["--require", "babel-register", "cli.js"].concat(extraArgs) + ["--require", "babel-register", "cli.js"].concat(extraArgs), + { cwd: path.join(__dirname, "..", "fixtures", "sandbox") } ); } @@ -17,6 +19,7 @@ function spawnCli(extraArgs) { * Gets the resulting install command given the provided arguments * @async * @param {string[]} extraArgs - arguments to be passed to the CLI + * @param {boolean} noDryRun - whether to actually run the command or not * @returns {Promise} - a Promise which resolves to the resulting install command */ async function getCliInstallCommand(extraArgs) { @@ -83,6 +86,14 @@ test("adds an explicit `--no-save` when using `--silent` with NPM", t => { }, t.fail); }); +test("installs packages correctly even if package name ends with '-0'", t => { + const cli = spawnCli("enzyme-adapter-react-16@1.1.1"); + cli.on("exit", code => { + t.equal(code, 0, `errored, exit code was ${code}`); + t.end(); + }); +}); + // @todo - tests for the actual install process // see https://github.com/sindresorhus/has-yarn/blob/master/test.js for details // Perhaps abstract the functionality of getting the package name diff --git a/src/install-peerdeps.js b/src/install-peerdeps.js index 72dd14d..0c3817f 100644 --- a/src/install-peerdeps.js +++ b/src/install-peerdeps.js @@ -190,10 +190,18 @@ function installPeerDeps( // I know I can push it, but I'll just // keep concatenating for consistency args = args.concat(subcommand); + // See issue #33 - issue with "-0" + function fixPackageName(packageName) { + if (packageName.substr(-2) === "-0") { + // Remove -0 + return packageName.substr(0, packageName.length - 2); + } + return packageName; + } // If we have spaces in our args spawn() // cries foul so we'll split the packagesString // into an array of individual packages - args = args.concat(packagesString.split(" ")); + args = args.concat(packagesString.split(" ").map(fixPackageName)); // If devFlag is empty, then we'd be adding an empty arg // That causes the command to fail if (devFlag !== "") {