appInfo.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.filterCFBundleIdentifier = exports.AppInfo = exports.smarten = void 0;
  4. const builder_util_1 = require("builder-util");
  5. const semver_1 = require("semver");
  6. const macroExpander_1 = require("./util/macroExpander");
  7. const filename_1 = require("./util/filename");
  8. // fpm bug - rpm build --description is not escaped, well... decided to replace quite to smart quote
  9. // http://leancrew.com/all-this/2010/11/smart-quotes-in-javascript/
  10. function smarten(s) {
  11. // opening singles
  12. s = s.replace(/(^|[-\u2014\s(["])'/g, "$1\u2018");
  13. // closing singles & apostrophes
  14. s = s.replace(/'/g, "\u2019");
  15. // opening doubles
  16. s = s.replace(/(^|[-\u2014/[(\u2018\s])"/g, "$1\u201c");
  17. // closing doubles
  18. s = s.replace(/"/g, "\u201d");
  19. return s;
  20. }
  21. exports.smarten = smarten;
  22. class AppInfo {
  23. constructor(info, buildVersion, platformSpecificOptions = null) {
  24. this.info = info;
  25. this.platformSpecificOptions = platformSpecificOptions;
  26. this.description = smarten(this.info.metadata.description || "");
  27. this.version = info.metadata.version;
  28. if (buildVersion == null) {
  29. buildVersion = info.config.buildVersion;
  30. }
  31. const buildNumberEnvs = process.env.BUILD_NUMBER ||
  32. process.env.TRAVIS_BUILD_NUMBER ||
  33. process.env.APPVEYOR_BUILD_NUMBER ||
  34. process.env.CIRCLE_BUILD_NUM ||
  35. process.env.BUILD_BUILDNUMBER ||
  36. process.env.CI_PIPELINE_IID;
  37. this.buildNumber = info.config.buildNumber || buildNumberEnvs;
  38. if (buildVersion == null) {
  39. buildVersion = this.version;
  40. if (!(0, builder_util_1.isEmptyOrSpaces)(this.buildNumber)) {
  41. buildVersion += `.${this.buildNumber}`;
  42. }
  43. }
  44. this.buildVersion = buildVersion;
  45. if (info.metadata.shortVersion) {
  46. this.shortVersion = info.metadata.shortVersion;
  47. }
  48. if (info.metadata.shortVersionWindows) {
  49. this.shortVersionWindows = info.metadata.shortVersionWindows;
  50. }
  51. this.productName = info.config.productName || info.metadata.productName || info.metadata.name;
  52. this.sanitizedProductName = (0, filename_1.sanitizeFileName)(this.productName);
  53. this.productFilename = (platformSpecificOptions === null || platformSpecificOptions === void 0 ? void 0 : platformSpecificOptions.executableName) != null ? (0, filename_1.sanitizeFileName)(platformSpecificOptions.executableName) : this.sanitizedProductName;
  54. }
  55. get channel() {
  56. const prereleaseInfo = (0, semver_1.prerelease)(this.version);
  57. if (prereleaseInfo != null && prereleaseInfo.length > 0) {
  58. return prereleaseInfo[0];
  59. }
  60. return null;
  61. }
  62. getVersionInWeirdWindowsForm(isSetBuildNumber = true) {
  63. const [major, maybe_minor, maybe_patch] = this.version.split(".").map(versionPart => parseInt(versionPart));
  64. // The major component must be present. Here it can be either NaN or undefined, which
  65. // both returns true from isNaN.
  66. if (isNaN(major)) {
  67. throw new Error(`Invalid major number in: ${this.version}`);
  68. }
  69. // Allow missing version parts. Minor and patch can be left out and default to zero
  70. const minor = maybe_minor !== null && maybe_minor !== void 0 ? maybe_minor : 0;
  71. const patch = maybe_patch !== null && maybe_patch !== void 0 ? maybe_patch : 0;
  72. // ... but reject non-integer version parts. '1.a' is not going to fly
  73. if (isNaN(minor) || isNaN(patch)) {
  74. throw new Error(`Invalid minor or patch number in: ${this.version}`);
  75. }
  76. // https://github.com/electron-userland/electron-builder/issues/2635#issuecomment-371792272
  77. let buildNumber = isSetBuildNumber ? this.buildNumber : null;
  78. if (buildNumber == null || !/^\d+$/.test(buildNumber)) {
  79. buildNumber = "0";
  80. }
  81. return `${major}.${minor}.${patch}.${buildNumber}`;
  82. }
  83. get notNullDevMetadata() {
  84. return this.info.devMetadata || {};
  85. }
  86. get companyName() {
  87. const author = this.info.metadata.author || this.notNullDevMetadata.author;
  88. return author == null ? null : author.name;
  89. }
  90. get id() {
  91. let appId = null;
  92. for (const options of [this.platformSpecificOptions, this.info.config]) {
  93. if (options != null && appId == null) {
  94. appId = options.appId;
  95. }
  96. }
  97. const generateDefaultAppId = () => {
  98. const info = this.info;
  99. return `${info.framework.defaultAppIdPrefix}${info.metadata.name.toLowerCase()}`;
  100. };
  101. if (appId != null && (appId === "your.id" || (0, builder_util_1.isEmptyOrSpaces)(appId))) {
  102. const incorrectAppId = appId;
  103. appId = generateDefaultAppId();
  104. builder_util_1.log.warn(`do not use "${incorrectAppId}" as appId, "${appId}" will be used instead`);
  105. }
  106. return appId == null ? generateDefaultAppId() : appId;
  107. }
  108. get macBundleIdentifier() {
  109. return filterCFBundleIdentifier(this.id);
  110. }
  111. get name() {
  112. return this.info.metadata.name;
  113. }
  114. get linuxPackageName() {
  115. const name = this.name;
  116. // https://github.com/electron-userland/electron-builder/issues/2963
  117. return name.startsWith("@") ? this.sanitizedProductName : name;
  118. }
  119. get sanitizedName() {
  120. return (0, filename_1.sanitizeFileName)(this.name);
  121. }
  122. get updaterCacheDirName() {
  123. return this.sanitizedName.toLowerCase() + "-updater";
  124. }
  125. get copyright() {
  126. const copyright = this.info.config.copyright;
  127. if (copyright != null) {
  128. return (0, macroExpander_1.expandMacro)(copyright, null, this);
  129. }
  130. return `Copyright © ${new Date().getFullYear()} ${this.companyName || this.productName}`;
  131. }
  132. async computePackageUrl() {
  133. const url = this.info.metadata.homepage || this.notNullDevMetadata.homepage;
  134. if (url != null) {
  135. return url;
  136. }
  137. const info = await this.info.repositoryInfo;
  138. return info == null || info.type !== "github" ? null : `https://${info.domain}/${info.user}/${info.project}`;
  139. }
  140. }
  141. exports.AppInfo = AppInfo;
  142. /** @internal */
  143. function filterCFBundleIdentifier(identifier) {
  144. // Remove special characters and allow only alphanumeric (A-Z,a-z,0-9), hyphen (-), and period (.)
  145. // Apple documentation: https://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-102070
  146. return identifier.replace(/ /g, "-").replace(/[^a-zA-Z0-9.-]/g, "");
  147. }
  148. exports.filterCFBundleIdentifier = filterCFBundleIdentifier;
  149. //# sourceMappingURL=appInfo.js.map