-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
NodeModuleCopyHelper.ts
146 lines (129 loc) · 4.86 KB
/
NodeModuleCopyHelper.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import BluebirdPromise from "bluebird-lst"
import { CONCURRENCY } from "builder-util/out/fs"
import { lstat, readdir } from "fs-extra"
import * as path from "path"
import { excludedNames, FileMatcher } from "../fileMatcher"
import { Packager } from "../packager"
import { resolveFunction } from "../platformPackager"
import { FileCopyHelper } from "./AppFileWalker"
const excludedFiles = new Set(
[".DS_Store", "node_modules" /* already in the queue */, "CHANGELOG.md", "ChangeLog", "changelog.md", "Changelog.md", "Changelog", "binding.gyp", ".npmignore"].concat(
excludedNames.split(",")
)
)
const topLevelExcludedFiles = new Set([
"karma.conf.js",
".coveralls.yml",
"README.md",
"readme.markdown",
"README",
"readme.md",
"Readme.md",
"Readme",
"readme",
"test",
"tests",
"__tests__",
"powered-test",
"example",
"examples",
".bin",
])
/** @internal */
export class NodeModuleCopyHelper extends FileCopyHelper {
constructor(matcher: FileMatcher, packager: Packager) {
super(matcher, matcher.isEmpty() ? null : matcher.createFilter(), packager)
}
async collectNodeModules(baseDir: string, moduleNames: Iterable<string>, nodeModuleExcludedExts: Array<string>): Promise<Array<string>> {
const filter = this.filter
const metadata = this.metadata
const onNodeModuleFile = await resolveFunction(this.packager.appInfo.type, this.packager.config.onNodeModuleFile, "onNodeModuleFile")
const result: Array<string> = []
const queue: Array<string> = []
for (const moduleName of moduleNames) {
const tmpPath = baseDir + path.sep + moduleName
queue.length = 1
// The path should be corrected in Windows that when the moduleName is Scoped packages named.
const depPath = path.normalize(tmpPath)
queue[0] = depPath
while (queue.length > 0) {
const dirPath = queue.pop()!
const childNames = await readdir(dirPath)
childNames.sort()
const isTopLevel = dirPath === depPath
const dirs: Array<string> = []
// our handler is async, but we should add sorted files, so, we add file to result not in the mapper, but after map
const sortedFilePaths = await BluebirdPromise.map(
childNames,
name => {
const filePath = dirPath + path.sep + name
const forceIncluded = onNodeModuleFile != null && !!onNodeModuleFile(filePath)
if (excludedFiles.has(name) || name.startsWith("._")) {
return null
}
if (!forceIncluded) {
for (const ext of nodeModuleExcludedExts) {
if (name.endsWith(ext)) {
return null
}
}
// noinspection SpellCheckingInspection
if (isTopLevel && (topLevelExcludedFiles.has(name) || (moduleName === "libui-node" && (name === "build" || name === "docs" || name === "src")))) {
return null
}
if (dirPath.endsWith("build")) {
if (name === "gyp-mac-tool" || name === "Makefile" || name.endsWith(".mk") || name.endsWith(".gypi") || name.endsWith(".Makefile")) {
return null
}
} else if (dirPath.endsWith("Release") && (name === ".deps" || name === "obj.target")) {
return null
} else if (name === "src" && (dirPath.endsWith("keytar") || dirPath.endsWith("keytar-prebuild"))) {
return null
} else if (dirPath.endsWith("lzma-native") && (name === "build" || name === "deps")) {
return null
}
}
return lstat(filePath).then(stat => {
if (filter != null && !filter(filePath, stat)) {
return null
}
if (!stat.isDirectory()) {
metadata.set(filePath, stat)
}
const consumerResult = this.handleFile(filePath, dirPath, stat)
if (consumerResult == null) {
if (stat.isDirectory()) {
dirs.push(name)
return null
} else {
return filePath
}
} else {
return consumerResult.then(it => {
// asarUtil can return modified stat (symlink handling)
if ((it == null ? stat : it).isDirectory()) {
dirs.push(name)
return null
} else {
return filePath
}
})
}
})
},
CONCURRENCY
)
for (const child of sortedFilePaths) {
if (child != null) {
result.push(child)
}
}
dirs.sort()
for (const child of dirs) {
queue.push(dirPath + path.sep + child)
}
}
}
return result
}
}