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
284 lines (247 loc) • 12.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findIdentity = exports.findIdentityRawResult = exports.createKeychain = exports.downloadCertificate = exports.appleCertificatePrefixes = 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 downloadCertificate = exports.downloadCertificate = (() => {
var _ref = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (urlOrBase64, tmpDir) {
if (urlOrBase64.startsWith("file://")) {
return urlOrBase64.substring("file://".length);
}
const tempFile = yield tmpDir.getTempFile(".p12");
if (urlOrBase64.startsWith("https://")) {
yield (0, (_httpRequest || _load_httpRequest()).download)(urlOrBase64, tempFile);
} else {
yield (0, (_fsExtraP || _load_fsExtraP()).outputFile)(tempFile, new Buffer(urlOrBase64, "base64"));
}
return tempFile;
});
return function downloadCertificate(_x, _x2) {
return _ref.apply(this, arguments);
};
})();
// "Note that filename will not be searched to resolve the signing identity's certificate chain unless it is also on the user's keychain search list."
// but "security list-keychains" doesn't support add - we should 1) get current list 2) set new list - it is very bad http://stackoverflow.com/questions/10538942/add-a-keychain-to-search-list
// "overly complicated and introduces a race condition."
// https://github.com/electron-userland/electron-builder/issues/398
let createCustomCertKeychain = (() => {
var _ref2 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* () {
// copy to temp and then atomic rename to final path
const tmpKeychainPath = (_path || _load_path()).join((0, (_util || _load_util()).getCacheDirectory)(), (0, (_util || _load_util()).getTempName)("electron-builder-root-certs"));
const keychainPath = (_path || _load_path()).join((0, (_util || _load_util()).getCacheDirectory)(), "electron-builder-root-certs.keychain");
const results = yield (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all([(0, (_util || _load_util()).exec)("security", ["list-keychains"]), (0, (_fsExtraP || _load_fsExtraP()).copy)((_path || _load_path()).join(__dirname, "..", "certs", "root_certs.keychain"), tmpKeychainPath).then(function () {
return (0, (_fsExtraP || _load_fsExtraP()).rename)(tmpKeychainPath, keychainPath);
})]);
const list = results[0].split("\n").map(function (it) {
let r = it.trim();
return r.substring(1, r.length - 1);
}).filter(function (it) {
return it.length > 0;
});
if (!(list.indexOf(keychainPath) !== -1)) {
yield (0, (_util || _load_util()).exec)("security", ["list-keychains", "-d", "user", "-s", keychainPath].concat(list));
}
});
return function createCustomCertKeychain() {
return _ref2.apply(this, arguments);
};
})();
let createKeychain = exports.createKeychain = (() => {
var _ref3 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (tmpDir, cscLink, cscKeyPassword, cscILink, cscIKeyPassword) {
if (bundledCertKeychainAdded == null) {
bundledCertKeychainAdded = createCustomCertKeychain();
}
yield bundledCertKeychainAdded;
const keychainName = yield tmpDir.getTempFile(".keychain");
const certLinks = [cscLink];
if (cscILink != null) {
certLinks.push(cscILink);
}
const certPaths = new Array(certLinks.length);
const keychainPassword = (0, (_crypto || _load_crypto()).randomBytes)(8).toString("hex");
return yield (0, (_promise || _load_promise()).executeFinally)((_bluebirdLstC2 || _load_bluebirdLstC2()).default.all([(_bluebirdLstC2 || _load_bluebirdLstC2()).default.map(certLinks, function (link, i) {
return downloadCertificate(link, tmpDir).then(function (it) {
return certPaths[i] = it;
});
}), (_bluebirdLstC2 || _load_bluebirdLstC2()).default.mapSeries([["create-keychain", "-p", keychainPassword, keychainName], ["unlock-keychain", "-p", keychainPassword, keychainName], ["set-keychain-settings", "-t", "3600", "-u", keychainName]], function (it) {
return (0, (_util || _load_util()).exec)("security", it);
})]).then(function () {
return importCerts(keychainName, certPaths, [cscKeyPassword, cscIKeyPassword].filter(function (it) {
return it != null;
}));
}), function () {
return (0, (_promise || _load_promise()).all)(certPaths.map(function (it, index) {
return certLinks[index].startsWith("https://") ? (0, (_fsExtraP || _load_fsExtraP()).deleteFile)(it, true) : (_bluebirdLstC2 || _load_bluebirdLstC2()).default.resolve();
}));
});
});
return function createKeychain(_x3, _x4, _x5, _x6, _x7) {
return _ref3.apply(this, arguments);
};
})();
let importCerts = (() => {
var _ref4 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (keychainName, paths, keyPasswords) {
for (let i = 0; i < paths.length; i++) {
yield (0, (_util || _load_util()).exec)("security", ["import", paths[i], "-k", keychainName, "-T", "/usr/bin/codesign", "-T", "/usr/bin/productbuild", "-P", keyPasswords[i]]);
}
return {
keychainName: keychainName
};
});
return function importCerts(_x8, _x9, _x10) {
return _ref4.apply(this, arguments);
};
})();
let getValidIdentities = (() => {
var _ref5 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (keychain) {
function addKeychain(args) {
if (keychain != null) {
args.push(keychain);
}
return args;
}
let result = findIdentityRawResult;
if (result == null || keychain != null) {
// https://github.com/electron-userland/electron-builder/issues/481
// https://github.com/electron-userland/electron-builder/issues/535
result = (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all([(0, (_util || _load_util()).exec)("security", addKeychain(["find-identity", "-v"])).then(function (it) {
return it.trim().split("\n").filter(function (it) {
for (let prefix of appleCertificatePrefixes) {
if (it.indexOf(prefix) !== -1) {
return true;
}
}
return false;
});
}), (0, (_util || _load_util()).exec)("security", addKeychain(["find-identity", "-v", "-p", "codesigning"])).then(function (it) {
return it.trim().split("\n");
})]).then(function (it) {
const array = it[0].concat(it[1]).filter(function (it) {
return !(it.indexOf("(Missing required extension)") !== -1) && !(it.indexOf("valid identities found") !== -1) && !(it.indexOf("iPhone ") !== -1) && !(it.indexOf("com.apple.idms.appleid.prd.") !== -1);
}).map(function (it) {
return it.substring(it.indexOf(")") + 1).trim();
});
return Array.from(new Set(array));
});
if (keychain == null) {
exports.findIdentityRawResult = findIdentityRawResult = result;
}
}
return result;
});
return function getValidIdentities(_x11) {
return _ref5.apply(this, arguments);
};
})();
let _findIdentity = (() => {
var _ref6 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (namePrefix, qualifier, keychain) {
// https://github.com/electron-userland/electron-builder/issues/484
//noinspection SpellCheckingInspection
const lines = yield getValidIdentities(keychain);
for (let line of lines) {
if (qualifier != null && !(line.indexOf(qualifier) !== -1)) {
continue;
}
if (line.indexOf(namePrefix) !== -1) {
return line.substring(line.indexOf('"') + 1, line.lastIndexOf('"'));
}
}
if (namePrefix === "Developer ID Application") {
// find non-Apple certificate
// https://github.com/electron-userland/electron-builder/issues/458
l: for (let line of lines) {
if (qualifier != null && !(line.indexOf(qualifier) !== -1)) {
continue;
}
if (line.indexOf("Mac Developer:") !== -1) {
continue;
}
for (let prefix of appleCertificatePrefixes) {
if (line.indexOf(prefix) !== -1) {
continue l;
}
}
return line.substring(line.indexOf('"') + 1, line.lastIndexOf('"'));
}
}
return null;
});
return function _findIdentity(_x12, _x13, _x14) {
return _ref6.apply(this, arguments);
};
})();
let findIdentity = exports.findIdentity = (() => {
var _ref7 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (certType, qualifier, keychain) {
let identity = process.env.CSC_NAME || qualifier;
if ((0, (_util || _load_util()).isEmptyOrSpaces)(identity)) {
if (keychain == null && !(0, (_util || _load_util()).isCi)() && process.env.CSC_IDENTITY_AUTO_DISCOVERY === "false") {
return null;
} else {
return yield _findIdentity(certType, null, keychain);
}
} else {
identity = identity.trim();
for (let prefix of appleCertificatePrefixes) {
checkPrefix(identity, prefix);
}
const result = yield _findIdentity(certType, identity, keychain);
if (result == null) {
throw new Error(`Identity name "${ identity }" is specified, but no valid identity with this name in the keychain`);
}
return result;
}
});
return function findIdentity(_x15, _x16, _x17) {
return _ref7.apply(this, arguments);
};
})();
exports.sign = sign;
var _util;
function _load_util() {
return _util = require("./util/util");
}
var _fsExtraP;
function _load_fsExtraP() {
return _fsExtraP = require("fs-extra-p");
}
var _httpRequest;
function _load_httpRequest() {
return _httpRequest = require("./util/httpRequest");
}
var _path;
function _load_path() {
return _path = _interopRequireWildcard(require("path"));
}
var _promise;
function _load_promise() {
return _promise = require("./util/promise");
}
var _crypto;
function _load_crypto() {
return _crypto = require("crypto");
}
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 appleCertificatePrefixes = exports.appleCertificatePrefixes = ["Developer ID Application:", "Developer ID Installer:", "3rd Party Mac Developer Application:", "3rd Party Mac Developer Installer:"];
let bundledCertKeychainAdded = null;function sign(path, name, keychain) {
const args = ["--deep", "--force", "--sign", name, path];
if (keychain != null) {
args.push("--keychain", keychain);
}
return (0, (_util || _load_util()).exec)("codesign", args);
}
let findIdentityRawResult = exports.findIdentityRawResult = null;
function checkPrefix(name, prefix) {
if (name.startsWith(prefix)) {
throw new Error(`Please remove prefix "${ prefix }" from the specified name — appropriate certificate will be chosen automatically`);
}
}
//# sourceMappingURL=codeSign.js.map