UNPKG

8.75 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.8.0
2var buildModule, configureModule, exec, fetchModule, fs, os, path, queue, rmDirRecursiveSync, runCmd, semver;
3
4fs = require('fs');
5
6path = require('path');
7
8os = require('os');
9
10exec = require('child_process').exec;
11
12queue = require('queue-async');
13
14semver = require('semver');
15
16require('terminal-colors');
17
18rmDirRecursiveSync = function(dirPath) {
19 var e, filePath, files, i;
20 try {
21 files = fs.readdirSync(dirPath);
22 } catch (_error) {
23 e = _error;
24 return;
25 }
26 if (files.length > 0) {
27 i = 0;
28 while (i < files.length) {
29 filePath = dirPath + "/" + files[i];
30 if (fs.statSync(filePath).isFile()) {
31 fs.unlinkSync(filePath);
32 } else {
33 rmDirRecursiveSync(filePath);
34 }
35 i++;
36 }
37 }
38 try {
39 fs.rmdirSync(dirPath);
40 } catch (_error) {}
41};
42
43module.exports.runCmd = runCmd = function(cmd, opts, quiet, cb) {
44 var child, errMsg;
45 if (!quiet) {
46 console.log(("" + (opts.cwd || '') + "> " + cmd).lightBlue);
47 }
48 errMsg = '';
49 child = exec(cmd, opts);
50 if (!quiet) {
51 child.stdout.pipe(process.stdout);
52 }
53 if (!quiet) {
54 child.stderr.pipe(process.stderr);
55 } else {
56 child.stderr.on('data', function(chunk) {
57 errMsg += chunk;
58 });
59 }
60 child.on('exit', function(code) {
61 if (code !== 0) {
62 return typeof cb === "function" ? cb(new Error("command failed: " + cmd + "\n" + errMsg)) : void 0;
63 }
64 return typeof cb === "function" ? cb() : void 0;
65 });
66};
67
68configureModule = function(moduleName, opts, nodePreGypParams, cb) {
69 var cmd, cmdOpts;
70 cmd = "node-gyp configure " + nodePreGypParams;
71 cmdOpts = {
72 cwd: path.join('node_modules', moduleName)
73 };
74 if (opts.cwd) {
75 cmdOpts.cwd = path.join(opts.cwd, cmdOpts.cwd);
76 }
77 runCmd(cmd, cmdOpts, opts.quiet, cb);
78};
79
80module.exports.fetchModule = fetchModule = function(moduleName, opts, cb) {
81 var cmd, cmdOpts;
82 if (!opts.quiet) {
83 console.log("fetching " + (moduleName || 'all'));
84 }
85 if (moduleName == null) {
86 moduleName = '';
87 }
88 if (opts.tarball) {
89 moduleName = opts.tarball;
90 }
91 cmd = "npm install " + moduleName;
92 if (!opts.runScripts) {
93 cmd += ' --ignore-scripts';
94 }
95 if (opts.save) {
96 cmd += ' --save';
97 }
98 if (opts.saveDev) {
99 cmd += ' --save-dev';
100 }
101 cmdOpts = {};
102 if (opts.cwd) {
103 cmdOpts.cwd = opts.cwd;
104 }
105 runCmd(cmd, cmdOpts, opts.quiet, cb);
106};
107
108module.exports.buildModule = buildModule = function(moduleName, opts, cb) {
109 var arch, buildPkg, cmd, cmdOpts, config, cwd, fakeNodePreGyp, modulePath, module_path, modules, nodePreGypParams, nodePreGypPkg, nodePreGypVersion, node_abi, platform, preGyp, preScripts, projectPkg, q, scriptName, target, _i, _j, _len, _len1, _ref, _ref1, _ref2;
110 projectPkg = require(path.join(process.cwd(), 'package.json'));
111 config = (_ref = projectPkg.config) != null ? _ref['atom-shell'] : void 0;
112 target = opts.target || (config != null ? config.version : void 0);
113 arch = opts.arch || (config != null ? config.arch : void 0);
114 platform = opts['target-platform'] || (config != null ? config['platform'] : void 0) || os.platform();
115 modules = [];
116 nodePreGypParams = '';
117 if (moduleName.indexOf(' ') !== -1) {
118 modules = moduleName.split(' ');
119 }
120 if (!moduleName) {
121 modules = Object.keys(projectPkg.dependencies);
122 }
123 if (modules.length !== 0) {
124 q = queue(1);
125 for (_i = 0, _len = modules.length; _i < _len; _i++) {
126 moduleName = modules[_i];
127 q.defer(buildModule, moduleName);
128 q.awaitAll(function(err) {
129 return cb();
130 });
131 }
132 return;
133 }
134 moduleName = moduleName.split('@')[0];
135 cwd = process.cwd();
136 if (opts.cwd) {
137 cwd = path.join(cwd, opts.cwd);
138 }
139 modulePath = path.join(cwd, 'node_modules', moduleName);
140 if (!fs.existsSync(path.join(modulePath, 'package.json'))) {
141 return typeof cb === "function" ? cb(new Error("aspm: module not found '" + moduleName + "'")) : void 0;
142 }
143 if (!fs.existsSync(path.join(modulePath, 'binding.gyp'))) {
144 return cb();
145 }
146 if (!target) {
147 return typeof cb === "function" ? cb(new Error("aspm: no atom-shell version specified.")) : void 0;
148 }
149 if (!arch) {
150 return typeof cb === "function" ? cb(new Error("aspm: no target architecture specified.")) : void 0;
151 }
152 buildPkg = require(path.join(modulePath, 'package.json'));
153 fakeNodePreGyp = (((_ref1 = buildPkg.dependencies) != null ? _ref1['node-pre-gyp'] : void 0) != null) && (buildPkg.binary != null);
154 if (fakeNodePreGyp) {
155 nodePreGypPkg = require(path.join(modulePath, 'node_modules', 'node-pre-gyp', 'package.json'));
156 nodePreGypVersion = nodePreGypPkg.version;
157 node_abi = "atom-shell-v" + target;
158 if (semver.lte(nodePreGypVersion, '999.0.0')) {
159 node_abi = (function() {
160 var atomshellToModulesVersion, atomshellToNodeVersion, lookupTable, targetParts, targetSimplified;
161 atomshellToModulesVersion = {
162 '0.20.x': 17,
163 "0.19.x": 16,
164 "0.18.x": 16,
165 "0.17.x": 15,
166 "0.16.x": 14
167 };
168 atomshellToNodeVersion = {
169 '0.20.x': '0.13.0-pre',
170 '0.19.x': '0.11.14',
171 '0.18.x': '0.11.14',
172 '0.17.x': '0.11.14',
173 '0.16.x': '0.11.13'
174 };
175 lookupTable = (function() {
176 if (semver.lt(nodePreGypVersion, '0.6.0')) {
177 return atomshellToModulesVersion;
178 }
179 return atomshellToNodeVersion;
180 })();
181 targetParts = target.split('.');
182 targetParts[2] = 'x';
183 targetSimplified = targetParts.join('.');
184 return "node-v" + lookupTable[targetSimplified];
185 })();
186 }
187 module_path = buildPkg.binary.module_path;
188 module_path = module_path.replace('{node_abi}', node_abi).replace('{platform}', os.platform()).replace('{arch}', arch).replace('{module_name}', buildPkg.binary.module_name).replace('{configuration}', 'Release').replace('{version}', buildPkg.version);
189 preGyp = {
190 module_name: buildPkg.binary.module_name,
191 module_path: path.join('..', module_path)
192 };
193 }
194 if (fakeNodePreGyp) {
195 nodePreGypParams += " --module_name=" + preGyp.module_name;
196 nodePreGypParams += " --module_path=" + preGyp.module_path;
197 }
198 q = queue(1);
199 preScripts = 'prepublish preinstall'.split(' ');
200 if (opts.compatibility) {
201 preScripts.push('install');
202 }
203 cmdOpts = {
204 cwd: path.join('node_modules', moduleName)
205 };
206 if (opts.cwd) {
207 cmdOpts.cwd = path.join(opts.cwd, cmdOpts.cwd);
208 }
209 for (_j = 0, _len1 = preScripts.length; _j < _len1; _j++) {
210 scriptName = preScripts[_j];
211 if (((_ref2 = buildPkg.scripts) != null ? _ref2[scriptName] : void 0) != null) {
212 cmd = "npm run " + scriptName;
213 q.defer(runCmd, cmd, cmdOpts, opts.quiet);
214 }
215 }
216 q.awaitAll(function(err) {
217 configureModule(moduleName, opts, nodePreGypParams, function(err) {
218 if (!opts.quiet) {
219 console.log("building " + moduleName + " for Atom-Shell v" + target + " " + (os.platform()) + " " + arch);
220 }
221 cmd = "node-gyp rebuild --target=" + target + " --arch=" + arch + " --target_platform=" + platform + " --dist-url=https://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/dist " + nodePreGypParams;
222 runCmd(cmd, cmdOpts, opts.quiet, function(err) {
223 var _k, _len2, _ref3, _ref4;
224 if (err) {
225 return typeof cb === "function" ? cb(err) : void 0;
226 }
227
228 /*
229 unless fakeNodePreGyp
230 * we move the node_module.node file to lib/binding
231 try fs.mkdirSync "node_modules/#{moduleName}/lib/binding"
232 fs.renameSync "node_modules/#{moduleName}/build/Release/node_#{moduleName}.node", "node_modules/#{moduleName}/lib/binding/node_#{moduleName}.node"
233 rmDirRecursiveSync "node_modules/#{moduleName}/build/"
234 */
235 q = queue(1);
236 _ref3 = 'postinstall'.split(' ');
237 for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
238 scriptName = _ref3[_k];
239 if (((_ref4 = buildPkg.scripts) != null ? _ref4[scriptName] : void 0) != null) {
240 cmd = "npm run " + scriptName;
241 q.defer(runCmd, cmd, cmdOpts, opts.quiet);
242 }
243 }
244 return q.awaitAll(function(err) {
245 return typeof cb === "function" ? cb() : void 0;
246 });
247 });
248 });
249 });
250};
251
252module.exports.installModule = function(moduleName, opts, cb) {
253 if (!opts.quiet) {
254 console.log("installing " + (moduleName || 'all'));
255 }
256 fetchModule(moduleName, opts, function(err) {
257 if (err) {
258 return typeof cb === "function" ? cb(err) : void 0;
259 }
260 buildModule(moduleName, opts, function(err) {
261 return typeof cb === "function" ? cb(err) : void 0;
262 });
263 });
264};