UNPKG

5.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.NodeModuleCopyHelper = void 0;
4const bluebird_lst_1 = require("bluebird-lst");
5const fs_1 = require("builder-util/out/fs");
6const fs_extra_1 = require("fs-extra");
7const path = require("path");
8const fileMatcher_1 = require("../fileMatcher");
9const platformPackager_1 = require("../platformPackager");
10const AppFileWalker_1 = require("./AppFileWalker");
11const excludedFiles = new Set([".DS_Store", "node_modules" /* already in the queue */, "CHANGELOG.md", "ChangeLog", "changelog.md", "Changelog.md", "Changelog", "binding.gyp", ".npmignore"].concat(fileMatcher_1.excludedNames.split(",")));
12const topLevelExcludedFiles = new Set([
13 "test.js",
14 "karma.conf.js",
15 ".coveralls.yml",
16 "README.md",
17 "readme.markdown",
18 "README",
19 "readme.md",
20 "Readme.md",
21 "Readme",
22 "readme",
23 "test",
24 "__tests__",
25 "tests",
26 "powered-test",
27 "example",
28 "examples",
29 ".bin",
30]);
31/** @internal */
32class NodeModuleCopyHelper extends AppFileWalker_1.FileCopyHelper {
33 constructor(matcher, packager) {
34 super(matcher, matcher.isEmpty() ? null : matcher.createFilter(), packager);
35 }
36 async collectNodeModules(baseDir, moduleNames, nodeModuleExcludedExts) {
37 const filter = this.filter;
38 const metadata = this.metadata;
39 const onNodeModuleFile = platformPackager_1.resolveFunction(this.packager.config.onNodeModuleFile, "onNodeModuleFile");
40 const result = [];
41 const queue = [];
42 for (const moduleName of moduleNames) {
43 const tmpPath = baseDir + path.sep + moduleName;
44 queue.length = 1;
45 // The path should be corrected in Windows that when the moduleName is Scoped packages named.
46 const depPath = path.normalize(tmpPath);
47 queue[0] = depPath;
48 while (queue.length > 0) {
49 const dirPath = queue.pop();
50 const childNames = await fs_extra_1.readdir(dirPath);
51 childNames.sort();
52 const isTopLevel = dirPath === depPath;
53 const dirs = [];
54 // our handler is async, but we should add sorted files, so, we add file to result not in the mapper, but after map
55 const sortedFilePaths = await bluebird_lst_1.default.map(childNames, name => {
56 if (onNodeModuleFile != null) {
57 onNodeModuleFile(dirPath + path.sep + name);
58 }
59 if (excludedFiles.has(name) || name.startsWith("._")) {
60 return null;
61 }
62 for (const ext of nodeModuleExcludedExts) {
63 if (name.endsWith(ext)) {
64 return null;
65 }
66 }
67 // noinspection SpellCheckingInspection
68 if (isTopLevel && (topLevelExcludedFiles.has(name) || (moduleName === "libui-node" && (name === "build" || name === "docs" || name === "src")))) {
69 return null;
70 }
71 if (dirPath.endsWith("build")) {
72 if (name === "gyp-mac-tool" || name === "Makefile" || name.endsWith(".mk") || name.endsWith(".gypi") || name.endsWith(".Makefile")) {
73 return null;
74 }
75 }
76 else if (dirPath.endsWith("Release") && (name === ".deps" || name === "obj.target")) {
77 return null;
78 }
79 else if (name === "src" && (dirPath.endsWith("keytar") || dirPath.endsWith("keytar-prebuild"))) {
80 return null;
81 }
82 else if (dirPath.endsWith("lzma-native") && (name === "build" || name === "deps")) {
83 return null;
84 }
85 const filePath = dirPath + path.sep + name;
86 return fs_extra_1.lstat(filePath).then(stat => {
87 if (filter != null && !filter(filePath, stat)) {
88 return null;
89 }
90 if (!stat.isDirectory()) {
91 metadata.set(filePath, stat);
92 }
93 const consumerResult = this.handleFile(filePath, dirPath, stat);
94 if (consumerResult == null) {
95 if (stat.isDirectory()) {
96 dirs.push(name);
97 return null;
98 }
99 else {
100 return filePath;
101 }
102 }
103 else {
104 return consumerResult.then(it => {
105 // asarUtil can return modified stat (symlink handling)
106 if ((it == null ? stat : it).isDirectory()) {
107 dirs.push(name);
108 return null;
109 }
110 else {
111 return filePath;
112 }
113 });
114 }
115 });
116 }, fs_1.CONCURRENCY);
117 for (const child of sortedFilePaths) {
118 if (child != null) {
119 result.push(child);
120 }
121 }
122 dirs.sort();
123 for (const child of dirs) {
124 queue.push(dirPath + path.sep + child);
125 }
126 }
127 }
128 return result;
129 }
130}
131exports.NodeModuleCopyHelper = NodeModuleCopyHelper;
132//# sourceMappingURL=NodeModuleCopyHelper.js.map
\No newline at end of file