packageMetadata.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.checkMetadata = exports.readPackageJson = void 0;
  4. const builder_util_1 = require("builder-util");
  5. const fs_extra_1 = require("fs-extra");
  6. const path = require("path");
  7. const semver = require("semver");
  8. const normalizePackageData_1 = require("./normalizePackageData");
  9. /** @internal */
  10. async function readPackageJson(file) {
  11. const data = await (0, fs_extra_1.readJson)(file);
  12. await authors(file, data);
  13. // remove not required fields because can be used for remote build
  14. delete data.scripts;
  15. delete data.readme;
  16. (0, normalizePackageData_1.normalizePackageData)(data);
  17. return data;
  18. }
  19. exports.readPackageJson = readPackageJson;
  20. async function authors(file, data) {
  21. if (data.contributors != null) {
  22. return;
  23. }
  24. let authorData;
  25. try {
  26. authorData = await (0, fs_extra_1.readFile)(path.resolve(path.dirname(file), "AUTHORS"), "utf8");
  27. }
  28. catch (ignored) {
  29. return;
  30. }
  31. data.contributors = authorData.split(/\r?\n/g).map(it => it.replace(/^\s*#.*$/, "").trim());
  32. }
  33. /** @internal */
  34. function checkMetadata(metadata, devMetadata, appPackageFile, devAppPackageFile) {
  35. const errors = [];
  36. const reportError = (missedFieldName) => {
  37. errors.push(`Please specify '${missedFieldName}' in the package.json (${appPackageFile})`);
  38. };
  39. const checkNotEmpty = (name, value) => {
  40. if ((0, builder_util_1.isEmptyOrSpaces)(value)) {
  41. reportError(name);
  42. }
  43. };
  44. if (metadata.directories != null) {
  45. errors.push(`"directories" in the root is deprecated, please specify in the "build"`);
  46. }
  47. checkNotEmpty("name", metadata.name);
  48. if ((0, builder_util_1.isEmptyOrSpaces)(metadata.description)) {
  49. builder_util_1.log.warn({ appPackageFile }, `description is missed in the package.json`);
  50. }
  51. if (metadata.author == null) {
  52. builder_util_1.log.warn({ appPackageFile }, `author is missed in the package.json`);
  53. }
  54. checkNotEmpty("version", metadata.version);
  55. checkDependencies(metadata.dependencies, errors);
  56. if (metadata !== devMetadata) {
  57. if (metadata.build != null) {
  58. errors.push(`'build' in the application package.json (${appPackageFile}) is not supported since 3.0 anymore. Please move 'build' into the development package.json (${devAppPackageFile})`);
  59. }
  60. }
  61. const devDependencies = metadata.devDependencies;
  62. if (devDependencies != null && ("electron-rebuild" in devDependencies || "@electron/rebuild" in devDependencies)) {
  63. builder_util_1.log.info('@electron/rebuild not required if you use electron-builder, please consider to remove excess dependency from devDependencies\n\nTo ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`');
  64. }
  65. if (errors.length > 0) {
  66. throw new builder_util_1.InvalidConfigurationError(errors.join("\n"));
  67. }
  68. }
  69. exports.checkMetadata = checkMetadata;
  70. function versionSatisfies(version, range, loose) {
  71. if (version == null) {
  72. return false;
  73. }
  74. const coerced = semver.coerce(version);
  75. if (coerced == null) {
  76. return false;
  77. }
  78. return semver.satisfies(coerced, range, loose);
  79. }
  80. function checkDependencies(dependencies, errors) {
  81. if (dependencies == null) {
  82. return;
  83. }
  84. const updaterVersion = dependencies["electron-updater"];
  85. const requiredElectronUpdaterVersion = "4.0.0";
  86. if (updaterVersion != null && !versionSatisfies(updaterVersion, `>=${requiredElectronUpdaterVersion}`)) {
  87. errors.push(`At least electron-updater ${requiredElectronUpdaterVersion} is recommended by current electron-builder version. Please set electron-updater version to "^${requiredElectronUpdaterVersion}"`);
  88. }
  89. const swVersion = dependencies["electron-builder-squirrel-windows"];
  90. if (swVersion != null && !versionSatisfies(swVersion, ">=20.32.0")) {
  91. errors.push(`At least electron-builder-squirrel-windows 20.32.0 is required by current electron-builder version. Please set electron-builder-squirrel-windows to "^20.32.0"`);
  92. }
  93. const deps = ["electron", "electron-prebuilt", "electron-rebuild"];
  94. if (process.env.ALLOW_ELECTRON_BUILDER_AS_PRODUCTION_DEPENDENCY !== "true") {
  95. deps.push("electron-builder");
  96. }
  97. for (const name of deps) {
  98. if (name in dependencies) {
  99. errors.push(`Package "${name}" is only allowed in "devDependencies". ` + `Please remove it from the "dependencies" section in your package.json.`);
  100. }
  101. }
  102. }
  103. //# sourceMappingURL=packageMetadata.js.map