UNPKG

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