-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpost-install.js
81 lines (68 loc) · 2.8 KB
/
post-install.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
/* eslint-disable */
/*
Technically, this script shouldn't exist! If you're using electron-rxdb in an
Electron project set up with `electron-rebuild`, native node modules will be
automatically recompiled for Electron's version of V8.
However, if electron-rxdb is your only node module using native code, you might
not have that set up. SQLite3 is also notoriously difficult to build on Windows.
For me, it only works with VS2013.
This post-install script manually detects Electron in a package.json file in
a parent directory, and recompiles sqlite3, so you can get on to the fun stuff.
*/
var execSync = require('child_process').execSync;
var fs = require('fs');
var path = require('path');
function electronVersionFromPackageJSON(jsonPath) {
var data = fs.readFileSync(jsonPath);
var json = JSON.parse(data);
var deps = Object.assign({}, json.dependencies, json.devDependencies);
var version = deps['electron'] || deps['electron-prebuilt'] || deps['electron-prebuilt-compile'];
if (version) {
return version.replace('^', '').replace('x', '0');
}
return null;
}
// find out what version of Electron we should build against
var targetElectronVersion = null;
var dir = path.dirname(__dirname);
while (dir !== '/') {
try {
targetElectronVersion = electronVersionFromPackageJSON(path.join(dir, 'package.json'));
if (targetElectronVersion) {
break;
}
} catch (er) {
} finally {
dir = path.dirname(dir);
}
}
if (targetElectronVersion === '*') {
throw new Error("Electron version is specified as `*` in your package.json file.\nYou should lock it to at least a minor version, like ^1.4.0.")
}
if (!targetElectronVersion) {
targetElectronVersion = electronVersionFromPackageJSON(path.join(__dirname, 'package.json'));
console.warn("NOTE: Could not find `electron`, `electron-prebuilt` or `electron-prebuilt-compile`\nin a package.json file in the working path. Building SQLite3 for local\nElectron (v"+targetElectronVersion+") and `npm test`.");
}
// prepare other params
var targetArch = require('os').arch();
var targetPlatform = process.platform;
if (targetPlatform == "win32") {
var targetArch = "ia32"
}
var pathToNodeGyp = [
path.resolve(__dirname, 'node_modules', '.bin', 'node-gyp'),
path.resolve(__dirname, '..', '.bin', 'node-gyp'),
].find((p) => fs.existsSync(p));
if (!pathToNodeGyp) {
throw new Error("Couldn't find node-gyp");
}
var pathToSqlite = [
'../better-sqlite3',
'./node_modules/better-sqlite3'
].find((p) => fs.existsSync(p));
if (!pathToSqlite) {
throw new Error("Couldn't find better-sqlite3");
}
var cmd = "cd " + pathToSqlite + " && \""+pathToNodeGyp+"\" configure rebuild --msvs_version=2013 --target="+targetElectronVersion+" --arch="+targetArch+" --target_platform="+targetPlatform+" --dist-url=https://atom.io/download/electron";
console.log(cmd);
execSync(cmd);