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
283 lines (248 loc) • 11.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.WinPackager = 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 checkIcon = (() => {
var _ref = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (file) {
const fd = yield (0, (_fsExtraP || _load_fsExtraP()).open)(file, "r");
const buffer = new Buffer(512);
try {
yield (0, (_fsExtraP || _load_fsExtraP()).read)(fd, buffer, 0, buffer.length, 0);
} finally {
yield (0, (_fsExtraP || _load_fsExtraP()).close)(fd);
}
if (!isIco(buffer)) {
throw new Error(`Windows icon is not valid ico file, please fix "${ file }"`);
}
const sizes = parseIco(buffer);
for (let size of sizes) {
if (size.w >= 256 && size.h >= 256) {
return;
}
}
throw new Error(`Windows icon size must be at least 256x256, please fix "${ file }"`);
});
return function checkIcon(_x) {
return _ref.apply(this, arguments);
};
})();
var _codeSign;
function _load_codeSign() {
return _codeSign = require("./codeSign");
}
var _platformPackager;
function _load_platformPackager() {
return _platformPackager = require("./platformPackager");
}
var _metadata;
function _load_metadata() {
return _metadata = require("./metadata");
}
var _path;
function _load_path() {
return _path = _interopRequireWildcard(require("path"));
}
var _log;
function _load_log() {
return _log = require("./util/log");
}
var _util;
function _load_util() {
return _util = require("./util/util");
}
var _fsExtraP;
function _load_fsExtraP() {
return _fsExtraP = require("fs-extra-p");
}
var _windowsCodeSign;
function _load_windowsCodeSign() {
return _windowsCodeSign = require("./windowsCodeSign");
}
var _targetFactory;
function _load_targetFactory() {
return _targetFactory = require("./targets/targetFactory");
}
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 WinPackager extends (_platformPackager || _load_platformPackager()).PlatformPackager {
constructor(info) {
super(info);
const subjectName = this.platformSpecificBuildOptions.certificateSubjectName;
if (subjectName == null) {
const certificateFile = this.platformSpecificBuildOptions.certificateFile;
if (certificateFile != null) {
const certificatePassword = this.getCscPassword();
this.cscInfo = (_bluebirdLstC2 || _load_bluebirdLstC2()).default.resolve({
file: certificateFile,
password: certificatePassword == null ? null : certificatePassword.trim()
});
} else {
const cscLink = process.env.WIN_CSC_LINK || this.options.cscLink;
if (cscLink != null) {
this.cscInfo = (0, (_codeSign || _load_codeSign()).downloadCertificate)(cscLink, info.tempDirManager).then(path => {
return {
file: path,
password: this.getCscPassword()
};
});
} else {
this.cscInfo = (_bluebirdLstC2 || _load_bluebirdLstC2()).default.resolve(null);
}
}
} else {
this.cscInfo = (_bluebirdLstC2 || _load_bluebirdLstC2()).default.resolve({
subjectName: subjectName
});
}
this.iconPath = this.getValidIconPath();
}
doGetCscPassword() {
return this.platformSpecificBuildOptions.certificatePassword || process.env.WIN_CSC_KEY_PASSWORD || super.doGetCscPassword();
}
createTargets(targets, mapper, cleanupTasks) {
for (let name of targets) {
if (name === (_targetFactory || _load_targetFactory()).DIR_TARGET) {
continue;
}
if (name === "squirrel") {
mapper("squirrel", () => {
const targetClass = require("./targets/squirrelWindows").default;
return new targetClass(this);
});
} else if (name === (_targetFactory || _load_targetFactory()).DEFAULT_TARGET || name === "nsis") {
mapper(name, outDir => {
const targetClass = require("./targets/nsis").default;
return new targetClass(this, outDir);
});
} else {
mapper(name, () => (0, (_targetFactory || _load_targetFactory()).createCommonTarget)(name));
}
}
}
get platform() {
return (_metadata || _load_metadata()).Platform.WINDOWS;
}
getIconPath() {
var _this = this;
return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () {
return yield _this.iconPath;
})();
}
getValidIconPath() {
var _this2 = this;
return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () {
let iconPath = _this2.platformSpecificBuildOptions.icon || _this2.devMetadata.build.icon;
if (iconPath != null && !iconPath.endsWith(".ico")) {
iconPath += ".ico";
}
iconPath = iconPath == null ? yield _this2.getDefaultIcon("ico") : (_path || _load_path()).resolve(_this2.projectDir, iconPath);
if (iconPath == null) {
return null;
}
yield checkIcon(iconPath);
return iconPath;
})();
}
pack(outDir, arch, targets, postAsyncTasks) {
var _this3 = this;
return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () {
const appOutDir = _this3.computeAppOutDir(outDir, arch);
yield _this3.doPack(outDir, appOutDir, _this3.platform.nodeName, arch, _this3.platformSpecificBuildOptions);
_this3.packageInDistributableFormat(outDir, appOutDir, arch, targets, postAsyncTasks);
})();
}
sign(file) {
var _this4 = this;
return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () {
const cscInfo = yield _this4.cscInfo;
if (cscInfo != null) {
(0, (_log || _load_log()).log)(`Signing ${ (_path || _load_path()).basename(file) } (certificate file "${ cscInfo.file }")`);
yield _this4.doSign({
path: file,
cert: cscInfo.file,
subjectName: cscInfo.subjectName,
password: cscInfo.password,
name: _this4.appInfo.productName,
site: yield _this4.appInfo.computePackageUrl(),
hash: _this4.platformSpecificBuildOptions.signingHashAlgorithms,
tr: _this4.platformSpecificBuildOptions.rfc3161TimeStampServer
});
}
})();
}
//noinspection JSMethodCanBeStatic
doSign(options) {
return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () {
return (0, (_windowsCodeSign || _load_windowsCodeSign()).sign)(options);
})();
}
signAndEditResources(file) {
var _this5 = this;
return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () {
const appInfo = _this5.appInfo;
const args = [file, "--set-version-string", "CompanyName", appInfo.companyName, "--set-version-string", "FileDescription", appInfo.description, "--set-version-string", "ProductName", appInfo.productName, "--set-version-string", "InternalName", (_path || _load_path()).basename(appInfo.productFilename, ".exe"), "--set-version-string", "LegalCopyright", appInfo.copyright, "--set-version-string", "OriginalFilename", "", "--set-file-version", appInfo.buildVersion, "--set-product-version", appInfo.version];
(0, (_util || _load_util()).use)(_this5.platformSpecificBuildOptions.legalTrademarks, function (it) {
return args.push("--set-version-string", "LegalTrademarks", it);
});
(0, (_util || _load_util()).use)((yield _this5.getIconPath()), function (it) {
return args.push("--set-icon", it);
});
const rceditExecutable = (_path || _load_path()).join((yield (0, (_windowsCodeSign || _load_windowsCodeSign()).getSignVendorPath)()), "rcedit.exe");
const isWin = process.platform === "win32";
if (!isWin) {
args.unshift(rceditExecutable);
}
yield (0, (_util || _load_util()).exec)(isWin ? rceditExecutable : "wine", args);
yield _this5.sign(file);
})();
}
postInitApp(appOutDir) {
var _this6 = this;
return (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () {
const executable = (_path || _load_path()).join(appOutDir, `${ _this6.appInfo.productFilename }.exe`);
yield (0, (_fsExtraP || _load_fsExtraP()).rename)((_path || _load_path()).join(appOutDir, "electron.exe"), executable);
yield _this6.signAndEditResources(executable);
})();
}
packageInDistributableFormat(outDir, appOutDir, arch, targets, promises) {
for (let target of targets) {
if (target instanceof (_platformPackager || _load_platformPackager()).TargetEx) {
promises.push(target.build(appOutDir, arch));
} else {
const format = target.name;
(0, (_log || _load_log()).log)(`Building Windows ${ format }`);
// we use app name here - see https://github.com/electron-userland/electron-builder/pull/204
const outFile = (_path || _load_path()).join(outDir, this.generateName(format, arch, false, "win"));
promises.push(this.archiveApp(format, appOutDir, outFile).then(() => this.dispatchArtifactCreated(outFile, this.generateName(format, arch, true, "win"))));
}
}
}
}
exports.WinPackager = WinPackager;
function parseIco(buffer) {
if (!isIco(buffer)) {
throw new Error("buffer is not ico");
}
const n = buffer.readUInt16LE(4);
const result = new Array(n);
for (let i = 0; i < n; i++) {
result[i] = {
w: buffer.readUInt8(6 + i * 16) || 256,
h: buffer.readUInt8(7 + i * 16) || 256
};
}
return result;
}
function isIco(buffer) {
return buffer.readUInt16LE(0) === 0 && buffer.readUInt16LE(2) === 1;
}
//# sourceMappingURL=winPackager.js.map