|
1 | | -var path = require("path"); |
2 | | -var fs = require("fs"); |
3 | | -var cp = require("child_process"); |
4 | | -var prepareForBuild = require("./prepareForBuild"); |
5 | | -var exec = require("../utils/execPromise"); |
| 1 | +var nodePreGypConstructor = require("node-pre-gyp"); |
| 2 | +var nodePreGyp = new nodePreGypConstructor.Run(); |
| 3 | +var buildFlags = require("../utils/buildFlags"); |
6 | 4 |
|
7 | 5 | module.exports = function install() { |
8 | | - var fromRegistry; |
| 6 | + // we need to add 2 blank entires to help the parser later. |
| 7 | + var argv = ["", "", "install"]; |
9 | 8 |
|
10 | | - try { |
11 | | - fs.statSync(path.join(__dirname, "..", "include")); |
12 | | - fs.statSync(path.join(__dirname, "..", "src")); |
13 | | - fs.statSync(path.join(__dirname, "..", "dist")); |
14 | | - fromRegistry = true; |
15 | | - } |
16 | | - catch(e) { |
17 | | - fromRegistry = false; |
18 | | - } |
19 | | - |
20 | | - if (!fromRegistry) { |
21 | | - console.info("[nodegit] Local install, no fetching allowed."); |
22 | | - return prepareAndBuild(); |
23 | | - } |
24 | | - if (process.env.BUILD_DEBUG) { |
25 | | - console.info("[nodegit] Doing a debug build, no fetching allowed."); |
26 | | - return prepareAndBuild(); |
27 | | - } |
28 | | - if (process.env.BUILD_ONLY) { |
29 | | - console.info("[nodegit] BUILD_ONLY is set to true, no fetching allowed."); |
30 | | - return prepareAndBuild(); |
31 | | - } |
32 | | - |
33 | | - return installPrebuilt(); |
| 9 | + if (buildFlags.mustBuild) { |
| 10 | + argv.push("--build-from-source"); |
34 | 11 |
|
35 | | - function installPrebuilt() { |
36 | | - console.info("[nodegit] Fetching binary from S3."); |
37 | | - var npg = pathForTool("node-pre-gyp"); |
38 | | - return exec("\""+ npg + "\" install --fallback-to-build=false") |
39 | | - .then( |
40 | | - function() { |
41 | | - console.info("[nodegit] Completed installation successfully."); |
42 | | - }, |
43 | | - function(err) { |
44 | | - console.info("[nodegit] Failed to install prebuilt binary:"); |
45 | | - console.error(err); |
46 | | - console.info("[nodegit] Building manually. (You'll be here a while.)"); |
47 | | - return prepareAndBuild(); |
48 | | - } |
49 | | - ); |
50 | | - } |
51 | | - |
52 | | - function pathForTool(name) { |
53 | | - var toolPath = path.resolve(".", "node_modules", ".bin", name); |
54 | | - if (process.platform == "win32") { |
55 | | - toolPath += ".cmd"; |
| 12 | + if (buildFlags.debugBuild) { |
| 13 | + argv.push("--debug"); |
56 | 14 | } |
57 | | - return toolPath; |
58 | 15 | } |
59 | | - |
60 | | - function prepareAndBuild() { |
61 | | - console.info("[nodegit] Regenerating and configuring code"); |
62 | | - return prepareForBuild() |
63 | | - .then(function() { |
64 | | - return build(); |
65 | | - }) |
66 | | - .then(function() { |
67 | | - return transpileJavascript(); |
68 | | - }); |
| 16 | + else { |
| 17 | + argv.push("--fallback-to-build"); |
69 | 18 | } |
70 | 19 |
|
71 | | - function transpileJavascript() { |
72 | | - var cmd = pathForTool("babel"); |
73 | | - var args = [ |
74 | | - "--presets", |
75 | | - "es2015", |
76 | | - "-d", |
77 | | - "./dist", |
78 | | - "./lib" |
79 | | - ]; |
80 | | - var opts = { |
81 | | - cwd: ".", |
82 | | - maxBuffer: Number.MAX_VALUE, |
83 | | - env: process.env, |
84 | | - stdio: "inherit" |
85 | | - }; |
86 | | - var home = process.platform == "win32" ? |
87 | | - process.env.USERPROFILE : process.env.HOME; |
88 | | - |
89 | | - opts.env.HOME = path.join(home, ".nodegit-gyp"); |
| 20 | + nodePreGyp.parseArgv(argv); |
90 | 21 |
|
91 | | - return new Promise(function(resolve, reject) { |
92 | | - var child = cp.spawn(cmd, args, opts); |
93 | | - child.on("close", function(code) { |
94 | | - if (code) { |
95 | | - reject(code); |
96 | | - process.exitCode = 13; |
97 | | - } |
98 | | - else { |
99 | | - resolve(); |
100 | | - } |
101 | | - }); |
102 | | - }); |
103 | | - } |
104 | | - |
105 | | - function build() { |
106 | | - console.info("[nodegit] Everything is ready to go, attempting compilation"); |
107 | | - |
108 | | - var electronVersion = process.env.ELECTRON_VERSION; |
109 | | - var nwjsVersion = process.env.NWJS_VERSION; |
110 | | - var opts = { |
111 | | - cwd: ".", |
112 | | - maxBuffer: Number.MAX_VALUE, |
113 | | - env: process.env, |
114 | | - stdio: "inherit" |
115 | | - }; |
116 | | - |
117 | | - var builder = "node-gyp"; |
118 | | - var debug = (process.env.BUILD_DEBUG ? "--debug" : ""); |
119 | | - var target = ""; |
120 | | - var arch = (process.env.TARGET_ARCH ? |
121 | | - "--arch=" + process.env.TARGET_ARCH : ""); |
122 | | - var distUrl = ""; |
123 | | - var runtime = ""; |
124 | | - |
125 | | - process.argv.forEach(function(arg) { |
126 | | - if (~arg.indexOf("electronVersion")) { |
127 | | - electronVersion = arg.split("=")[1].trim(); |
128 | | - } |
129 | | - else if (~arg.indexOf("nsjwVersion")) { |
130 | | - nwjsVersion = arg.split("=")[1].trim(); |
131 | | - } |
132 | | - }); |
| 22 | + function run() { |
| 23 | + var command = nodePreGyp.todo.shift(); |
| 24 | + if (!command) { |
| 25 | + return; |
| 26 | + } |
133 | 27 |
|
134 | | - if (electronVersion) { |
135 | | - target = "--target=" + electronVersion; |
136 | | - distUrl = "--dist-url=https://atom.io/download/atom-shell"; |
137 | | - runtime = "--runtime=electron"; |
| 28 | + nodePreGyp.commands[command.name](command.args, function (err) { |
| 29 | + if (err) { |
| 30 | + console.error(command.name + " error"); |
| 31 | + console.error("stack", err.stack); |
| 32 | + console.error("not ok"); |
| 33 | + console.log(err.message); |
| 34 | + return process.exit(1); |
138 | 35 | } |
139 | | - else if (nwjsVersion) { |
140 | | - builder = "nw-gyp"; |
141 | | - target = "--target=" + nwjsVersion; |
142 | | - runtime = "--runtime=node-webkit"; |
| 36 | + var args_array = [].slice.call(arguments, 1); |
| 37 | + if (args_array.length) { |
| 38 | + console.log.apply(console, args_array); |
143 | 39 | } |
| 40 | + // now run the next command in the queue |
| 41 | + process.nextTick(run); |
| 42 | + }); |
| 43 | + } |
144 | 44 |
|
145 | | - var home = process.platform == "win32" ? |
146 | | - process.env.USERPROFILE : process.env.HOME; |
147 | | - |
148 | | - opts.env.HOME = path.join(home, ".nodegit-gyp"); |
149 | | - |
150 | | - var cmd = pathForTool(builder); |
151 | | - var args = [ |
152 | | - "rebuild", |
153 | | - debug, |
154 | | - target, |
155 | | - arch, |
156 | | - distUrl, |
157 | | - runtime |
158 | | - ] |
159 | | - .filter(function(arg) { |
160 | | - return arg; |
161 | | - }); |
162 | | - |
163 | | - return new Promise(function(resolve, reject) { |
164 | | - var child = cp.spawn(cmd, args, opts); |
165 | | - child.on("close", function(code) { |
166 | | - console.log(code); |
167 | | - if (code) { |
168 | | - reject(code); |
169 | | - process.exitCode = 13; |
170 | | - } |
171 | | - else { |
172 | | - resolve(); |
173 | | - } |
174 | | - }); |
175 | | - }); |
176 | | - } |
| 45 | + run(); |
177 | 46 | }; |
178 | 47 |
|
179 | 48 | // Called on the command line |
180 | 49 | if (require.main === module) { |
181 | | - module |
182 | | - .exports() |
183 | | - .catch(function(err) { |
184 | | - console.error(err); |
185 | | - return -1; |
186 | | - }); |
| 50 | + module.exports(); |
187 | 51 | } |
0 commit comments