forked from sverweij/dependency-cruiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather-initial-sources.js
119 lines (111 loc) · 3.89 KB
/
gather-initial-sources.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const get = require("lodash/get");
const filenameMatchesPattern =
require("../graph-utl/match-facade").filenameMatchesPattern;
const getExtension = require("../utl/get-extension");
const pathToPosix = require("../utl/path-to-posix");
const transpileMeta = require("./transpile/meta");
/**
*
* @param {import('../../types/options').IStrictCruiseOptions} pOptions
* @returns {string[]}
*/
function getScannableExtensions(pOptions) {
return transpileMeta.scannableExtensions.concat(
pOptions.extraExtensionsToScan
);
}
function fileIsScannable(pOptions, pPathToFile) {
return getScannableExtensions(pOptions).includes(getExtension(pPathToFile));
}
function shouldBeIncluded(pFullPathToFile, pOptions) {
return (
!get(pOptions, "includeOnly.path") ||
filenameMatchesPattern(pFullPathToFile, pOptions.includeOnly.path)
);
}
function shouldNotBeExcluded(pFullPathToFile, pOptions) {
return (
(!get(pOptions, "exclude.path") ||
!filenameMatchesPattern(pFullPathToFile, pOptions.exclude.path)) &&
(!get(pOptions, "doNotFollow.path") ||
!filenameMatchesPattern(pFullPathToFile, pOptions.doNotFollow.path))
);
}
/**
*
* @param {string} pDirectoryName
* @param {import('../../types/options').IStrictCruiseOptions} pOptions options that
* @returns {string[]}
*/
function gatherScannableFilesFromDirectory(pDirectoryName, pOptions) {
return fs
.readdirSync(path.join(pOptions.baseDir, pDirectoryName))
.map((pFileName) => path.join(pDirectoryName, pFileName))
.filter((pFullPathToFile) =>
shouldNotBeExcluded(pathToPosix(pFullPathToFile), pOptions)
)
.reduce((pSum, pFullPathToFile) => {
let lStat = fs.statSync(path.join(pOptions.baseDir, pFullPathToFile), {
throwIfNoEntry: false,
});
if (lStat) {
if (lStat.isDirectory()) {
return pSum.concat(
gatherScannableFilesFromDirectory(pFullPathToFile, pOptions)
);
}
if (fileIsScannable(pOptions, pFullPathToFile)) {
return pSum.concat(pFullPathToFile);
}
}
return pSum;
}, [])
.map((pFullPathToFile) => pathToPosix(pFullPathToFile))
.filter((pFullPathToFile) => shouldBeIncluded(pFullPathToFile, pOptions));
}
/**
* Returns an array of strings, representing paths to files to be gathered
*
* If an entry in the array passed is a (path to a) directory, it recursively
* scans that directory for files with a scannable extension.
* If an entry is a path to a file it just adds it.
*
* Files and directories are assumed to be either absolute, or relative to the
* current working directory.
*
* @param {string[]} pFileAndDirectoryArray globs and/ or paths to files or
* directories to be gathered
* @param {import('../..').IStrictCruiseOptions} pOptions options that
* influence what needs to be gathered/ scanned
* notably useful attributes:
* - exclude - regexp of what to exclude
* - includeOnly - regexp what to include
* @return {string[]} paths to files to be gathered.
*/
module.exports = function gatherInitialSources(
pFileAndDirectoryArray,
pOptions
) {
const lOptions = { baseDir: process.cwd(), ...pOptions };
return pFileAndDirectoryArray
.reduce(
(pAll, pFileOrDirectory) =>
pAll.concat(glob.sync(pFileOrDirectory, { cwd: lOptions.baseDir })),
[]
)
.reduce((pAll, pFileOrDirectory) => {
if (
fs.statSync(path.join(lOptions.baseDir, pFileOrDirectory)).isDirectory()
) {
return pAll.concat(
gatherScannableFilesFromDirectory(pFileOrDirectory, lOptions)
);
} else {
return pAll.concat(pathToPosix(pFileOrDirectory));
}
}, [])
.sort();
};