UNPKG

5.35 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __importDefault = (this && this.__importDefault) || function (mod) {
22 return (mod && mod.__esModule) ? mod : { "default": mod };
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.ModuleRebuilder = void 0;
26const debug_1 = __importDefault(require("debug"));
27const fs = __importStar(require("fs-extra"));
28const path = __importStar(require("path"));
29const cache_1 = require("./cache");
30const node_gyp_1 = require("./module-type/node-gyp");
31const prebuildify_1 = require("./module-type/prebuildify");
32const prebuild_install_1 = require("./module-type/prebuild-install");
33const d = (0, debug_1.default)('electron-rebuild');
34class ModuleRebuilder {
35 constructor(rebuilder, modulePath) {
36 this.modulePath = modulePath;
37 this.rebuilder = rebuilder;
38 this.nodeGyp = new node_gyp_1.NodeGyp(rebuilder, modulePath);
39 this.prebuildify = new prebuildify_1.Prebuildify(rebuilder, modulePath);
40 this.prebuildInstall = new prebuild_install_1.PrebuildInstall(rebuilder, modulePath);
41 }
42 get metaPath() {
43 return path.resolve(this.modulePath, 'build', this.rebuilder.buildType, '.forge-meta');
44 }
45 get metaData() {
46 return `${this.rebuilder.arch}--${this.rebuilder.ABI}`;
47 }
48 async alreadyBuiltByRebuild() {
49 if (await fs.pathExists(this.metaPath)) {
50 const meta = await fs.readFile(this.metaPath, 'utf8');
51 return meta === this.metaData;
52 }
53 return false;
54 }
55 async cacheModuleState(cacheKey) {
56 if (this.rebuilder.useCache) {
57 await (0, cache_1.cacheModuleState)(this.modulePath, this.rebuilder.cachePath, cacheKey);
58 }
59 }
60 /**
61 * Whether a prebuild-install-generated native module exists.
62 */
63 async prebuildInstallNativeModuleExists() {
64 return this.prebuildInstall.prebuiltModuleExists();
65 }
66 /**
67 * If the native module uses prebuildify, check to see if it comes with a prebuilt module for
68 * the given platform and arch.
69 */
70 async findPrebuildifyModule(cacheKey) {
71 if (await this.prebuildify.usesTool()) {
72 d(`assuming is prebuildify powered: ${this.prebuildify.moduleName}`);
73 if (await this.prebuildify.findPrebuiltModule()) {
74 await this.writeMetadata();
75 await this.cacheModuleState(cacheKey);
76 return true;
77 }
78 }
79 return false;
80 }
81 async findPrebuildInstallModule(cacheKey) {
82 if (await this.prebuildInstall.usesTool()) {
83 d(`assuming is prebuild-install powered: ${this.prebuildInstall.moduleName}`);
84 if (await this.prebuildInstall.findPrebuiltModule()) {
85 d('installed prebuilt module:', this.prebuildInstall.moduleName);
86 await this.writeMetadata();
87 await this.cacheModuleState(cacheKey);
88 return true;
89 }
90 }
91 return false;
92 }
93 async rebuildNodeGypModule(cacheKey) {
94 await this.nodeGyp.rebuildModule();
95 d('built via node-gyp:', this.nodeGyp.moduleName);
96 await this.writeMetadata();
97 await this.replaceExistingNativeModule();
98 await this.cacheModuleState(cacheKey);
99 }
100 async replaceExistingNativeModule() {
101 const buildLocation = path.resolve(this.modulePath, 'build', this.rebuilder.buildType);
102 d('searching for .node file', buildLocation);
103 const buildLocationFiles = await fs.readdir(buildLocation);
104 d('testing files', buildLocationFiles);
105 const nodeFile = buildLocationFiles.find((file) => file !== '.node' && file.endsWith('.node'));
106 const nodePath = nodeFile ? path.resolve(buildLocation, nodeFile) : undefined;
107 if (nodePath && await fs.pathExists(nodePath)) {
108 d('found .node file', nodePath);
109 if (!this.rebuilder.disablePreGypCopy) {
110 const abiPath = path.resolve(this.modulePath, `bin/${this.rebuilder.platform}-${this.rebuilder.arch}-${this.rebuilder.ABI}`);
111 d('copying to prebuilt place:', abiPath);
112 await fs.mkdir(abiPath, { recursive: true });
113 await fs.copyFile(nodePath, path.join(abiPath, `${this.nodeGyp.moduleName}.node`));
114 }
115 }
116 }
117 async writeMetadata() {
118 await fs.outputFile(this.metaPath, this.metaData);
119 }
120}
121exports.ModuleRebuilder = ModuleRebuilder;
122//# sourceMappingURL=module-rebuilder.js.map
\No newline at end of file