rebuild.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.rebuild = exports.Rebuilder = void 0;
  30. const debug_1 = __importDefault(require("debug"));
  31. const events_1 = require("events");
  32. const fs = __importStar(require("fs-extra"));
  33. const nodeAbi = __importStar(require("node-abi"));
  34. const os = __importStar(require("os"));
  35. const path = __importStar(require("path"));
  36. const cache_1 = require("./cache");
  37. const types_1 = require("./types");
  38. const module_rebuilder_1 = require("./module-rebuilder");
  39. const module_walker_1 = require("./module-walker");
  40. const d = (0, debug_1.default)('electron-rebuild');
  41. const defaultMode = 'sequential';
  42. const defaultTypes = ['prod', 'optional'];
  43. class Rebuilder {
  44. constructor(options) {
  45. console.log("options", options)
  46. var _a;
  47. this.platform = options.platform || process.platform;
  48. this.lifecycle = options.lifecycle;
  49. this.buildPath = options.buildPath;
  50. this.electronVersion = options.electronVersion;
  51. this.arch = options.arch || process.arch;
  52. this.force = options.force || false;
  53. this.headerURL = options.headerURL || 'https://www.electronjs.org/headers';
  54. this.mode = options.mode || defaultMode;
  55. this.debug = options.debug || false;
  56. this.useCache = options.useCache || false;
  57. this.useElectronClang = options.useElectronClang || false;
  58. this.cachePath = options.cachePath || path.resolve(os.homedir(), '.electron-rebuild-cache');
  59. this.prebuildTagPrefix = options.prebuildTagPrefix || 'v';
  60. this.msvsVersion = process.env.GYP_MSVS_VERSION;
  61. this.disablePreGypCopy = options.disablePreGypCopy || false;
  62. if (this.useCache && this.force) {
  63. console.warn('[WARNING]: Electron Rebuild has force enabled and cache enabled, force take precedence and the cache will not be used.');
  64. this.useCache = false;
  65. }
  66. if (typeof this.electronVersion === 'number') {
  67. if (`${this.electronVersion}`.split('.').length === 1) {
  68. this.electronVersion = `${this.electronVersion}.0.0`;
  69. }
  70. else {
  71. this.electronVersion = `${this.electronVersion}.0`;
  72. }
  73. }
  74. if (typeof this.electronVersion !== 'string') {
  75. throw new Error(`Expected a string version for electron version, got a "${typeof this.electronVersion}"`);
  76. }
  77. this.ABIVersion = (_a = options.forceABI) === null || _a === void 0 ? void 0 : _a.toString();
  78. const onlyModules = options.onlyModules || null;
  79. const extraModules = (options.extraModules || []).reduce((acc, x) => acc.add(x), new Set());
  80. const types = options.types || defaultTypes;
  81. this.moduleWalker = new module_walker_1.ModuleWalker(this.buildPath, options.projectRootPath, types, extraModules, onlyModules);
  82. this.rebuilds = [];
  83. d('rebuilding with args:', this.buildPath, this.electronVersion, this.arch, extraModules, this.force, this.headerURL, types, this.debug);
  84. console.log("THIS>PLATFORM", this.platform)
  85. }
  86. get ABI() {
  87. if (this.ABIVersion === undefined) {
  88. this.ABIVersion = nodeAbi.getAbi(this.electronVersion, 'electron');
  89. }
  90. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  91. return this.ABIVersion;
  92. }
  93. get buildType() {
  94. return this.debug ? types_1.BuildType.Debug : types_1.BuildType.Release;
  95. }
  96. async rebuild() {
  97. if (!path.isAbsolute(this.buildPath)) {
  98. throw new Error('Expected buildPath to be an absolute path');
  99. }
  100. this.lifecycle.emit('start');
  101. await this.moduleWalker.walkModules();
  102. for (const nodeModulesPath of await this.moduleWalker.nodeModulesPaths) {
  103. await this.moduleWalker.findAllModulesIn(nodeModulesPath);
  104. }
  105. for (const modulePath of this.moduleWalker.modulesToRebuild) {
  106. this.rebuilds.push(() => this.rebuildModuleAt(modulePath));
  107. }
  108. this.rebuilds.push(() => this.rebuildModuleAt(this.buildPath));
  109. if (this.mode !== 'sequential') {
  110. await Promise.all(this.rebuilds.map(fn => fn()));
  111. }
  112. else {
  113. for (const rebuildFn of this.rebuilds) {
  114. await rebuildFn();
  115. }
  116. }
  117. }
  118. async rebuildModuleAt(modulePath) {
  119. if (!(await fs.pathExists(path.resolve(modulePath, 'binding.gyp')))) {
  120. return;
  121. }
  122. const moduleRebuilder = new module_rebuilder_1.ModuleRebuilder(this, modulePath);
  123. this.lifecycle.emit('module-found', path.basename(modulePath));
  124. if (!this.force && await moduleRebuilder.alreadyBuiltByRebuild()) {
  125. d(`skipping: ${path.basename(modulePath)} as it is already built`);
  126. this.lifecycle.emit('module-done');
  127. this.lifecycle.emit('module-skip');
  128. return;
  129. }
  130. if (await moduleRebuilder.prebuildInstallNativeModuleExists()) {
  131. d(`skipping: ${path.basename(modulePath)} as it was prebuilt`);
  132. return;
  133. }
  134. let cacheKey;
  135. if (this.useCache) {
  136. cacheKey = await (0, cache_1.generateCacheKey)({
  137. ABI: this.ABI,
  138. arch: this.arch,
  139. debug: this.debug,
  140. electronVersion: this.electronVersion,
  141. headerURL: this.headerURL,
  142. modulePath,
  143. });
  144. const applyDiffFn = await (0, cache_1.lookupModuleState)(this.cachePath, cacheKey);
  145. if (typeof applyDiffFn === 'function') {
  146. await applyDiffFn(modulePath);
  147. this.lifecycle.emit('module-done');
  148. return;
  149. }
  150. }
  151. if (await moduleRebuilder.rebuild(cacheKey)) {
  152. this.lifecycle.emit('module-done');
  153. }
  154. }
  155. }
  156. exports.Rebuilder = Rebuilder;
  157. function rebuild(options) {
  158. console.log("(rebuild)", options)
  159. // eslint-disable-next-line prefer-rest-params
  160. d('rebuilding with args:', arguments);
  161. const lifecycle = new events_1.EventEmitter();
  162. const rebuilderOptions = { ...options, lifecycle };
  163. const rebuilder = new Rebuilder(rebuilderOptions);
  164. const ret = rebuilder.rebuild();
  165. ret.lifecycle = lifecycle;
  166. return ret;
  167. }
  168. exports.rebuild = rebuild;
  169. //# sourceMappingURL=rebuild.js.map