electronVersion.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.computeElectronVersion = exports.getElectronPackage = exports.getElectronVersionFromInstalled = exports.getElectronVersion = void 0;
  4. const builder_util_1 = require("builder-util");
  5. const builder_util_runtime_1 = require("builder-util-runtime");
  6. const nodeHttpExecutor_1 = require("builder-util/out/nodeHttpExecutor");
  7. const fs_extra_1 = require("fs-extra");
  8. const path = require("path");
  9. const read_config_file_1 = require("read-config-file");
  10. const semver = require("semver");
  11. const config_1 = require("../util/config");
  12. const electronPackages = ["electron", "electron-prebuilt", "electron-prebuilt-compile", "electron-nightly"];
  13. async function getElectronVersion(projectDir, config) {
  14. if (config == null) {
  15. config = await (0, config_1.getConfig)(projectDir, null, null);
  16. }
  17. if (config.electronVersion != null) {
  18. return config.electronVersion;
  19. }
  20. return computeElectronVersion(projectDir);
  21. }
  22. exports.getElectronVersion = getElectronVersion;
  23. async function getElectronVersionFromInstalled(projectDir) {
  24. for (const name of electronPackages) {
  25. try {
  26. return (await (0, fs_extra_1.readJson)(path.join(projectDir, "node_modules", name, "package.json"))).version;
  27. }
  28. catch (e) {
  29. if (e.code !== "ENOENT") {
  30. builder_util_1.log.warn({ name, error: e }, `cannot read electron version package.json`);
  31. }
  32. }
  33. }
  34. return null;
  35. }
  36. exports.getElectronVersionFromInstalled = getElectronVersionFromInstalled;
  37. async function getElectronPackage(projectDir) {
  38. for (const name of electronPackages) {
  39. try {
  40. return await (0, fs_extra_1.readJson)(path.join(projectDir, "node_modules", name, "package.json"));
  41. }
  42. catch (e) {
  43. if (e.code !== "ENOENT") {
  44. builder_util_1.log.warn({ name, error: e }, `cannot find electron in package.json`);
  45. }
  46. }
  47. }
  48. return null;
  49. }
  50. exports.getElectronPackage = getElectronPackage;
  51. /** @internal */
  52. async function computeElectronVersion(projectDir) {
  53. const result = await getElectronVersionFromInstalled(projectDir);
  54. if (result != null) {
  55. return result;
  56. }
  57. const metadata = await (0, read_config_file_1.orNullIfFileNotExist)((0, fs_extra_1.readJson)(path.join(projectDir, "package.json")));
  58. const dependency = metadata ? findFromPackageMetadata(metadata) : null;
  59. if ((dependency === null || dependency === void 0 ? void 0 : dependency.name) === "electron-nightly") {
  60. builder_util_1.log.info("You are using a nightly version of electron, be warned that those builds are highly unstable.");
  61. const feedXml = await nodeHttpExecutor_1.httpExecutor.request({
  62. hostname: "github.com",
  63. path: `/electron/nightlies/releases.atom`,
  64. headers: {
  65. accept: "application/xml, application/atom+xml, text/xml, */*",
  66. },
  67. });
  68. const feed = (0, builder_util_runtime_1.parseXml)(feedXml);
  69. const latestRelease = feed.element("entry", false, `No published versions on GitHub`);
  70. const v = /\/tag\/v?([^/]+)$/.exec(latestRelease.element("link").attribute("href"))[1];
  71. return v.startsWith("v") ? v.substring(1) : v;
  72. }
  73. else if ((dependency === null || dependency === void 0 ? void 0 : dependency.version) === "latest") {
  74. builder_util_1.log.warn('Electron version is set to "latest", but it is recommended to set it to some more restricted version range.');
  75. try {
  76. const releaseInfo = JSON.parse((await nodeHttpExecutor_1.httpExecutor.request({
  77. hostname: "github.com",
  78. path: `/electron/${dependency.name === "electron-nightly" ? "nightlies" : "electron"}/releases/latest`,
  79. headers: {
  80. accept: "application/json",
  81. },
  82. })));
  83. const version = releaseInfo.tag_name.startsWith("v") ? releaseInfo.tag_name.substring(1) : releaseInfo.tag_name;
  84. builder_util_1.log.info({ version }, `resolve ${dependency.name}@${dependency.version}`);
  85. return version;
  86. }
  87. catch (e) {
  88. builder_util_1.log.warn(e);
  89. }
  90. throw new builder_util_1.InvalidConfigurationError(`Cannot find electron dependency to get electron version in the '${path.join(projectDir, "package.json")}'`);
  91. }
  92. const version = dependency === null || dependency === void 0 ? void 0 : dependency.version;
  93. if (version == null || !/^\d/.test(version)) {
  94. const versionMessage = version == null ? "" : ` and version ("${version}") is not fixed in project`;
  95. throw new builder_util_1.InvalidConfigurationError(`Cannot compute electron version from installed node modules - none of the possible electron modules are installed${versionMessage}.\nSee https://github.com/electron-userland/electron-builder/issues/3984#issuecomment-504968246`);
  96. }
  97. return semver.coerce(version).toString();
  98. }
  99. exports.computeElectronVersion = computeElectronVersion;
  100. function findFromPackageMetadata(packageData) {
  101. for (const name of electronPackages) {
  102. const devDependencies = packageData.devDependencies;
  103. let dep = devDependencies == null ? null : devDependencies[name];
  104. if (dep == null) {
  105. const dependencies = packageData.dependencies;
  106. dep = dependencies == null ? null : dependencies[name];
  107. }
  108. if (dep != null) {
  109. return { name, version: dep };
  110. }
  111. }
  112. return null;
  113. }
  114. //# sourceMappingURL=electronVersion.js.map