-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_directory.js
75 lines (67 loc) · 2.57 KB
/
source_directory.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
import { join } from "node:path"
import { readdirSync } from "node:fs"
// eslint-disable-next-line no-unused-vars
import AbstractExternalPackage from "../abstract_external_package"
import AbstractSourceFile from "../abstract_source_file"
// eslint-disable-next-line no-unused-vars
import CommonInfo from "../common_info"
// eslint-disable-next-line no-unused-vars
import RelativePathPairBuilder from "./relative_path_pair_builder"
import UnnamedSourceFile from "./unnamed_source_file"
/**
* Represent a collection source files within the directory.
*
* It will search the files using the input directory from common information.
*/
export default class SourceDirectory extends AbstractSourceFile {
/**
* Creates a representation of collection of files.
* @param {CommonInfo} commonInfo Common information needed to bundle the files.
* @param {any[]|((RelativePathPair) => any[])} plugins Array of common plugins to bundle the
* source or function that receives the
* relative path pair from builder.
* @param {AbstractExternalPackage[]} externals Array of common external packages.
* @param {RelativePathPairBuilder} pathPairBuilder Class to create relative paths.
*/
constructor(commonInfo, plugins, externals, pathPairBuilder) {
super()
this._sourceFiles = []
this._externals = externals
const { inputDirectory } = commonInfo
const directories = [ inputDirectory ]
while (directories.length > 0) {
const currentDirectory = directories.shift()
readdirSync(currentDirectory, { "withFileTypes": true }).forEach(relativePath => {
const relativePathToInput = join(
currentDirectory.slice(inputDirectory.length === 0 ? 0 : inputDirectory.length + 1),
relativePath.name
)
const pathPair = pathPairBuilder.build(relativePathToInput, relativePathToInput)
if (relativePath.isFile()) {
const sourceFile = new UnnamedSourceFile(
commonInfo,
pathPair,
typeof plugins === "function"
? plugins(pathPair)
: plugins,
externals
)
this._sourceFiles.push(sourceFile)
} else {
directories.push(join(currentDirectory, relativePath.name))
}
})
}
}
toConfigurationArray() {
const configurations = []
for (const sourceFile of this._sourceFiles) {
const configuration = sourceFile.toOwnConfiguration()
configurations.push(configuration)
}
for (const externalPackage of this._externals) {
configurations.push(...externalPackage.toConfigurationArray())
}
return configurations
}
}