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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createElectronFrameworkSupport = exports.createBrandingOpts = 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 lazy_val_1 = require("lazy-val");
const path = require("path");
const index_1 = require("../index");
const platformPackager_1 = require("../platformPackager");
const pathManager_1 = require("../util/pathManager");
const electronMac_1 = require("./electronMac");
const electronVersion_1 = require("./electronVersion");
const fs = require("fs/promises");
function createBrandingOpts(opts) {
    var _a, _b;
    return {
        projectName: ((_a = opts.electronBranding) === null || _a === void 0 ? void 0 : _a.projectName) || "electron",
        productName: ((_b = opts.electronBranding) === null || _b === void 0 ? void 0 : _b.productName) || "Electron",
    };
}
exports.createBrandingOpts = createBrandingOpts;
function createDownloadOpts(opts, platform, arch, electronVersion) {
    return {
        platform,
        arch,
        version: electronVersion,
        ...opts.electronDownload,
    };
}
async function beforeCopyExtraFiles(options) {
    const packager = options.packager;
    const appOutDir = options.appOutDir;
    const electronBranding = createBrandingOpts(packager.config);
    if (packager.platform === index_1.Platform.LINUX) {
        if (!platformPackager_1.isSafeToUnpackElectronOnRemoteBuildServer(packager)) {
            const linuxPackager = packager;
            const executable = path.join(appOutDir, linuxPackager.executableName);
            await fs_extra_1.rename(path.join(appOutDir, electronBranding.projectName), executable);
        }
    }
    else if (packager.platform === index_1.Platform.WINDOWS) {
        const executable = path.join(appOutDir, `${packager.appInfo.productFilename}.exe`);
        await fs_extra_1.rename(path.join(appOutDir, `${electronBranding.projectName}.exe`), executable);
    }
    else {
        await electronMac_1.createMacApp(packager, appOutDir, options.asarIntegrity, options.platformName === "mas");
        const wantedLanguages = builder_util_1.asArray(packager.platformSpecificBuildOptions.electronLanguages);
        if (wantedLanguages.length === 0) {
            return;
        }
        // noinspection SpellCheckingInspection
        const langFileExt = ".lproj";
        const resourcesDir = packager.getResourcesDir(appOutDir);
        await bluebird_lst_1.default.map(fs_extra_1.readdir(resourcesDir), file => {
            if (!file.endsWith(langFileExt)) {
                return;
            }
            const language = file.substring(0, file.length - langFileExt.length);
            if (!wantedLanguages.includes(language)) {
                return fs.rm(path.join(resourcesDir, file), { recursive: true, force: true });
            }
            return;
        }, fs_1.CONCURRENCY);
    }
}
class ElectronFramework {
    constructor(name, version, distMacOsAppName) {
        this.name = name;
        this.version = version;
        this.distMacOsAppName = distMacOsAppName;
        // noinspection JSUnusedGlobalSymbols
        this.macOsDefaultTargets = ["zip", "dmg"];
        // noinspection JSUnusedGlobalSymbols
        this.defaultAppIdPrefix = "com.electron.";
        // noinspection JSUnusedGlobalSymbols
        this.isCopyElevateHelper = true;
        // noinspection JSUnusedGlobalSymbols
        this.isNpmRebuildRequired = true;
    }
    getDefaultIcon(platform) {
        if (platform === index_1.Platform.LINUX) {
            return path.join(pathManager_1.getTemplatePath("icons"), "electron-linux");
        }
        else {
            // default icon is embedded into app skeleton
            return null;
        }
    }
    prepareApplicationStageDirectory(options) {
        return unpack(options, createDownloadOpts(options.packager.config, options.platformName, options.arch, this.version), this.distMacOsAppName);
    }
    beforeCopyExtraFiles(options) {
        return beforeCopyExtraFiles(options);
    }
}
async function createElectronFrameworkSupport(configuration, packager) {
    let version = configuration.electronVersion;
    if (version == null) {
        // for prepacked app asar no dev deps in the app.asar
        if (packager.isPrepackedAppAsar) {
            version = await electronVersion_1.getElectronVersionFromInstalled(packager.projectDir);
            if (version == null) {
                throw new Error(`Cannot compute electron version for prepacked asar`);
            }
        }
        else {
            version = await electronVersion_1.computeElectronVersion(packager.projectDir, new lazy_val_1.Lazy(() => Promise.resolve(packager.metadata)));
        }
        configuration.electronVersion = version;
    }
    const branding = createBrandingOpts(configuration);
    return new ElectronFramework(branding.projectName, version, `${branding.productName}.app`);
}
exports.createElectronFrameworkSupport = createElectronFrameworkSupport;
async function unpack(prepareOptions, options, distMacOsAppName) {
    const { packager, appOutDir, platformName } = prepareOptions;
    const electronDist = packager.config.electronDist;
    let dist = typeof electronDist === "function" ? electronDist(prepareOptions) : electronDist;
    if (dist != null) {
        const zipFile = `electron-v${options.version}-${platformName}-${options.arch}.zip`;
        const resolvedDist = path.isAbsolute(dist) ? dist : path.resolve(packager.projectDir, dist);
        if ((await fs_1.statOrNull(path.join(resolvedDist, zipFile))) != null) {
            builder_util_1.log.info({ resolvedDist, zipFile }, "Resolved electronDist");
            options.cache = resolvedDist;
            dist = null;
        }
    }
    let isFullCleanup = false;
    if (dist == null) {
        if (platformPackager_1.isSafeToUnpackElectronOnRemoteBuildServer(packager)) {
            return;
        }
        await builder_util_1.executeAppBuilder(["unpack-electron", "--configuration", JSON.stringify([options]), "--output", appOutDir, "--distMacOsAppName", distMacOsAppName]);
    }
    else {
        isFullCleanup = true;
        const source = packager.getElectronSrcDir(dist);
        const destination = packager.getElectronDestinationDir(appOutDir);
        builder_util_1.log.info({ source, destination }, "copying Electron");
        await fs_extra_1.emptyDir(appOutDir);
        await fs_1.copyDir(source, destination, {
            isUseHardLink: fs_1.DO_NOT_USE_HARD_LINKS,
        });
    }
    await cleanupAfterUnpack(prepareOptions, distMacOsAppName, isFullCleanup);
}
function cleanupAfterUnpack(prepareOptions, distMacOsAppName, isFullCleanup) {
    const out = prepareOptions.appOutDir;
    const isMac = prepareOptions.packager.platform === index_1.Platform.MAC;
    const resourcesPath = isMac ? path.join(out, distMacOsAppName, "Contents", "Resources") : path.join(out, "resources");
    return Promise.all([
        isFullCleanup ? fs_1.unlinkIfExists(path.join(resourcesPath, "default_app.asar")) : Promise.resolve(),
        isFullCleanup ? fs_1.unlinkIfExists(path.join(out, "version")) : Promise.resolve(),
        isMac
            ? Promise.resolve()
            : fs_extra_1.rename(path.join(out, "LICENSE"), path.join(out, "LICENSE.electron.txt")).catch(() => {
                /* ignore */
            }),
    ]);
}
//# sourceMappingURL=ElectronFramework.js.map