yarn.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.rebuild = exports.nodeGypRebuild = exports.getGypEnv = exports.installOrRebuild = void 0;
  4. const builder_util_1 = require("builder-util");
  5. const fs_extra_1 = require("fs-extra");
  6. const os_1 = require("os");
  7. const path = require("path");
  8. const electronVersion_1 = require("../electron/electronVersion");
  9. const electronRebuild = require("@electron/rebuild");
  10. const searchModule = require("@electron/rebuild/lib/src/search-module");
  11. async function installOrRebuild(config, appDir, options, forceInstall = false) {
  12. console.log("install or rebuild", { config, options })
  13. let isDependenciesInstalled = false;
  14. for (const fileOrDir of ["node_modules", ".pnp.js"]) {
  15. if (await (0, fs_extra_1.pathExists)(path.join(appDir, fileOrDir))) {
  16. isDependenciesInstalled = true;
  17. break;
  18. }
  19. }
  20. if (forceInstall || !isDependenciesInstalled) {
  21. const effectiveOptions = {
  22. buildFromSource: config.buildDependenciesFromSource === true,
  23. additionalArgs: (0, builder_util_1.asArray)(config.npmArgs),
  24. ...options,
  25. };
  26. await installDependencies(appDir, effectiveOptions);
  27. }
  28. else {
  29. await rebuild(appDir, config.buildDependenciesFromSource === true, options);
  30. }
  31. }
  32. exports.installOrRebuild = installOrRebuild;
  33. function getElectronGypCacheDir() {
  34. return path.join((0, os_1.homedir)(), ".electron-gyp");
  35. }
  36. function getGypEnv(frameworkInfo, platform, arch, buildFromSource) {
  37. const npmConfigArch = arch === "armv7l" ? "arm" : arch;
  38. const common = {
  39. ...process.env,
  40. npm_config_arch: npmConfigArch,
  41. npm_config_target_arch: npmConfigArch,
  42. npm_config_platform: platform,
  43. npm_config_build_from_source: buildFromSource,
  44. // required for node-pre-gyp
  45. npm_config_target_platform: platform,
  46. npm_config_update_binary: true,
  47. npm_config_fallback_to_build: true,
  48. };
  49. if (platform !== process.platform) {
  50. common.npm_config_force = "true";
  51. }
  52. if (platform === "win32" || platform === "darwin") {
  53. common.npm_config_target_libc = "unknown";
  54. }
  55. if (!frameworkInfo.useCustomDist) {
  56. return common;
  57. }
  58. // https://github.com/nodejs/node-gyp/issues/21
  59. return {
  60. ...common,
  61. npm_config_disturl: "https://electronjs.org/headers",
  62. npm_config_target: frameworkInfo.version,
  63. npm_config_runtime: "electron",
  64. npm_config_devdir: getElectronGypCacheDir(),
  65. };
  66. }
  67. exports.getGypEnv = getGypEnv;
  68. function checkYarnBerry() {
  69. var _a;
  70. const npmUserAgent = process.env["npm_config_user_agent"] || "";
  71. const regex = /yarn\/(\d+)\./gm;
  72. const yarnVersionMatch = regex.exec(npmUserAgent);
  73. const yarnMajorVersion = Number((_a = yarnVersionMatch === null || yarnVersionMatch === void 0 ? void 0 : yarnVersionMatch[1]) !== null && _a !== void 0 ? _a : 0);
  74. return yarnMajorVersion >= 2;
  75. }
  76. function installDependencies(appDir, options) {
  77. const platform = options.platform || process.platform;
  78. const arch = options.arch || process.arch;
  79. const additionalArgs = options.additionalArgs;
  80. builder_util_1.log.info({ platform, arch, appDir }, `installing production dependencies`);
  81. let execPath = process.env.npm_execpath || process.env.NPM_CLI_JS;
  82. const execArgs = ["install"];
  83. const isYarnBerry = checkYarnBerry();
  84. if (!isYarnBerry) {
  85. if (process.env.NPM_NO_BIN_LINKS === "true") {
  86. execArgs.push("--no-bin-links");
  87. }
  88. execArgs.push("--production");
  89. }
  90. if (!isRunningYarn(execPath)) {
  91. execArgs.push("--prefer-offline");
  92. }
  93. if (execPath == null) {
  94. execPath = getPackageToolPath();
  95. }
  96. else if (!isYarnBerry) {
  97. execArgs.unshift(execPath);
  98. execPath = process.env.npm_node_execpath || process.env.NODE_EXE || "node";
  99. }
  100. if (additionalArgs != null) {
  101. execArgs.push(...additionalArgs);
  102. }
  103. return (0, builder_util_1.spawn)(execPath, execArgs, {
  104. cwd: appDir,
  105. env: getGypEnv(options.frameworkInfo, platform, arch, options.buildFromSource === true),
  106. });
  107. }
  108. async function nodeGypRebuild(arch) {
  109. return rebuild(process.cwd(), false, arch);
  110. }
  111. exports.nodeGypRebuild = nodeGypRebuild;
  112. function getPackageToolPath() {
  113. if (process.env.FORCE_YARN === "true") {
  114. return process.platform === "win32" ? "yarn.cmd" : "yarn";
  115. }
  116. else {
  117. return process.platform === "win32" ? "npm.cmd" : "npm";
  118. }
  119. }
  120. function isRunningYarn(execPath) {
  121. const userAgent = process.env.npm_config_user_agent;
  122. return process.env.FORCE_YARN === "true" || (execPath != null && path.basename(execPath).startsWith("yarn")) || (userAgent != null && /\byarn\b/.test(userAgent));
  123. }
  124. /** @internal */
  125. async function rebuild(appDir, buildFromSource, options) {
  126. builder_util_1.log.info({ appDir, arch: options.arch, platform: options.platform }, "executing @electron/rebuild");
  127. const effectiveOptions = {
  128. buildPath: appDir,
  129. electronVersion: await (0, electronVersion_1.getElectronVersion)(appDir),
  130. arch: options.arch,
  131. platform: options.platform,
  132. force: true,
  133. debug: builder_util_1.log.isDebugEnabled,
  134. projectRootPath: await searchModule.getProjectRootPath(appDir),
  135. };
  136. if (buildFromSource) {
  137. effectiveOptions.prebuildTagPrefix = "totally-not-a-real-prefix-to-force-rebuild";
  138. }
  139. return electronRebuild.rebuild(effectiveOptions);
  140. }
  141. exports.rebuild = rebuild;
  142. //# sourceMappingURL=yarn.js.map