Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Avoid shell mangling during eslint --init #8936

Merged
merged 1 commit into from
Jul 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/util/npm-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//------------------------------------------------------------------------------

const fs = require("fs"),
childProcess = require("child_process"),
spawn = require("cross-spawn"),
path = require("path"),
log = require("../logging");

Expand Down Expand Up @@ -50,10 +50,10 @@ function findPackageJson(startDir) {
* @returns {void}
*/
function installSyncSaveDev(packages) {
if (Array.isArray(packages)) {
packages = packages.join(" ");
if (!Array.isArray(packages)) {
packages = [packages];
}
childProcess.execSync(`npm i --save-dev ${packages}`, { stdio: "inherit", encoding: "utf8" });
spawn.sync("npm", ["i", "--save-dev"].concat(packages), { stdio: "inherit" });
}

/**
Expand All @@ -62,10 +62,11 @@ function installSyncSaveDev(packages) {
* @returns {string[]} Gotten peerDependencies.
*/
function fetchPeerDependencies(packageName) {
const fetchedText = childProcess.execSync(
`npm show --json ${packageName} peerDependencies`,
const fetchedText = spawn.sync(
"npm",
["show", "--json", packageName, "peerDependencies"],
{ encoding: "utf8" }
).trim();
).stdout.trim();

return JSON.parse(fetchedText || "{}");
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"babel-code-frame": "^6.22.0",
"chalk": "^1.1.3",
"concat-stream": "^1.6.0",
"cross-spawn": "^5.1.0",
"debug": "^2.6.8",
"doctrine": "^2.0.0",
"eslint-scope": "^3.7.1",
Expand Down
17 changes: 10 additions & 7 deletions tests/lib/util/npm-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//------------------------------------------------------------------------------

const assert = require("chai").assert,
childProcess = require("child_process"),
spawn = require("cross-spawn"),
sinon = require("sinon"),
npmUtil = require("../../../lib/util/npm-util"),
log = require("../../../lib/logging"),
Expand Down Expand Up @@ -170,31 +170,34 @@ describe("npmUtil", () => {

describe("installSyncSaveDev()", () => {
it("should invoke npm to install a single desired package", () => {
const stub = sandbox.stub(childProcess, "execSync");
const stub = sandbox.stub(spawn, "sync");

npmUtil.installSyncSaveDev("desired-package");
assert(stub.calledOnce);
assert.equal(stub.firstCall.args[0], "npm i --save-dev desired-package");
assert.equal(stub.firstCall.args[0], "npm");
assert.deepEqual(stub.firstCall.args[1], ["i", "--save-dev", "desired-package"]);
stub.restore();
});

it("should accept an array of packages to install", () => {
const stub = sandbox.stub(childProcess, "execSync");
const stub = sandbox.stub(spawn, "sync");

npmUtil.installSyncSaveDev(["first-package", "second-package"]);
assert(stub.calledOnce);
assert.equal(stub.firstCall.args[0], "npm i --save-dev first-package second-package");
assert.equal(stub.firstCall.args[0], "npm");
assert.deepEqual(stub.firstCall.args[1], ["i", "--save-dev", "first-package", "second-package"]);
stub.restore();
});
});

describe("fetchPeerDependencies()", () => {
it("should execute 'npm show --json <packageName> peerDependencies' command", () => {
const stub = sandbox.stub(childProcess, "execSync").returns("");
const stub = sandbox.stub(spawn, "sync").returns({ stdout: "" });

npmUtil.fetchPeerDependencies("desired-package");
assert(stub.calledOnce);
assert.equal(stub.firstCall.args[0], "npm show --json desired-package peerDependencies");
assert.equal(stub.firstCall.args[0], "npm");
assert.deepEqual(stub.firstCall.args[1], ["show", "--json", "desired-package", "peerDependencies"]);
stub.restore();
});
});
Expand Down