Skip to content

Commit

Permalink
Fix for issue nathanhleung#43. install peer deps doesn't update a pee…
Browse files Browse the repository at this point in the history
…r dep when a more recent version is already installed.
  • Loading branch information
ashleydavis committed Jan 15, 2019
1 parent 402b697 commit 95dd3d1
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 141 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Fixtures ignore
fixtures/sandbox/node_modules/
fixtures/has-newer-peer-dep/node_modules/

## From https://raw.githubusercontent.com/github/gitignore/master/Node.gitignore
# Logs
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions fixtures/has-newer-peer-dep/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

Example project to illustrate [issue 43](https://github.com/nathanhleung/install-peerdeps/issues/43).

This project has data-forge 1.3.3 installed.

It also has data-forge-indicators 0.1.7 which has a peer dependency on data-forge 1.0.10.

Previously using install-peerdeps to install peers of data-forge-indicators would have overwritten data-forge with the older version.
8 changes: 8 additions & 0 deletions fixtures/has-newer-peer-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "has-newer-peer-deps",
"private": true,
"dependencies": {
"data-forge": "1.3.3",
"data-forge-indicators": "0.1.7"
}
}
74 changes: 72 additions & 2 deletions src/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn } from "child_process";
import { spawn, exec } from "child_process";
import path from "path";
import test from "tape";
// import fs from "fs";
import fs from "fs";

/**
* Spawns the CLI with the provided arguments
Expand Down Expand Up @@ -52,6 +52,50 @@ async function getCliInstallCommand(extraArgs) {
});
}

/**
* Run a shell command.
* @param {string} cmd - The command to run.
* @param {string} cwd - Working directory for the command.
*/
function runCmd(cmd, cwd) {
return new Promise((resolve, reject) => {
console.log(`! ${cmd} in ${cwd}`);
exec(cmd, { cwd }, (err /* , stdout, stderr */) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

/**
* Cleans and reinstalls modules in a fixture directory.
* @param {string} fixture - The fixture for which to reset node modules.
*/
async function resetFixtureNodeModules(fixture) {
const cwd = path.join(__dirname, "..", "fixtures", fixture);
await runCmd("rm -rf node_modules", cwd);
await runCmd("npm install", cwd);
}

/**
* Read a JSON file from disk.
* @param {string} filePath - The path to the JSON file to be read.
*/
function readJSON(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, "utf8", (err, packageFileJson) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(packageFileJson));
}
});
});
}

test("errors when more than one package is provided", t => {
const cli = spawnCli(["eslint-config-airbnb", "angular"]);
cli.on("exit", code => {
Expand Down Expand Up @@ -133,6 +177,32 @@ test("installs packages correctly even if package name ends with '-0'", t => {
});
});

test("peer dependency is not installed when a later version already exists", t => {
resetFixtureNodeModules("has-newer-peer-dep")
.then(() => {
const cli = spawnCli("data-forge-indicators", "has-newer-peer-dep");
cli.on("exit", code => {
t.equal(code, 0, `errored, exit code was ${code}`);

readJSON(
"fixtures/has-newer-peer-dep/node_modules/data-forge/package.json"
)
.then(packageFile => {
t.equal(packageFile.version, "1.3.3", "Bad version!");
t.end();
})
.catch(err => {
t.fail((err && err.stack) || err);
t.end();
});
});
})
.catch(err => {
t.fail((err && err.stack) || err);
t.end();
});
});

// Work on this test later
/*
test("doesn't replace existing installed peer dependencies", t => {
Expand Down
Loading

0 comments on commit 95dd3d1

Please sign in to comment.