UNPKG

5.67 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.isLibOrExe = isLibOrExe;
7exports.detectUnpackedDirs = void 0;
8
9function _bluebirdLst() {
10 const data = _interopRequireWildcard(require("bluebird-lst"));
11
12 _bluebirdLst = function () {
13 return data;
14 };
15
16 return data;
17}
18
19function _builderUtil() {
20 const data = require("builder-util");
21
22 _builderUtil = function () {
23 return data;
24 };
25
26 return data;
27}
28
29function _fs() {
30 const data = require("builder-util/out/fs");
31
32 _fs = function () {
33 return data;
34 };
35
36 return data;
37}
38
39function _fsExtraP() {
40 const data = require("fs-extra-p");
41
42 _fsExtraP = function () {
43 return data;
44 };
45
46 return data;
47}
48
49var path = _interopRequireWildcard(require("path"));
50
51function _fileTransformer() {
52 const data = require("../fileTransformer");
53
54 _fileTransformer = function () {
55 return data;
56 };
57
58 return data;
59}
60
61function _appFileCopier() {
62 const data = require("../util/appFileCopier");
63
64 _appFileCopier = function () {
65 return data;
66 };
67
68 return data;
69}
70
71function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
72
73const isBinaryFile = _bluebirdLst().default.promisify(require("isbinaryfile"));
74
75function addValue(map, key, value) {
76 let list = map.get(key);
77
78 if (list == null) {
79 list = [value];
80 map.set(key, list);
81 } else {
82 list.push(value);
83 }
84}
85
86function isLibOrExe(file) {
87 return file.endsWith(".dll") || file.endsWith(".exe") || file.endsWith(".dylib") || file.endsWith(".so");
88}
89/** @internal */
90
91
92let detectUnpackedDirs = (() => {
93 var _ref = (0, _bluebirdLst().coroutine)(function* (fileSet, autoUnpackDirs, unpackedDest, rootForAppFilesWithoutAsar) {
94 const dirToCreate = new Map();
95 const metadata = fileSet.metadata;
96
97 function addParents(child, root) {
98 child = path.dirname(child);
99
100 if (autoUnpackDirs.has(child)) {
101 return;
102 }
103
104 do {
105 autoUnpackDirs.add(child);
106 const p = path.dirname(child); // create parent dir to be able to copy file later without directory existence check
107
108 addValue(dirToCreate, p, path.basename(child));
109
110 if (child === root || p === root || autoUnpackDirs.has(p)) {
111 break;
112 }
113
114 child = p;
115 } while (true);
116
117 autoUnpackDirs.add(root);
118 }
119
120 for (let i = 0, n = fileSet.files.length; i < n; i++) {
121 const file = fileSet.files[i];
122 const index = file.lastIndexOf(_fileTransformer().NODE_MODULES_PATTERN);
123
124 if (index < 0) {
125 continue;
126 }
127
128 let nextSlashIndex = file.indexOf(path.sep, index + _fileTransformer().NODE_MODULES_PATTERN.length + 1);
129
130 if (nextSlashIndex < 0) {
131 continue;
132 }
133
134 if (file[index + _fileTransformer().NODE_MODULES_PATTERN.length] === "@") {
135 nextSlashIndex = file.indexOf(path.sep, nextSlashIndex + 1);
136 }
137
138 if (!metadata.get(file).isFile()) {
139 continue;
140 }
141
142 const packageDir = file.substring(0, nextSlashIndex);
143 const packageDirPathInArchive = path.relative(rootForAppFilesWithoutAsar, (0, _appFileCopier().getDestinationPath)(packageDir, fileSet));
144 const pathInArchive = path.relative(rootForAppFilesWithoutAsar, (0, _appFileCopier().getDestinationPath)(file, fileSet));
145
146 if (autoUnpackDirs.has(packageDirPathInArchive)) {
147 // if package dir is unpacked, any file also unpacked
148 addParents(pathInArchive, packageDirPathInArchive);
149 continue;
150 } // https://github.com/electron-userland/electron-builder/issues/2679
151
152
153 let shouldUnpack = false;
154
155 if (isLibOrExe(file)) {
156 shouldUnpack = true;
157 } else if (!file.includes(".", nextSlashIndex) && path.extname(file) === "") {
158 shouldUnpack = yield isBinaryFile(file);
159 }
160
161 if (!shouldUnpack) {
162 continue;
163 }
164
165 if (_builderUtil().log.isDebugEnabled) {
166 _builderUtil().log.debug({
167 file: pathInArchive,
168 reason: "contains executable code"
169 }, "not packed into asar archive");
170 }
171
172 addParents(pathInArchive, packageDirPathInArchive);
173 }
174
175 if (dirToCreate.size > 0) {
176 yield (0, _fsExtraP().ensureDir)(unpackedDest + path.sep + "node_modules"); // child directories should be not created asynchronously - parent directories should be created first
177
178 yield _bluebirdLst().default.map(dirToCreate.keys(), (() => {
179 var _ref2 = (0, _bluebirdLst().coroutine)(function* (parentDir) {
180 const base = unpackedDest + path.sep + parentDir;
181 yield (0, _fsExtraP().ensureDir)(base);
182 yield _bluebirdLst().default.each(dirToCreate.get(parentDir), it => {
183 if (dirToCreate.has(parentDir + path.sep + it)) {
184 // already created
185 return null;
186 } else {
187 return (0, _fsExtraP().ensureDir)(base + path.sep + it);
188 }
189 });
190 });
191
192 return function (_x5) {
193 return _ref2.apply(this, arguments);
194 };
195 })(), _fs().CONCURRENCY);
196 }
197 });
198
199 return function detectUnpackedDirs(_x, _x2, _x3, _x4) {
200 return _ref.apply(this, arguments);
201 };
202})(); exports.detectUnpackedDirs = detectUnpackedDirs;
203//# sourceMappingURL=unpackDetector.js.map
\No newline at end of file