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

263 lines (237 loc) 14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildInstaller = 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 buildInstaller = exports.buildInstaller = (() => { var _ref = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (options, outputDirectory, setupExe, packager, appOutDir) { const appUpdate = yield packager.getTempFile("Update.exe"); yield (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all([(0, (_fsExtraP || _load_fsExtraP()).copy)((_path || _load_path()).join(options.vendorPath, "Update.exe"), appUpdate).then(function () { return packager.sign(appUpdate); }), (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all([(0, (_fsExtraP || _load_fsExtraP()).remove)(`${ outputDirectory.replace(/\\/g, "/") }/*-full.nupkg`), (0, (_fsExtraP || _load_fsExtraP()).remove)((_path || _load_path()).join(outputDirectory, "RELEASES"))]).then(function () { return (0, (_fsExtraP || _load_fsExtraP()).ensureDir)(outputDirectory); })]); if (options.remoteReleases) { yield syncReleases(outputDirectory, options); } const embeddedArchiveFile = yield packager.getTempFile("setup.zip"); const embeddedArchive = archiver("zip", { zlib: { level: options.packageCompressionLevel == null ? 6 : options.packageCompressionLevel } }); const embeddedArchiveOut = (0, (_fsExtraP || _load_fsExtraP()).createWriteStream)(embeddedArchiveFile); const embeddedArchivePromise = new (_bluebirdLstC2 || _load_bluebirdLstC2()).default(function (resolve, reject) { embeddedArchive.on("error", reject); embeddedArchiveOut.on("close", resolve); }); embeddedArchive.pipe(embeddedArchiveOut); embeddedArchive.file(appUpdate, { name: "Update.exe" }); embeddedArchive.file(options.loadingGif ? (_path || _load_path()).resolve(options.loadingGif) : (_path || _load_path()).join(__dirname, "..", "..", "templates", "install-spinner.gif"), { name: "background.gif" }); const version = convertVersion(options.version); const packageName = `${ options.name }-${ version }-full.nupkg`; const nupkgPath = (_path || _load_path()).join(outputDirectory, packageName); const setupPath = (_path || _load_path()).join(outputDirectory, setupExe || `${ options.name || options.productName }Setup.exe`); yield (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all([pack(options, appOutDir, appUpdate, nupkgPath, version, options.packageCompressionLevel), (0, (_fsExtraP || _load_fsExtraP()).copy)((_path || _load_path()).join(options.vendorPath, "Setup.exe"), setupPath)]); embeddedArchive.file(nupkgPath, { name: packageName }); const releaseEntry = yield releasify(options, nupkgPath, outputDirectory, packageName); embeddedArchive.append(releaseEntry, { name: "RELEASES" }); embeddedArchive.finalize(); yield embeddedArchivePromise; const writeZipToSetup = (_path || _load_path()).join(options.vendorPath, "WriteZipToSetup.exe"); yield (0, (_util || _load_util()).exec)(process.platform === "win32" ? writeZipToSetup : "wine", prepareArgs([setupPath, embeddedArchiveFile], writeZipToSetup)); yield packager.signAndEditResources(setupPath); if (options.msi && process.platform === "win32") { const outFile = setupExe.replace(".exe", ".msi"); yield msi(options, nupkgPath, setupPath, outputDirectory, outFile); // rcedit can only edit .exe resources yield packager.sign((_path || _load_path()).join(outputDirectory, outFile)); } }); return function buildInstaller(_x, _x2, _x3, _x4, _x5) { return _ref.apply(this, arguments); }; })(); let pack = (() => { var _ref2 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (options, directory, updateFile, outFile, version, packageCompressionLevel) { const archive = archiver("zip", { zlib: { level: packageCompressionLevel == null ? 9 : packageCompressionLevel } }); const archiveOut = (0, (_fsExtraP || _load_fsExtraP()).createWriteStream)(outFile); const archivePromise = new (_bluebirdLstC2 || _load_bluebirdLstC2()).default(function (resolve, reject) { archive.on("error", reject); archiveOut.on("error", reject); archiveOut.on("close", resolve); }); archive.pipe(archiveOut); const author = options.authors || options.owners; const copyright = options.copyright || `Copyright © ${ new Date().getFullYear() } ${ author }`; const nuspecContent = `<?xml version="1.0"?> <package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"> <metadata> <id>${ options.appId }</id> <version>${ version }</version> <title>${ options.productName }</title> <authors>${ author }</authors> <owners>${ options.owners || options.authors }</owners> <iconUrl>${ options.iconUrl }</iconUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>${ options.description }</description> <copyright>${ copyright }</copyright>${ options.extraMetadataSpecs || "" } </metadata> </package>`; (0, (_util || _load_util()).debug)(`Created NuSpec file:\n${ nuspecContent }`); archive.append(nuspecContent.replace(/\n/, "\r\n"), { name: `${ encodeURI(options.name).replace(/%5B/g, "[").replace(/%5D/g, "]") }.nuspec` }); //noinspection SpellCheckingInspection archive.append(`<?xml version="1.0" encoding="utf-8"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Type="http://schemas.microsoft.com/packaging/2010/07/manifest" Target="/${ options.name }.nuspec" Id="Re0" /> <Relationship Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="/package/services/metadata/core-properties/1.psmdcp" Id="Re1" /> </Relationships>`.replace(/\n/, "\r\n"), { name: ".rels", prefix: "_rels" }); //noinspection SpellCheckingInspection archive.append(`<?xml version="1.0" encoding="utf-8"?> <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" /> <Default Extension="nuspec" ContentType="application/octet" /> <Default Extension="pak" ContentType="application/octet" /> <Default Extension="asar" ContentType="application/octet" /> <Default Extension="bin" ContentType="application/octet" /> <Default Extension="dll" ContentType="application/octet" /> <Default Extension="exe" ContentType="application/octet" /> <Default Extension="dat" ContentType="application/octet" /> <Default Extension="psmdcp" ContentType="application/vnd.openxmlformats-package.core-properties+xml" /> <Override PartName="/lib/net45/LICENSE" ContentType="application/octet" /> <Default Extension="diff" ContentType="application/octet" /> <Default Extension="bsdiff" ContentType="application/octet" /> <Default Extension="shasum" ContentType="text/plain" /> </Types>`.replace(/\n/, "\r\n"), { name: "[Content_Types].xml" }); archive.append(`<?xml version="1.0" encoding="utf-8"?> <coreProperties xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"> <dc:creator>${ author }</dc:creator> <dc:description>${ options.description }</dc:description> <dc:identifier>${ options.appId }</dc:identifier> <version>${ version }</version> <keywords/> <dc:title>${ options.productName }</dc:title> <lastModifiedBy>NuGet, Version=2.8.50926.602, Culture=neutral, PublicKeyToken=null;Microsoft Windows NT 6.2.9200.0;.NET Framework 4</lastModifiedBy> </coreProperties>`.replace(/\n/, "\r\n"), { name: "1.psmdcp", prefix: "package/services/metadata/core-properties" }); archive.file(updateFile, { name: "Update.exe", prefix: "lib/net45" }); encodedZip(archive, directory, "lib/net45"); yield archivePromise; }); return function pack(_x6, _x7, _x8, _x9, _x10, _x11) { return _ref2.apply(this, arguments); }; })(); let releasify = (() => { var _ref3 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (options, nupkgPath, outputDirectory, packageName) { const args = ["--releasify", nupkgPath, "--releaseDir", outputDirectory]; const out = (yield (0, (_util || _load_util()).exec)(process.platform === "win32" ? (_path || _load_path()).join(options.vendorPath, "Update.com") : "mono", prepareArgs(args, (_path || _load_path()).join(options.vendorPath, "Update-Mono.exe")), { maxBuffer: 4 * 1024000 })).trim(); if ((_util || _load_util()).debug.enabled) { (0, (_util || _load_util()).debug)(`Squirrel output: ${ out }`); } const lines = out.split("\n"); for (let i = lines.length - 1; i > -1; i--) { const line = lines[i]; if (line.indexOf(packageName) !== -1) { return line.trim(); } } throw new Error(`Invalid output, cannot find last release entry, output: ${ out }`); }); return function releasify(_x12, _x13, _x14, _x15) { return _ref3.apply(this, arguments); }; })(); let msi = (() => { var _ref4 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (options, nupkgPath, setupPath, outputDirectory, outFile) { const args = ["--createMsi", nupkgPath, "--bootstrapperExe", setupPath]; yield (0, (_util || _load_util()).exec)(process.platform === "win32" ? (_path || _load_path()).join(options.vendorPath, "Update.com") : "mono", prepareArgs(args, (_path || _load_path()).join(options.vendorPath, "Update-Mono.exe"))); //noinspection SpellCheckingInspection yield (0, (_util || _load_util()).exec)((_path || _load_path()).join(options.vendorPath, "candle.exe"), ["-nologo", "-ext", "WixNetFxExtension", "-out", "Setup.wixobj", "Setup.wxs"], { cwd: outputDirectory }); //noinspection SpellCheckingInspection yield (0, (_util || _load_util()).exec)((_path || _load_path()).join(options.vendorPath, "light.exe"), ["-ext", "WixNetFxExtension", "-sval", "-out", outFile, "Setup.wixobj"], { cwd: outputDirectory }); //noinspection SpellCheckingInspection yield (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all([(0, (_fsExtraP || _load_fsExtraP()).unlink)((_path || _load_path()).join(outputDirectory, "Setup.wxs")), (0, (_fsExtraP || _load_fsExtraP()).unlink)((_path || _load_path()).join(outputDirectory, "Setup.wixobj")), (0, (_fsExtraP || _load_fsExtraP()).unlink)((_path || _load_path()).join(outputDirectory, outFile.replace(".msi", ".wixpdb"))).catch(function (e) { return (0, (_util || _load_util()).debug)(e.toString()); })]); }); return function msi(_x16, _x17, _x18, _x19, _x20) { return _ref4.apply(this, arguments); }; })(); exports.convertVersion = convertVersion; var _path; function _load_path() { return _path = _interopRequireWildcard(require("path")); } var _fsExtraP; function _load_fsExtraP() { return _fsExtraP = require("fs-extra-p"); } var _util; function _load_util() { return _util = require("../util/util"); } var _log; function _load_log() { return _log = require("../util/log"); } 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 archiverUtil = require("archiver-utils"); const archiver = require("archiver"); function convertVersion(version) { const parts = version.split("-"); const mainVersion = parts.shift(); if (parts.length > 0) { return [mainVersion, parts.join("-").replace(/\./g, "")].join("-"); } else { return mainVersion; } } function syncReleases(outputDirectory, options) { (0, (_log || _load_log()).log)("Sync releases to build delta package"); const args = prepareArgs(["-u", options.remoteReleases, "-r", outputDirectory], (_path || _load_path()).join(options.vendorPath, "SyncReleases.exe")); if (options.remoteToken) { args.push("-t", options.remoteToken); } return (0, (_util || _load_util()).spawn)(process.platform === "win32" ? (_path || _load_path()).join(options.vendorPath, "SyncReleases.exe") : "mono", args); } function prepareArgs(args, exePath) { if (process.platform !== "win32") { args.unshift(exePath); } return args; } function encodedZip(archive, dir, prefix) { archiverUtil.walkdir(dir, function (error, files) { if (error) { archive.emit("error", error); return; } for (let file of files) { if (file.stats.isDirectory()) { continue; } // GBK file name encoding (or Non-English file name) caused a problem const entryData = { name: encodeURI(file.relative.replace(/\\/g, "/")).replace(/%5B/g, "[").replace(/%5D/g, "]"), prefix: prefix, stats: file.stats }; archive._append(file.path, entryData); } archive.finalize(); }); } //# sourceMappingURL=squirrelPack.js.map