15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectUnpackedDirs = exports.isLibOrExe = void 0;
const bluebird_lst_1 = require("bluebird-lst");
const builder_util_1 = require("builder-util");
const fs_1 = require("builder-util/out/fs");
const fs_extra_1 = require("fs-extra");
const isbinaryfile_1 = require("isbinaryfile");
const path = require("path");
const fileTransformer_1 = require("../fileTransformer");
const appFileCopier_1 = require("../util/appFileCopier");
function addValue(map, key, value) {
    let list = map.get(key);
    if (list == null) {
        list = [value];
        map.set(key, list);
    }
    else {
        list.push(value);
    }
}
function isLibOrExe(file) {
    return file.endsWith(".dll") || file.endsWith(".exe") || file.endsWith(".dylib") || file.endsWith(".so");
}
exports.isLibOrExe = isLibOrExe;
/** @internal */
async function detectUnpackedDirs(fileSet, autoUnpackDirs, unpackedDest, rootForAppFilesWithoutAsar) {
    const dirToCreate = new Map();
    const metadata = fileSet.metadata;
    function addParents(child, root) {
        child = path.dirname(child);
        if (autoUnpackDirs.has(child)) {
            return;
        }
        do {
            autoUnpackDirs.add(child);
            const p = path.dirname(child);
            // create parent dir to be able to copy file later without directory existence check
            addValue(dirToCreate, p, path.basename(child));
            if (child === root || p === root || autoUnpackDirs.has(p)) {
                break;
            }
            child = p;
        } while (true);
        autoUnpackDirs.add(root);
    }
    for (let i = 0, n = fileSet.files.length; i < n; i++) {
        const file = fileSet.files[i];
        const index = file.lastIndexOf(fileTransformer_1.NODE_MODULES_PATTERN);
        if (index < 0) {
            continue;
        }
        let nextSlashIndex = file.indexOf(path.sep, index + fileTransformer_1.NODE_MODULES_PATTERN.length + 1);
        if (nextSlashIndex < 0) {
            continue;
        }
        if (file[index + fileTransformer_1.NODE_MODULES_PATTERN.length] === "@") {
            nextSlashIndex = file.indexOf(path.sep, nextSlashIndex + 1);
        }
        if (!metadata.get(file).isFile()) {
            continue;
        }
        const packageDir = file.substring(0, nextSlashIndex);
        const packageDirPathInArchive = path.relative(rootForAppFilesWithoutAsar, appFileCopier_1.getDestinationPath(packageDir, fileSet));
        const pathInArchive = path.relative(rootForAppFilesWithoutAsar, appFileCopier_1.getDestinationPath(file, fileSet));
        if (autoUnpackDirs.has(packageDirPathInArchive)) {
            // if package dir is unpacked, any file also unpacked
            addParents(pathInArchive, packageDirPathInArchive);
            continue;
        }
        // https://github.com/electron-userland/electron-builder/issues/2679
        let shouldUnpack = false;
        // ffprobe-static and ffmpeg-static are known packages to always unpack
        const moduleName = path.basename(packageDir);
        if (moduleName === "ffprobe-static" || moduleName === "ffmpeg-static" || isLibOrExe(file)) {
            shouldUnpack = true;
        }
        else if (!file.includes(".", nextSlashIndex)) {
            shouldUnpack = !!isbinaryfile_1.isBinaryFileSync(file);
        }
        if (!shouldUnpack) {
            continue;
        }
        if (builder_util_1.log.isDebugEnabled) {
            builder_util_1.log.debug({ file: pathInArchive, reason: "contains executable code" }, "not packed into asar archive");
        }
        addParents(pathInArchive, packageDirPathInArchive);
    }
    if (dirToCreate.size > 0) {
        await fs_extra_1.mkdir(`${unpackedDest + path.sep}node_modules`, { recursive: true });
        // child directories should be not created asynchronously - parent directories should be created first
        await bluebird_lst_1.default.map(dirToCreate.keys(), async (parentDir) => {
            const base = unpackedDest + path.sep + parentDir;
            await fs_extra_1.mkdir(base, { recursive: true });
            await bluebird_lst_1.default.each(dirToCreate.get(parentDir), (it) => {
                if (dirToCreate.has(parentDir + path.sep + it)) {
                    // already created
                    return null;
                }
                else {
                    return fs_extra_1.mkdir(base + path.sep + it, { recursive: true });
                }
            });
        }, fs_1.CONCURRENCY);
    }
}
exports.detectUnpackedDirs = detectUnpackedDirs;
//# sourceMappingURL=unpackDetector.js.map