UNPKG

electron-builder

Version:

A complete solution to package and build a ready for distribution Electron app for MacOS, Windows and Linux with “auto update” support out of the box

197 lines (178 loc) 9.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinuxTargetHelper = exports.installPrefix = undefined; var _bluebirdLstC; function _load_bluebirdLstC() { return _bluebirdLstC = require("bluebird-lst-c"); } var _bluebirdLstC2; function _load_bluebirdLstC2() { return _bluebirdLstC2 = _interopRequireDefault(require("bluebird-lst-c")); } var _fsExtraP; function _load_fsExtraP() { return _fsExtraP = require("fs-extra-p"); } var _path; function _load_path() { return _path = _interopRequireWildcard(require("path")); } var _util; function _load_util() { return _util = require("../util/util"); } function _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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const installPrefix = exports.installPrefix = "/opt"; class LinuxTargetHelper { constructor(packager) { this.packager = packager; this.maxIconPath = null; this.icons = this.computeDesktopIcons(); } // must be name without spaces and other special characters, but not product name used computeDesktopIcons() { var _this = this; return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () { const resourceList = yield _this.packager.resourceList; if (resourceList.indexOf("icons") !== -1) { return _this.iconsFromDir((_path || _load_path()).join(_this.packager.buildResourcesDir, "icons")); } else { return _this.createFromIcns((yield _this.packager.getTempFile("electron-builder-linux.iconset").then(function (it) { return (0, (_fsExtraP || _load_fsExtraP()).ensureDir)(it).thenReturn(it); }))); } })(); } iconsFromDir(iconsDir) { var _this2 = this; return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () { const mappings = []; let maxSize = 0; for (let file of yield (0, (_fsExtraP || _load_fsExtraP()).readdir)(iconsDir)) { if (file.endsWith(".png") || file.endsWith(".PNG")) { // If parseInt encounters a character that is not a numeral in the specified radix, // it returns the integer value parsed up to that point try { const size = parseInt(file, 10); if (size > 0) { const iconPath = `${ iconsDir }/${ file }`; mappings.push([iconPath, `${ size }x${ size }/apps/${ _this2.packager.appInfo.name }.png`]); if (size > maxSize) { maxSize = size; _this2.maxIconPath = iconPath; } } } catch (e) { console.error(e); } } } return mappings; })(); } getIcns() { var _this3 = this; return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () { const build = _this3.packager.devMetadata.build; let iconPath = (build.mac || {}).icon || build.icon; if (iconPath != null && !iconPath.endsWith(".icns")) { iconPath += ".icns"; } return iconPath == null ? yield _this3.packager.getDefaultIcon("icns") : (_path || _load_path()).resolve(_this3.packager.projectDir, iconPath); })(); } computeDesktopEntry(platformSpecificBuildOptions, exec, extra) { var _this4 = this; return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () { const appInfo = _this4.packager.appInfo; const productFilename = appInfo.productFilename; const tempFile = yield _this4.packager.getTempFile(`${ productFilename }.desktop`); const desktopMeta = Object.assign({ Name: appInfo.productName, Comment: platformSpecificBuildOptions.description || appInfo.description, Exec: exec == null ? `"${ installPrefix }/${ productFilename }/${ _this4.packager.executableName }"` : exec, Terminal: "false", Type: "Application", Icon: appInfo.name }, extra, platformSpecificBuildOptions.desktop); const category = platformSpecificBuildOptions.category; if (!(0, (_util || _load_util()).isEmptyOrSpaces)(category)) { desktopMeta.Categories = category; } let data = `[Desktop Entry]`; for (let name of Object.keys(desktopMeta)) { const value = desktopMeta[name]; data += `\n${ name }=${ value }`; } data += "\n"; yield (0, (_fsExtraP || _load_fsExtraP()).outputFile)(tempFile, data); return tempFile; })(); } createFromIcns(tempDir) { var _this5 = this; return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () { const iconPath = yield _this5.getIcns(); if (iconPath == null) { return _this5.iconsFromDir((_path || _load_path()).join(__dirname, "..", "..", "templates", "linux", "electron-icons")); } if (process.platform === "darwin") { yield (0, (_util || _load_util()).exec)("iconutil", ["--convert", "iconset", "--output", tempDir, iconPath]); const iconFiles = yield (0, (_fsExtraP || _load_fsExtraP()).readdir)(tempDir); const imagePath = iconFiles.indexOf("icon_512x512.png") !== -1 ? (_path || _load_path()).join(tempDir, "icon_512x512.png") : (_path || _load_path()).join(tempDir, "icon_256x256.png"); _this5.maxIconPath = imagePath; function resize(size) { const filename = `icon_${ size }x${ size }.png`; if (iconFiles.indexOf(filename) !== -1) { return (_bluebirdLstC2 || _load_bluebirdLstC2()).default.resolve(); } const sizeArg = `${ size }x${ size }`; return (0, (_util || _load_util()).exec)("gm", ["convert", "-size", sizeArg, imagePath, "-resize", sizeArg, (_path || _load_path()).join(tempDir, filename)]); } const promises = [resize(24), resize(96)]; promises.push(resize(16)); promises.push(resize(48)); promises.push(resize(64)); promises.push(resize(128)); yield (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all(promises); return _this5.createMappings(tempDir); } else { const output = yield (0, (_util || _load_util()).exec)("icns2png", ["-x", "-o", tempDir, iconPath]); (0, (_util || _load_util()).debug)(output); //noinspection UnnecessaryLocalVariableJS const imagePath = (_path || _load_path()).join(tempDir, "icon_256x256x32.png"); _this5.maxIconPath = imagePath; function resize(size) { const sizeArg = `${ size }x${ size }`; return (0, (_util || _load_util()).exec)("gm", ["convert", "-size", sizeArg, imagePath, "-resize", sizeArg, (_path || _load_path()).join(tempDir, `icon_${ size }x${ size }x32.png`)]); } const promises = [resize(24), resize(96)]; if (!(output.indexOf("is32") !== -1)) { promises.push(resize(16)); } if (!(output.indexOf("ih32") !== -1)) { promises.push(resize(48)); } if (!(output.toString().indexOf("icp6") !== -1)) { promises.push(resize(64)); } if (!(output.indexOf("it32") !== -1)) { promises.push(resize(128)); } yield (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all(promises); return _this5.createMappings(tempDir); } })(); } createMappings(tempDir) { const appName = this.packager.appInfo.name; function createMapping(size) { return [process.platform === "darwin" ? `${ tempDir }/icon_${ size }x${ size }.png` : `${ tempDir }/icon_${ size }x${ size }x32.png`, `${ size }x${ size }/apps/${ appName }.png`]; } return [createMapping("16"), createMapping("24"), createMapping("32"), createMapping("48"), createMapping("64"), createMapping("96"), createMapping("128"), createMapping("256"), createMapping("512")]; } } exports.LinuxTargetHelper = LinuxTargetHelper; //# sourceMappingURL=LinuxTargetHelper.js.map