Skip to content

Commit

Permalink
Handle UNC paths (workaround isaacs/node-glob#74), fixes prettier#2945?
Browse files Browse the repository at this point in the history
  • Loading branch information
azz committed Oct 2, 2017
1 parent a6096ee commit 7a01f5c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"postcss-values-parser": "1.3.1",
"strip-bom": "3.0.0",
"typescript": "2.5.3",
"typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#9c71a627da36e97da52ed2731d58509c952b67ae"
"typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#9c71a627da36e97da52ed2731d58509c952b67ae",
"unc-path-regex": "0.1.2"
},
"devDependencies": {
"babel-cli": "6.24.1",
Expand Down
47 changes: 39 additions & 8 deletions src/cli-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ignore = require("ignore");
const chalk = require("chalk");
const readline = require("readline");
const leven = require("leven");
const uncPathRegex = require("unc-path-regex")();

const prettier = eval("require")("../index");
const cleanAST = require("./clean-ast").cleanAST;
Expand Down Expand Up @@ -250,19 +251,13 @@ function eachFilename(argv, patterns, callback) {
}

try {
const filePaths = globby
.sync(patterns, { dot: true })
.map(
filePath =>
path.isAbsolute(filePath)
? path.relative(process.cwd(), filePath)
: filePath
);
const filePaths = getFilesFromPatterns(patterns);
if (filePaths.length === 0) {
console.error(`No matching files. Patterns tried: ${patterns.join(" ")}`);
process.exitCode = 2;
return;
}

ignorer
.filter(filePaths)
.forEach(filePath =>
Expand All @@ -277,6 +272,42 @@ function eachFilename(argv, patterns, callback) {
}
}

function getFilesFromPatterns(patterns) {
const uncPatternTasks = [];
const nonUncPaterns = [];

// Hideous workaround for https://github.com/isaacs/node-glob/issues/74
patterns.forEach(pattern => {
const result = uncPathRegex.exec(pattern);
if (result) {
// Samba share, e.g.: `//localhost/c$`
const device = result[0];
uncPatternTasks.push({
pattern: pattern.substring(device.length),
options: { dot: true, root: device }
});
} else {
nonUncPaterns.push(pattern);
}
});

return globby
.sync(nonUncPaterns, { dot: true })
.map(
filePath =>
path.isAbsolute(filePath)
? path.relative(process.cwd(), filePath)
: filePath
)
.concat(
flatMap(uncPatternTasks, task => globby.sync(task.pattern, task.options))
);
}

function flatMap(array, iteratee) {
return array.reduce((output, item) => output.concat(iteratee(item)), []);
}

function formatFiles(argv) {
eachFilename(argv, argv.__filePatterns, (filename, options) => {
if (argv["write"]) {
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4066,6 +4066,10 @@ uid-number@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"

unc-path-regex@0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/unc-path-regex/-/unc-path-regex-0.1.2.tgz#e73dd3d7b0d7c5ed86fbac6b0ae7d8c6a69d50fa"

underscore.string@~2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/underscore.string/-/underscore.string-2.4.0.tgz#8cdd8fbac4e2d2ea1e7e2e8097c42f442280f85b"
Expand Down

0 comments on commit 7a01f5c

Please sign in to comment.