/
rebuildNative.js
139 lines (115 loc) · 4.08 KB
/
rebuildNative.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* A script to rebuild native dependencies against the installed Electron version.
*/
"use strict";
let childProcess = require("child_process");
let electron = require("electron");
let electronRebuild = require("electron-rebuild");
let fs = require("fs");
let path = require("path");
let rimraf = require("rimraf");
// KEEP THIS UPDATED: Some modules need to have their built directory cleaned before rebuilding. Put any such known modules in this list.
let knownNativeModules = [
{
packageName: "serialport",
cleanDir: path.join("build", "release")
},
{
packageName: "keytar",
cleanDir: path.join("build", "release")
}
];
let targetDir = path.join(__dirname, "..", "src", "node_modules");
if (process.argv.length >= 3) {
targetDir = process.argv[2];
if (!/node_modules\/?$/.test(targetDir)) {
targetDir = path.join(targetDir, "node_modules");
}
}
let foldersToRebuild = [
targetDir
];
function findFinalLinkTarget(p) {
let foundFinal = false;
let target = p;
while (!foundFinal) {
try {
target = fs.readlinkSync(target);
} catch (e) {
foundFinal = true;
}
}
return target;
}
function cleanKnownModulesRecursive(p) {
knownNativeModules.forEach((nm) => {
let fullPackagePath = path.join(p, nm.packageName);
if (fs.existsSync(fullPackagePath)) {
let fullBuildDir = path.join(fullPackagePath, nm.cleanDir);
console.log(` Cleaning ${fullBuildDir}`);
rimraf.sync(fullBuildDir);
}
});
let recurseFolders = [];
fs.readdirSync(p).forEach((item) => {
let fullDependencyPath = path.join(p, item);
let nodeModulesPath = path.join(fullDependencyPath, "node_modules");
if (fs.statSync(fullDependencyPath).isDirectory() && fs.existsSync(nodeModulesPath)) {
recurseFolders.push(nodeModulesPath);
}
});
recurseFolders.forEach((f) => {
cleanKnownModulesRecursive(f);
});
}
electronRebuild.shouldRebuildNativeModules(electron)
.then((shouldBuild) => {
if (!shouldBuild) {
console.log("It doesn't look like you need to rebuild");
return Promise.resolve();
}
console.log("Detecting linked modules (\"npm link\")...");
fs.readdirSync(targetDir).forEach((m) => {
let moduleRootPath = path.resolve(targetDir, m);
let stat = fs.statSync(moduleRootPath);
if (stat.isDirectory()) {
let finalLink = path.resolve(findFinalLinkTarget(moduleRootPath));
if (finalLink !== moduleRootPath) {
console.log(` Detected npm link: ${m}`);
//foldersToRebuild.push(path.join(finalLink, "node_modules"));
}
}
});
console.log("Cleaning known native modules...");
foldersToRebuild.forEach((f) => {
cleanKnownModulesRecursive(f);
});
console.log("Rebuilding native modules...");
let electronVersion = childProcess.execSync(`${electron} --version`, {
encoding: "utf8",
});
electronVersion = electronVersion.match(/v(\d+\.\d+\.\d+)/)[1];
let hadError = false;
return foldersToRebuild.reduce((soFar, currentFolder) => {
return soFar
.then(() => {
console.log(` ${currentFolder}`);
return electronRebuild.rebuildNativeModules(electronVersion, currentFolder);
})
.then(() => electronRebuild.preGypFixRun(currentFolder, true, electron))
.catch((e) => {
hadError = true;
console.error(e);
});
}, electronRebuild.installNodeHeaders(electronVersion))
.then(() => {
if (hadError) {
throw new Error();
}
});
})
.then(() => {
console.log("Done!")
}, (e) => {
console.error("Failed to rebuild some native modules:" + e);
});