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

Ignore node_modules when running prettier from CLI #1683

Merged
merged 7 commits into from May 23, 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
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
node_modules
/node_modules
npm-debug.log
/errors
/test.js
Expand Down
15 changes: 14 additions & 1 deletion bin/prettier.js
Expand Up @@ -3,6 +3,7 @@
"use strict";

const fs = require("fs");
const path = require("path");
const getStdin = require("get-stdin");
const glob = require("glob");
const chalk = require("chalk");
Expand Down Expand Up @@ -30,6 +31,7 @@ const argv = minimist(process.argv.slice(2), {
"version",
"debug-print-doc",
"debug-check",
"with-node-modules",
// Deprecated in 0.0.10
"flow-parser"
],
Expand Down Expand Up @@ -64,6 +66,10 @@ if (argv["version"]) {
const filepatterns = argv["_"];
const write = argv["write"];
const stdin = argv["stdin"] || (!filepatterns.length && !process.stdin.isTTY);
const ignoreNodeModules = argv["with-node-modules"] === false;
const globOptions = {
ignore: ignoreNodeModules && "**/node_modules/**"
};

if (write && argv["debug-check"]) {
console.error("Cannot use --write and --debug-check together.");
Expand Down Expand Up @@ -341,11 +347,14 @@ if (stdin) {
function eachFilename(patterns, callback) {
patterns.forEach(pattern => {
if (!glob.hasMagic(pattern)) {
if (shouldIgnorePattern(pattern)) {
return;
}
callback(pattern);
return;
}

glob(pattern, (err, filenames) => {
glob(pattern, globOptions, (err, filenames) => {
if (err) {
console.error("Unable to expand glob pattern: " + pattern + "\n" + err);
// Don't exit the process if one pattern failed
Expand All @@ -359,3 +368,7 @@ function eachFilename(patterns, callback) {
});
});
}

function shouldIgnorePattern(pattern) {
return ignoreNodeModules && path.resolve(pattern).includes("/node_modules/");
}
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -22,6 +22,7 @@
"minimist": "1.2.0"
},
"devDependencies": {
"cross-spawn": "^5.1.0",
"diff": "3.2.0",
"eslint": "^3.19.0",
"eslint-plugin-prettier": "^2.1.1",
Expand All @@ -40,6 +41,7 @@
},
"scripts": {
"test": "jest",
"test-integration": "jest tests_integration",
"lint": "eslint .",
"build:docs": "rollup -c docs/rollup.config.js"
},
Expand All @@ -50,11 +52,10 @@
"snapshotSerializers": [
"<rootDir>/tests_config/raw-serializer.js"
],
"testRegex": "jsfmt\\.spec\\.js$",
"testRegex": "jsfmt\\.spec\\.js$|__tests__/.*\\.js$",
"testPathIgnorePatterns": [
"tests/new_react",
"tests/more_react",

"see https://github.com/eslint/typescript-eslint-parser/issues/269",
"tests/typescript/conformance/types/abstractKeyword/jsfmt.spec.js"
]
Expand Down
7 changes: 7 additions & 0 deletions tests_integration/.eslintrc.js
@@ -0,0 +1,7 @@
"use strict";

module.exports = {
env: {
jest: true
}
};
@@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`doesn't ignore node_modules with --with-node-modules flag 1`] = `
"node_modules/node-module.js
not_node_modules/file.js
regular-module.js
"
`;

exports[`doesn't ignore node_modules with --with-node-modules flag for file list 1`] = `
"node_modules/node-module.js
not_node_modules/file.js
regular-module.js
"
`;

exports[`ignores node_modules by default 1`] = `
"not_node_modules/file.js
regular-module.js
"
`;

exports[`ignores node_modules by default for file list 1`] = `
"not_node_modules/file.js
regular-module.js
"
`;
42 changes: 42 additions & 0 deletions tests_integration/__tests__/with-node-modules.js
@@ -0,0 +1,42 @@
"use strict";

const runPrettier = require("../runPrettier");

test("ignores node_modules by default", () => {
const result = runPrettier("cli/with-node-modules", ["**/*.js", "-l"]);

expect(result.stdout).toMatchSnapshot();
});

test("doesn't ignore node_modules with --with-node-modules flag", () => {
const result = runPrettier("cli/with-node-modules", [
"**/*.js",
"-l",
"--with-node-modules"
]);

expect(result.stdout).toMatchSnapshot();
});

test("ignores node_modules by default for file list", () => {
const result = runPrettier("cli/with-node-modules", [
"node_modules/node-module.js",
"not_node_modules/file.js",
"regular-module.js",
"-l"
]);

expect(result.stdout).toMatchSnapshot();
});

test("doesn't ignore node_modules with --with-node-modules flag for file list", () => {
const result = runPrettier("cli/with-node-modules", [
"node_modules/node-module.js",
"not_node_modules/file.js",
"regular-module.js",
"-l",
"--with-node-modules"
]);

expect(result.stdout).toMatchSnapshot();
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests_integration/cli/with-node-modules/regular-module.js
@@ -0,0 +1,2 @@
/* eslint-disable */
'use strict';
30 changes: 30 additions & 0 deletions tests_integration/runPrettier.js
@@ -0,0 +1,30 @@
/*
* runPrettier – spawns `prettier` process.
* Adopted from Jest's integration tests suite.
*/
"use strict";

const path = require("path");
const spawnSync = require("cross-spawn").sync;

const PRETTIER_PATH = path.resolve(__dirname, "../bin/prettier.js");

// return the result of the spawned process:
// [ 'status', 'signal', 'output', 'pid', 'stdout', 'stderr',
// 'envPairs', 'options', 'args', 'file' ]
function runPrettier(dir, args) {
const isRelative = dir[0] !== "/";

if (isRelative) {
dir = path.resolve(__dirname, dir);
}

const result = spawnSync(PRETTIER_PATH, args || [], { cwd: dir });

result.stdout = result.stdout && result.stdout.toString();
result.stderr = result.stderr && result.stderr.toString();

return result;
}

module.exports = runPrettier;
35 changes: 34 additions & 1 deletion yarn.lock
Expand Up @@ -592,6 +592,14 @@ create-hmac@^1.1.0, create-hmac@^1.1.2:
create-hash "^1.1.0"
inherits "^2.0.1"

cross-spawn@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
dependencies:
lru-cache "^4.0.1"
shebang-command "^1.2.0"
which "^1.2.9"

cryptiles@2.x.x:
version "2.0.5"
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
Expand Down Expand Up @@ -1843,6 +1851,13 @@ loose-envify@^1.0.0:
dependencies:
js-tokens "^3.0.0"

lru-cache@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.0.2.tgz#1d17679c069cda5d040991a09dbc2c0db377e55e"
dependencies:
pseudomap "^1.0.1"
yallist "^2.0.0"

magic-string@^0.16.0:
version "0.16.0"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.16.0.tgz#970ebb0da7193301285fb1aa650f39bdd81eb45a"
Expand Down Expand Up @@ -2189,6 +2204,10 @@ prr@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"

pseudomap@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

public-encrypt@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/public-encrypt/-/public-encrypt-4.0.0.tgz#39f699f3a46560dd5ebacbca693caf7c65c18cc6"
Expand Down Expand Up @@ -2467,6 +2486,16 @@ sha.js@^2.3.6:
dependencies:
inherits "^2.0.1"

shebang-command@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
dependencies:
shebang-regex "^1.0.0"

shebang-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"

shelljs@^0.7.5:
version "0.7.7"
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.7.tgz#b2f5c77ef97148f4b4f6e22682e10bba8667cff1"
Expand Down Expand Up @@ -2782,7 +2811,7 @@ which-module@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/which-module/-/which-module-1.0.0.tgz#bba63ca861948994ff307736089e3b96026c2a4f"

which@^1.2.12:
which@^1.2.12, which@^1.2.9:
version "1.2.14"
resolved "https://registry.yarnpkg.com/which/-/which-1.2.14.tgz#9a87c4378f03e827cecaf1acdf56c736c01c14e5"
dependencies:
Expand Down Expand Up @@ -2840,6 +2869,10 @@ y18n@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"

yallist@^2.0.0:
version "2.1.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"

yargs-parser@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-5.0.0.tgz#275ecf0d7ffe05c77e64e7c86e4cd94bf0e1228a"
Expand Down