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

313 lines (282 loc) 14.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.attachAndExecute = exports.DmgTarget = undefined; var _bluebirdLstC; function _load_bluebirdLstC() { return _bluebirdLstC = require("bluebird-lst-c"); } var _bluebirdLstC2; function _load_bluebirdLstC2() { return _bluebirdLstC2 = _interopRequireDefault(require("bluebird-lst-c")); } let detach = (() => { var _ref2 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (name) { try { yield (0, (_util || _load_util()).exec)("hdiutil", ["detach", name]); } catch (e) { yield new (_bluebirdLstC2 || _load_bluebirdLstC2()).default(function (resolve, reject) { setTimeout(function () { (0, (_util || _load_util()).exec)("hdiutil", ["detach", "-force", name]).then(resolve).catch(reject); }, 1000); }); } }); return function detach(_x) { return _ref2.apply(this, arguments); }; })(); let attachAndExecute = exports.attachAndExecute = (() => { var _ref3 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (dmgPath, readWrite, task) { //noinspection SpellCheckingInspection const args = ["attach", "-noverify", "-noautoopen"]; if (readWrite) { args.push("-readwrite"); } // otherwise hangs // addVerboseIfNeed(args) args.push(dmgPath); const attachResult = yield (0, (_util || _load_util()).exec)("hdiutil", args, { maxBuffer: 2 * 1024 * 1024 }); const deviceResult = attachResult == null ? null : /^(\/dev\/\w+)/.exec(attachResult); const device = deviceResult == null || deviceResult.length !== 2 ? null : deviceResult[1]; if (device == null) { throw new Error(`Cannot mount: ${ attachResult }`); } yield (0, (_promise || _load_promise()).executeFinally)(task(), function () { return detach(device); }); }); return function attachAndExecute(_x2, _x3, _x4) { return _ref3.apply(this, arguments); }; })(); var _deepAssign; function _load_deepAssign() { return _deepAssign = require("../util/deepAssign"); } var _path; function _load_path() { return _path = _interopRequireWildcard(require("path")); } var _log; function _load_log() { return _log = require("../util/log"); } var _platformPackager; function _load_platformPackager() { return _platformPackager = require("../platformPackager"); } var _util; function _load_util() { return _util = require("../util/util"); } var _fsExtraP; function _load_fsExtraP() { return _fsExtraP = require("fs-extra-p"); } var _promise; function _load_promise() { return _promise = require("../util/promise"); } var _sanitizeFilename; function _load_sanitizeFilename() { return _sanitizeFilename = _interopRequireDefault(require("sanitize-filename")); } 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 }; } class DmgTarget extends (_platformPackager || _load_platformPackager()).TargetEx { constructor(packager) { super("dmg"); this.packager = packager; this.helperDir = (_path || _load_path()).join(__dirname, "..", "..", "templates", "dmg"); } build(appOutDir, arch) { var _this = this; return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () { const packager = _this.packager; const appInfo = packager.appInfo; (0, (_log || _load_log()).log)("Building DMG"); const specification = yield _this.computeDmgOptions(); const tempDir = yield packager.getTempFile("dmg"); const tempDmg = (_path || _load_path()).join(tempDir, "temp.dmg"); const backgroundDir = (_path || _load_path()).join(tempDir, ".background"); const backgroundFilename = specification.background == null ? null : (_path || _load_path()).basename(specification.background); if (backgroundFilename != null) { yield (0, (_fsExtraP || _load_fsExtraP()).copy)((_path || _load_path()).resolve(packager.info.projectDir, specification.background), (_path || _load_path()).join(backgroundDir, backgroundFilename)); } let preallocatedSize = 32 * 1024; if (specification.icon != null) { const stat = yield (0, (_util || _load_util()).statOrNull)(specification.icon); if (stat != null) { preallocatedSize += stat.size; } } // allocate space for .DS_Store yield (0, (_fsExtraP || _load_fsExtraP()).outputFile)((_path || _load_path()).join(backgroundDir, "DSStorePlaceHolder"), new Buffer(preallocatedSize)); const volumeName = (0, (_sanitizeFilename || _load_sanitizeFilename()).default)(_this.computeVolumeName(specification.title)); //noinspection SpellCheckingInspection yield (0, (_util || _load_util()).spawn)("hdiutil", addVerboseIfNeed(["create", "-srcfolder", backgroundDir, "-srcfolder", (_path || _load_path()).join(appOutDir, `${ packager.appInfo.productFilename }.app`), "-volname", volumeName, "-anyowners", "-nospotlight", "-fs", "HFS+", "-fsargs", "-c c=64,a=16,e=16", "-format", "UDRW"]).concat(tempDmg)); const volumePath = (_path || _load_path()).join("/Volumes", volumeName); if (yield (0, (_util || _load_util()).exists)(volumePath)) { (0, (_util || _load_util()).debug)("Unmounting previous disk image"); yield detach(volumePath); } yield attachAndExecute(tempDmg, true, (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () { const promises = [specification.background == null ? (0, (_fsExtraP || _load_fsExtraP()).remove)(`${ volumePath }/.background`) : (0, (_fsExtraP || _load_fsExtraP()).unlink)(`${ volumePath }/.background/DSStorePlaceHolder`), (0, (_util || _load_util()).exec)("ln", ["-s", "/Applications", `${ volumePath }/Applications`])]; let contents = specification.contents; if (contents == null) { contents = [{ "x": 130, "y": 220 }, { "x": 410, "y": 220, "type": "link", "path": "/Applications" }]; } let location = contents.find(function (it) { return it.path == null && it.type !== "link"; }); if (location == null) { location = contents.find(function (it) { if (it.path != null && it.path.endsWith(".app") && it.type !== "link") { (0, (_log || _load_log()).warn)(`Do not specify path for application: "${ it.path }". Actual path to app will be used instead.`); return true; } return false; }); } const applicationsLocation = contents.find(function (it) { return it.type === "link" && (it.path === "/Applications" || it.path === "Applications"); }); const window = specification.window; const env = Object.assign({}, process.env, { volumePath: volumePath, appFileName: `${ packager.appInfo.productFilename }.app`, appFileX: location.x, appFileY: location.y, APPLICATIONS_LINK_X: applicationsLocation.x, APPLICATIONS_LINK_Y: applicationsLocation.y, iconSize: specification.iconSize || 80, iconTextSize: specification.iconTextSize || 12, windowX: window.x, windowY: window.y, VERSIONER_PERL_PREFER_32_BIT: "true" }); if (specification.icon == null) { delete env.volumeIcon; } else { const volumeIcon = `${ volumePath }/.VolumeIcon.icns`; promises.push((0, (_fsExtraP || _load_fsExtraP()).copy)((_path || _load_path()).resolve(packager.projectDir, specification.icon), volumeIcon)); env.volumeIcon = volumeIcon; } yield (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all(promises); if (specification.backgroundColor != null || specification.background == null) { env.backgroundColor = specification.backgroundColor || "#ffffff"; env.windowWidth = window.width || 540; env.windowHeight = window.height || 380; } else { delete env.backgroundColor; if (window.width == null) { delete env.windowWidth; } else { env.windowWidth = window.width; } if (window.height == null) { delete env.windowHeight; } else { env.windowHeight = window.height; } env.backgroundFilename = backgroundFilename; } yield (0, (_util || _load_util()).exec)("/usr/bin/perl", [(_path || _load_path()).join(_this.helperDir, "dmgProperties.pl")], { cwd: _this.helperDir, env: env }); yield (0, (_util || _load_util()).exec)("sync"); })); const artifactPath = (_path || _load_path()).join(appOutDir, `${ appInfo.productFilename }-${ appInfo.version }.dmg`); //noinspection SpellCheckingInspection yield (0, (_util || _load_util()).spawn)("hdiutil", addVerboseIfNeed(["convert", tempDmg, "-format", packager.devMetadata.build.compression === "store" ? "UDRO" : "UDBZ", "-imagekey", "zlib-level=9", "-o", artifactPath])); yield (0, (_util || _load_util()).exec)("hdiutil", addVerboseIfNeed(["internet-enable", "-no"]).concat(artifactPath)); _this.packager.dispatchArtifactCreated(artifactPath, `${ appInfo.name }-${ appInfo.version }.dmg`); })(); } computeVolumeName(custom) { const appInfo = this.packager.appInfo; if (custom == null) { return `${ appInfo.productFilename } ${ appInfo.version }`; } return custom.replace(/\$\{version}/g, appInfo.version).replace(/\$\{name}/g, appInfo.name).replace(/\$\{productName}/g, appInfo.productName); } // public to test computeDmgOptions() { var _this2 = this; return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () { const packager = _this2.packager; const specification = (0, (_deepAssign || _load_deepAssign()).deepAssign)({ window: { x: 400, y: 100 } }, packager.devMetadata.build.dmg); // appdmg const oldPosition = specification.window.position; if (oldPosition != null) { specification.window.x = oldPosition.x; specification.window.y = oldPosition.y; } const oldSize = specification.window.size; if (oldSize != null) { specification.window.width = oldSize.width; specification.window.height = oldSize.height; } if (specification["icon-size"] != null) { if (specification.iconSize == null) { specification.iconSize = specification["icon-size"]; } (0, (_log || _load_log()).warn)("dmg.icon-size is deprecated, please use dmg.iconSize instead"); } if (!("icon" in specification)) { (0, (_util || _load_util()).use)((yield packager.getIconPath()), function (it) { specification.icon = it; }); } if (specification.icon != null && (0, (_util || _load_util()).isEmptyOrSpaces)(specification.icon)) { throw new Error("dmg.icon cannot be specified as empty string"); } if (specification["background-color"] != null) { if (specification.backgroundColor == null) { specification.backgroundColor = specification["background-color"]; } (0, (_log || _load_log()).warn)("dmg.background-color is deprecated, please use dmg.backgroundColor instead"); } if (specification.backgroundColor != null) { if (specification.background != null) { throw new Error("Both dmg.backgroundColor and dmg.background are specified — please set the only one"); } specification.backgroundColor = require("parse-color")(specification.backgroundColor).hex; } if (specification.backgroundColor == null && !("background" in specification)) { const resourceList = yield packager.resourceList; if (resourceList.indexOf("background.tiff") !== -1) { specification.background = (_path || _load_path()).join(packager.buildResourcesDir, "background.tiff"); } else if (resourceList.indexOf("background.png") !== -1) { specification.background = (_path || _load_path()).join(packager.buildResourcesDir, "background.png"); } else { specification.background = (_path || _load_path()).join(_this2.helperDir, "background.tiff"); } } if (specification.format == null) { specification.format = packager.devMetadata.build.compression === "store" ? "UDRO" : "UDBZ"; } return specification; })(); } } exports.DmgTarget = DmgTarget; function addVerboseIfNeed(args) { if (process.env.DEBUG_DMG === "true") { args.push("-verbose"); } return args; } //# sourceMappingURL=dmg.js.map