electron-builder-lib
Version:
electron-builder lib
529 lines (475 loc) • 22.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Packager = undefined;
var _bluebirdLst;
function _load_bluebirdLst() {
return _bluebirdLst = require("bluebird-lst");
}
var _bluebirdLst2;
function _load_bluebirdLst2() {
return _bluebirdLst2 = _interopRequireDefault(require("bluebird-lst"));
}
var _builderUtil;
function _load_builderUtil() {
return _builderUtil = require("builder-util");
}
var _builderUtilRuntime;
function _load_builderUtilRuntime() {
return _builderUtilRuntime = require("builder-util-runtime");
}
var _promise;
function _load_promise() {
return _promise = require("builder-util/out/promise");
}
var _events;
function _load_events() {
return _events = require("events");
}
var _fsExtraP;
function _load_fsExtraP() {
return _fsExtraP = require("fs-extra-p");
}
var _isCi;
function _load_isCi() {
return _isCi = _interopRequireDefault(require("is-ci"));
}
var _jsYaml;
function _load_jsYaml() {
return _jsYaml = require("js-yaml");
}
var _lazyVal;
function _load_lazyVal() {
return _lazyVal = require("lazy-val");
}
var _path = _interopRequireWildcard(require("path"));
var _deepAssign;
function _load_deepAssign() {
return _deepAssign = require("read-config-file/out/deepAssign");
}
var _appInfo;
function _load_appInfo() {
return _appInfo = require("./appInfo");
}
var _asar;
function _load_asar() {
return _asar = require("./asar/asar");
}
var _core;
function _load_core() {
return _core = require("./core");
}
var _platformPackager;
function _load_platformPackager() {
return _platformPackager = require("./platformPackager");
}
var _targetFactory;
function _load_targetFactory() {
return _targetFactory = require("./targets/targetFactory");
}
var _config;
function _load_config() {
return _config = require("./util/config");
}
var _electronVersion;
function _load_electronVersion() {
return _electronVersion = require("./util/electronVersion");
}
var _packageDependencies;
function _load_packageDependencies() {
return _packageDependencies = require("./util/packageDependencies");
}
var _packageMetadata;
function _load_packageMetadata() {
return _packageMetadata = require("./util/packageMetadata");
}
var _repositoryInfo;
function _load_repositoryInfo() {
return _repositoryInfo = require("./util/repositoryInfo");
}
var _yarn;
function _load_yarn() {
return _yarn = require("./util/yarn");
}
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 }; }
function addHandler(emitter, event, handler) {
emitter.on(event, handler);
}
class Packager {
//noinspection JSUnusedGlobalSymbols
constructor(options, cancellationToken = new (_builderUtilRuntime || _load_builderUtilRuntime()).CancellationToken()) {
this.cancellationToken = cancellationToken;
this.isTwoPackageJsonProjectLayoutUsed = false;
this.eventEmitter = new (_events || _load_events()).EventEmitter();
this.tempDirManager = new (_builderUtil || _load_builderUtil()).TmpDir("packager");
this._repositoryInfo = new (_lazyVal || _load_lazyVal()).Lazy(() => (0, (_repositoryInfo || _load_repositoryInfo()).getRepositoryInfo)(this.projectDir, this.metadata, this.devMetadata));
this.afterPackHandlers = [];
this.afterSignHandlers = [];
this.debugLogger = new (_builderUtil || _load_builderUtil()).DebugLogger((_builderUtil || _load_builderUtil()).log.isDebugEnabled);
this._productionDeps = null;
this._electronDownloader = null;
this.electronDownloader = options => {
if (this._electronDownloader == null) {
this._electronDownloader = (_bluebirdLst2 || _load_bluebirdLst2()).default.promisify(require("electron-download-tf"));
}
return this._electronDownloader(options);
};
this.stageDirPathCustomizer = (target, packager, arch) => {
return _path.join(target.outDir, `__${target.name}-${(_builderUtil || _load_builderUtil()).Arch[arch]}`);
};
if ("project" in options) {
throw new Error("Use projectDir instead of project");
}
if ("devMetadata" in options) {
throw new Error("devMetadata in the options is deprecated, please use config instead");
}
if ("extraMetadata" in options) {
throw new Error("extraMetadata in the options is deprecated, please use config.extraMetadata instead");
}
const targets = options.targets || new Map();
if (options.targets == null) {
options.targets = targets;
}
function processTargets(platform, types) {
function commonArch(currentIfNotSpecified) {
if (platform === (_core || _load_core()).Platform.MAC) {
return currentIfNotSpecified ? [(_builderUtil || _load_builderUtil()).Arch.x64] : [];
}
const result = Array();
return result.length === 0 && currentIfNotSpecified ? [(0, (_builderUtil || _load_builderUtil()).archFromString)(process.arch)] : result;
}
let archToType = targets.get(platform);
if (archToType == null) {
archToType = new Map();
targets.set(platform, archToType);
}
if (types.length === 0) {
for (const arch of commonArch(false)) {
archToType.set(arch, []);
}
return;
}
for (const type of types) {
const suffixPos = type.lastIndexOf(":");
if (suffixPos > 0) {
(0, (_builderUtil || _load_builderUtil()).addValue)(archToType, (0, (_builderUtil || _load_builderUtil()).archFromString)(type.substring(suffixPos + 1)), type.substring(0, suffixPos));
} else {
for (const arch of commonArch(true)) {
(0, (_builderUtil || _load_builderUtil()).addValue)(archToType, arch, type);
}
}
}
}
if (options.mac != null) {
processTargets((_core || _load_core()).Platform.MAC, options.mac);
}
if (options.linux != null) {
processTargets((_core || _load_core()).Platform.LINUX, options.linux);
}
if (options.win != null) {
processTargets((_core || _load_core()).Platform.WINDOWS, options.win);
}
this.projectDir = options.projectDir == null ? process.cwd() : _path.resolve(options.projectDir);
this._appDir = this.projectDir;
this.options = Object.assign({}, options, { prepackaged: options.prepackaged == null ? null : _path.resolve(this.projectDir, options.prepackaged) });
try {
(_builderUtil || _load_builderUtil()).log.info({ version: "19.53.5" }, "electron-builder");
} catch (e) {
// error in dev mode without babel
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
get appDir() {
return this._appDir;
}
get metadata() {
return this._metadata;
}
get isPrepackedAppAsar() {
return this._isPrepackedAppAsar;
}
get devMetadata() {
return this._devMetadata;
}
get config() {
return this._configuration;
}
get repositoryInfo() {
return this._repositoryInfo.value;
}
get productionDeps() {
let result = this._productionDeps;
if (result == null) {
result = (0, (_packageDependencies || _load_packageDependencies()).createLazyProductionDeps)(this.appDir);
this._productionDeps = result;
}
return result;
}
get buildResourcesDir() {
let result = this._buildResourcesDir;
if (result == null) {
result = _path.resolve(this.projectDir, this.relativeBuildResourcesDirname);
this._buildResourcesDir = result;
}
return result;
}
get relativeBuildResourcesDirname() {
return this.config.directories.buildResources;
}
addAfterPackHandler(handler) {
this.afterPackHandlers.push(handler);
}
addAfterSignHandler(handler) {
this.afterSignHandlers.push(handler);
}
artifactCreated(handler) {
addHandler(this.eventEmitter, "artifactCreated", handler);
return this;
}
dispatchArtifactCreated(event) {
this.eventEmitter.emit("artifactCreated", event);
}
build() {
var _this = this;
return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
let configPath = null;
let configFromOptions = _this.options.config;
if (typeof configFromOptions === "string") {
// it is a path to config file
configPath = configFromOptions;
configFromOptions = null;
} else if (configFromOptions != null && configFromOptions.extends != null && configFromOptions.extends.includes(".")) {
configPath = configFromOptions.extends;
}
const projectDir = _this.projectDir;
const devPackageFile = _path.join(projectDir, "package.json");
_this._devMetadata = yield (0, (_promise || _load_promise()).orNullIfFileNotExist)((0, (_packageMetadata || _load_packageMetadata()).readPackageJson)(devPackageFile));
const devMetadata = _this.devMetadata;
const configuration = yield (0, (_config || _load_config()).getConfig)(projectDir, configPath, configFromOptions, new (_lazyVal || _load_lazyVal()).Lazy(function () {
return (_bluebirdLst2 || _load_bluebirdLst2()).default.resolve(devMetadata);
}));
if ((_builderUtil || _load_builderUtil()).log.isDebugEnabled) {
(_builderUtil || _load_builderUtil()).log.debug({ config: (0, (_builderUtil || _load_builderUtil()).serializeToYaml)(JSON.parse((0, (_builderUtil || _load_builderUtil()).safeStringifyJson)(configuration))) }, "effective config");
}
_this._appDir = yield (0, (_config || _load_config()).computeDefaultAppDirectory)(projectDir, configuration.directories.app);
_this.isTwoPackageJsonProjectLayoutUsed = _this._appDir !== projectDir;
const appPackageFile = _this.isTwoPackageJsonProjectLayoutUsed ? _path.join(_this.appDir, "package.json") : devPackageFile;
// tslint:disable:prefer-conditional-expression
if (_this.devMetadata != null && !_this.isTwoPackageJsonProjectLayoutUsed) {
_this._metadata = _this.devMetadata;
} else {
_this._metadata = yield _this.readProjectMetadataIfTwoPackageStructureOrPrepacked(appPackageFile);
}
(0, (_deepAssign || _load_deepAssign()).deepAssign)(_this.metadata, configuration.extraMetadata);
if (_this.isTwoPackageJsonProjectLayoutUsed) {
(_builderUtil || _load_builderUtil()).log.debug({ devPackageFile, appPackageFile }, "two package.json structure is used");
}
(0, (_packageMetadata || _load_packageMetadata()).checkMetadata)(_this.metadata, _this.devMetadata, appPackageFile, devPackageFile);
return yield _this._build(configuration, _this._metadata, _this._devMetadata);
})();
}
// external caller of this method always uses isTwoPackageJsonProjectLayoutUsed=false and appDir=projectDir, no way (and need) to use another values
_build(configuration, metadata, devMetadata, repositoryInfo) {
var _this2 = this;
return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
yield (0, (_config || _load_config()).validateConfig)(configuration, _this2.debugLogger);
_this2._configuration = configuration;
_this2._metadata = metadata;
_this2._devMetadata = devMetadata;
if (repositoryInfo != null) {
_this2._repositoryInfo.value = (_bluebirdLst2 || _load_bluebirdLst2()).default.resolve(repositoryInfo);
}
const projectDir = _this2.projectDir;
if (configuration.electronVersion == null) {
// for prepacked app asar no dev deps in the app.asar
if (_this2.isPrepackedAppAsar) {
configuration.electronVersion = yield (0, (_electronVersion || _load_electronVersion()).getElectronVersionFromInstalled)(projectDir);
if (configuration.electronVersion == null) {
throw new Error(`Cannot compute electron version for prepacked asar`);
}
}
configuration.electronVersion = yield (0, (_electronVersion || _load_electronVersion()).computeElectronVersion)(projectDir, new (_lazyVal || _load_lazyVal()).Lazy(function () {
return (_bluebirdLst2 || _load_bluebirdLst2()).default.resolve(_this2.metadata);
}));
}
_this2.appInfo = new (_appInfo || _load_appInfo()).AppInfo(_this2);
const outDir = _path.resolve(_this2.projectDir, configuration.directories.output);
if (!(_isCi || _load_isCi()).default && process.stdout.isTTY) {
const effectiveConfigFile = _path.join(outDir, "electron-builder.yaml");
(_builderUtil || _load_builderUtil()).log.info({ file: (_builderUtil || _load_builderUtil()).log.filePath(effectiveConfigFile) }, "writing effective config");
// dump instead of safeDump to dump functions
yield (0, (_fsExtraP || _load_fsExtraP()).outputFile)(effectiveConfigFile, (0, (_jsYaml || _load_jsYaml()).dump)(configuration));
}
return {
outDir,
platformToTargets: yield (0, (_promise || _load_promise()).executeFinally)(_this2.doBuild(outDir), (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
if (_this2.debugLogger.enabled) {
yield _this2.debugLogger.save(_path.join(outDir, "electron-builder-debug.yml"));
}
yield _this2.tempDirManager.cleanup();
}))
};
})();
}
readProjectMetadataIfTwoPackageStructureOrPrepacked(appPackageFile) {
var _this3 = this;
return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
let data = yield (0, (_promise || _load_promise()).orNullIfFileNotExist)((0, (_packageMetadata || _load_packageMetadata()).readPackageJson)(appPackageFile));
if (data != null) {
return data;
}
data = yield (0, (_promise || _load_promise()).orNullIfFileNotExist)((0, (_asar || _load_asar()).readAsarJson)(_path.join(_this3.projectDir, "app.asar"), "package.json"));
if (data != null) {
_this3._isPrepackedAppAsar = true;
return data;
}
throw new Error(`Cannot find package.json in the ${_path.dirname(appPackageFile)}`);
})();
}
doBuild(outDir) {
var _this4 = this;
return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
const taskManager = new (_builderUtil || _load_builderUtil()).AsyncTaskManager(_this4.cancellationToken);
const platformToTarget = new Map();
const createdOutDirs = new Set();
for (const [platform, archToType] of _this4.options.targets) {
if (_this4.cancellationToken.cancelled) {
break;
}
if (platform === (_core || _load_core()).Platform.MAC && process.platform === (_core || _load_core()).Platform.WINDOWS.nodeName) {
throw new Error("Build for macOS is supported only on macOS, please see https://electron.build/multi-platform-build");
}
const packager = _this4.createHelper(platform);
const nameToTarget = new Map();
platformToTarget.set(platform, nameToTarget);
for (const [arch, targetNames] of (0, (_targetFactory || _load_targetFactory()).computeArchToTargetNamesMap)(archToType, packager.platformSpecificBuildOptions, platform)) {
if (_this4.cancellationToken.cancelled) {
break;
}
yield _this4.installAppDependencies(platform, arch);
if (_this4.cancellationToken.cancelled) {
break;
}
const targetList = (0, (_targetFactory || _load_targetFactory()).createTargets)(nameToTarget, targetNames.length === 0 ? packager.defaultTarget : targetNames, outDir, packager);
yield createOutDirIfNeed(targetList, createdOutDirs);
yield packager.pack(outDir, arch, targetList, taskManager);
}
if (_this4.cancellationToken.cancelled) {
break;
}
for (const target of nameToTarget.values()) {
taskManager.addTask(target.finishBuild());
}
}
yield taskManager.awaitTasks();
return platformToTarget;
})();
}
createHelper(platform) {
if (this.options.platformPackagerFactory != null) {
return this.options.platformPackagerFactory(this, platform);
}
switch (platform) {
case (_core || _load_core()).Platform.MAC:
{
const helperClass = require("./macPackager").default;
return new helperClass(this);
}
case (_core || _load_core()).Platform.WINDOWS:
{
const helperClass = require("./winPackager").WinPackager;
return new helperClass(this);
}
case (_core || _load_core()).Platform.LINUX:
return new (require("./linuxPackager").LinuxPackager)(this);
default:
throw new Error(`Unknown platform: ${platform}`);
}
}
installAppDependencies(platform, arch) {
var _this5 = this;
return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
if (_this5.options.prepackaged != null) {
return;
}
const frameworkInfo = { version: _this5.config.muonVersion || _this5.config.electronVersion, useCustomDist: _this5.config.muonVersion == null };
const config = _this5.config;
if (config.nodeGypRebuild === true) {
(_builderUtil || _load_builderUtil()).log.info({ arch: (_builderUtil || _load_builderUtil()).Arch[arch] }, "executing node-gyp rebuild");
yield (0, (_builderUtil || _load_builderUtil()).exec)(process.platform === "win32" ? "node-gyp.cmd" : "node-gyp", ["rebuild"], {
env: (0, (_yarn || _load_yarn()).getGypEnv)(frameworkInfo, platform.nodeName, (_builderUtil || _load_builderUtil()).Arch[arch], true)
});
}
if (config.npmRebuild === false) {
(_builderUtil || _load_builderUtil()).log.info({ reason: "npmRebuild is set to false" }, "skipped app dependencies rebuild");
return;
}
const beforeBuild = (0, (_platformPackager || _load_platformPackager()).resolveFunction)(config.beforeBuild);
if (beforeBuild != null) {
const performDependenciesInstallOrRebuild = yield beforeBuild({
appDir: _this5.appDir,
electronVersion: _this5.config.electronVersion,
platform,
arch: (_builderUtil || _load_builderUtil()).Arch[arch]
});
if (!performDependenciesInstallOrRebuild) {
return;
}
}
if (config.buildDependenciesFromSource === true && platform.nodeName !== process.platform) {
(_builderUtil || _load_builderUtil()).log.info({ reason: "platform is different and buildDependenciesFromSource is set to true" }, "skipped app dependencies rebuild");
} else {
yield (0, (_yarn || _load_yarn()).installOrRebuild)(config, _this5.appDir, {
frameworkInfo,
platform: platform.nodeName,
arch: (_builderUtil || _load_builderUtil()).Arch[arch],
productionDeps: _this5.productionDeps
});
}
})();
}
afterPack(context) {
const afterPack = (0, (_platformPackager || _load_platformPackager()).resolveFunction)(this.config.afterPack);
const handlers = this.afterPackHandlers.slice();
if (afterPack != null) {
// user handler should be last
handlers.push(afterPack);
}
return (_bluebirdLst2 || _load_bluebirdLst2()).default.each(handlers, it => it(context));
}
afterSign(context) {
const afterSign = (0, (_platformPackager || _load_platformPackager()).resolveFunction)(this.config.afterSign);
const handlers = this.afterSignHandlers.slice();
if (afterSign != null) {
// user handler should be last
handlers.push(afterSign);
}
return (_bluebirdLst2 || _load_bluebirdLst2()).default.each(handlers, it => it(context));
}
}
exports.Packager = Packager;
function createOutDirIfNeed(targetList, createdOutDirs) {
const ourDirs = new Set();
for (const target of targetList) {
// noinspection SuspiciousInstanceOfGuard
if (target instanceof (_targetFactory || _load_targetFactory()).NoOpTarget) {
continue;
}
const outDir = target.outDir;
if (!createdOutDirs.has(outDir)) {
ourDirs.add(outDir);
}
}
if (ourDirs.size > 0) {
return (_bluebirdLst2 || _load_bluebirdLst2()).default.map(Array.from(ourDirs).sort(), it => {
createdOutDirs.add(it);
return (0, (_fsExtraP || _load_fsExtraP()).ensureDir)(it);
});
}
return (_bluebirdLst2 || _load_bluebirdLst2()).default.resolve();
}
//# sourceMappingURL=packager.js.map