/
make-file-finder.js
82 lines (65 loc) · 2.27 KB
/
make-file-finder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use strict";
const globby = require("globby");
const pMap = require("p-map");
const path = require("path");
const ValidationError = require("@lerna/validation-error");
module.exports.makeFileFinder = makeFileFinder;
module.exports.makeSyncFileFinder = makeSyncFileFinder;
function getGlobOpts(rootPath, packageConfigs) {
const globOpts = {
cwd: rootPath,
absolute: true,
expandDirectories: false,
followSymlinkedDirectories: false,
// POSIX results always need to be normalized
transform: fp => path.normalize(fp),
};
if (packageConfigs.some(cfg => cfg.indexOf("**") > -1)) {
if (packageConfigs.some(cfg => cfg.indexOf("node_modules") > -1)) {
throw new ValidationError(
"EPKGCONFIG",
"An explicit node_modules package path does not allow globstars (**)"
);
}
globOpts.ignore = [
// allow globs like "packages/**",
// but avoid picking up node_modules/**/package.json
"**/node_modules/**",
];
}
return globOpts;
}
function makeFileFinder(rootPath, packageConfigs) {
const globOpts = getGlobOpts(rootPath, packageConfigs);
return (fileName, fileMapper, customGlobOpts) => {
const options = Object.assign({}, customGlobOpts, globOpts);
const promise = pMap(
Array.from(packageConfigs).sort(),
globPath => {
let chain = globby(path.join(globPath, fileName), options);
// fast-glob does not respect pattern order, so we re-sort by absolute path
chain = chain.then(results => results.sort());
if (fileMapper) {
chain = chain.then(fileMapper);
}
return chain;
},
{ concurrency: 4 }
);
// always flatten the results
return promise.then(results => results.reduce((acc, result) => acc.concat(result), []));
};
}
function makeSyncFileFinder(rootPath, packageConfigs) {
const globOpts = getGlobOpts(rootPath, packageConfigs);
return (fileName, fileMapper, customGlobOpts) => {
const options = Object.assign({}, customGlobOpts, globOpts);
const patterns = packageConfigs.map(globPath => path.join(globPath, fileName)).sort();
let results = globby.sync(patterns, options);
/* istanbul ignore else */
if (fileMapper) {
results = results.map(fileMapper);
}
return results;
};
}