This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
src_files.ts
83 lines (75 loc) · 2.28 KB
/
src_files.ts
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
import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js'
import { getPackageJson, PackageJson } from '../../utils/package_json.js'
import { getNewCache, TraversalCache } from '../../utils/traversal_cache.js'
import type { GetSrcFilesFunction } from '../types.js'
import { getDependencyPathsForDependency } from '../zisi/traverse.js'
export const getSrcFiles: GetSrcFilesFunction = async ({ config, mainFile, pluginsModulesPath, srcDir }) => {
const { externalNodeModules = [], includedFiles = [], includedFilesBasePath } = config
const { excludePatterns, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
includedFilesBasePath,
)
const dependencyPaths = await getSrcFilesForDependencies({
dependencies: externalNodeModules,
basedir: srcDir,
pluginsModulesPath,
})
const srcFiles = filterExcludedPaths(dependencyPaths, excludePatterns)
const includedPaths = filterExcludedPaths(includedFilePaths, excludePatterns)
return {
srcFiles: [...srcFiles, ...includedPaths, mainFile],
includedFiles: includedPaths,
}
}
const getSrcFilesForDependencies = async function ({
dependencies: dependencyNames,
basedir,
state = getNewCache(),
pluginsModulesPath,
}: {
dependencies: string[]
basedir: string
state?: TraversalCache
pluginsModulesPath?: string
}) {
if (dependencyNames.length === 0) {
return []
}
const packageJson = await getPackageJson(basedir)
const dependencies = await Promise.all(
dependencyNames.map((dependencyName) =>
getSrcFilesForDependency({
dependency: dependencyName,
basedir,
state,
packageJson,
pluginsModulesPath,
}),
),
)
const paths = new Set(dependencies.flat())
return [...paths]
}
const getSrcFilesForDependency = async function ({
dependency,
basedir,
state = getNewCache(),
packageJson,
pluginsModulesPath,
}: {
dependency: string
basedir: string
state: TraversalCache
packageJson: PackageJson
pluginsModulesPath?: string
}) {
try {
const paths = await getDependencyPathsForDependency({ dependency, basedir, state, packageJson, pluginsModulesPath })
return paths
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
return []
}
throw error
}
}