Skip to content

Commit

Permalink
Rename regexp variables in findMatchingFiles.js
Browse files Browse the repository at this point in the history
There was some confusion around these variables, so I made it clearer
what is a compiled regexp and what is just a string pattern.

Before making the rename, I went down the path of switching to passing
in a compiled RegExp to all places. But I couldn't get
the `egrep` call inside `findMatchingFilesWithFind` to work with the
`toString()` output of RegExp. So I decided to keep the string
throughout all finders (so that it's consistent).
  • Loading branch information
trotzig committed Jun 27, 2016
1 parent d061f98 commit e404b28
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/findMatchingFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import normalizePath from './normalizePath';

function findMatchingFilesWithFind(
lookupPath: string,
validFilesRegex: string
validFilesPattern: string
): Promise<Array<string>> {
const findCommand = [
`find ${lookupPath}`,
'-name "**.js*"',
'-not -path "./node_modules/*"',
].join(' ');
const command = `${findCommand} | egrep -i \"${validFilesRegex}\"`;
const command = `${findCommand} | egrep -i \"${validFilesPattern}\"`;

return new Promise((resolve: Function, reject: Function) => {
// TODO switch to spawn so we can start processing the stream as it comes
Expand All @@ -36,8 +36,9 @@ function findMatchingFilesWithFind(

function findMatchingFilesWithNode(
lookupPath: string,
validFilesRegex: string
validFilesPattern: string
): Promise<Array<string>> {
const validFilesRegex = new RegExp(validFilesPattern, 'i');
return new Promise((resolve: Function, reject: Function) => {
glob(`${lookupPath}/**/*.js*`, {
ignore: './node_modules/**',
Expand All @@ -47,16 +48,16 @@ function findMatchingFilesWithNode(
return;
}
resolve(result.filter((filePath: string): bool =>
new RegExp(validFilesRegex, 'i').test(filePath)));
validFilesRegex.test(filePath)));
});
});
}

function findMatchingFilesWithWatchman(
lookupPath: string,
validFilesRegex: string
validFilesPattern: string
): Promise<Array<string>> {
const tester = new RegExp(validFilesRegex, 'i');
const validFilesRegex = new RegExp(validFilesPattern, 'i');
const normalizedLookupPath = normalizePath(lookupPath);
return new Promise((resolve: Function) => {
const matches = [];
Expand All @@ -65,7 +66,7 @@ function findMatchingFilesWithWatchman(
if (!filePath.startsWith(normalizedLookupPath)) {
return;
}
if (tester.test(filePath)) {
if (validFilesRegex.test(filePath)) {
matches.push(filePath);
}
});
Expand All @@ -87,14 +88,14 @@ export default function findMatchingFiles(
}

const formattedVarName = formattedToRegex(variableName);
const validFilesRegex = `(/|^)${formattedVarName}(/index)?(/package)?\\.js.*`;
const validFilesPattern = `(/|^)${formattedVarName}(/index)?(/package)?\\.js.*`;

if (WatchmanFileCache.isEnabled()) {
return findMatchingFilesWithWatchman(lookupPath, validFilesRegex);
return findMatchingFilesWithWatchman(lookupPath, validFilesPattern);
}
if (/^win/.test(process.platform) ||
process.env.IMPORT_JS_USE_NODE_FINDER) {
return findMatchingFilesWithNode(lookupPath, validFilesRegex);
return findMatchingFilesWithNode(lookupPath, validFilesPattern);
}
return findMatchingFilesWithFind(lookupPath, validFilesRegex);
return findMatchingFilesWithFind(lookupPath, validFilesPattern);
}

0 comments on commit e404b28

Please sign in to comment.