/
getFileList.js
52 lines (40 loc) · 1.28 KB
/
getFileList.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
const debug = require('debug')('npm-package-json-lint:getFileList');
const path = require('path');
const globby = require('globby');
/**
* Generates a list of files to lint based on a list of provided patterns
*
* @param {Array<string>} patterns An array of patterns
* @param {string} cwd The current working directory.
* @returns {Array<string>} An array a files to lint.
*/
const getFileList = (patterns, cwd) => {
// step 1 - filter out empty entries
const filteredPatterns = patterns.filter(pattern => pattern.length);
// step 2 - convert directories to globs
const globPatterns = filteredPatterns.map(pattern => {
return pattern.endsWith(`${path.sep}package.json`) ? pattern : `${pattern}${path.sep}package.json`;
});
debug('globPatterns');
debug(globPatterns);
const files = [];
const addedFiles = new Set();
const globFiles = globby.sync(globPatterns, {
cwd,
gitignore: true
});
debug('globFiles');
debug(globFiles);
globFiles.forEach(globFile => {
const filePath = path.resolve(cwd, globFile);
if (addedFiles.has(filePath)) {
return;
}
addedFiles.add(filePath);
files.push(filePath);
});
debug('Final file list from `getFileList`');
debug(files);
return files;
};
module.exports = getFileList;