-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbuilder.js
More file actions
603 lines (555 loc) · 20 KB
/
builder.js
File metadata and controls
603 lines (555 loc) · 20 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
* builder.js: Top-level include for module-smith.
*
* (C) 2012 Charlie Robbins, Bradley Meck and the Contributors
*
*/
var domain = require('domain'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
util = require('util'),
zlib = require('zlib'),
BufferedStream = require('buffered').BufferedStream,
EventEmitter = require('events').EventEmitter,
async = require('async'),
checkout = require('checkout'),
chownr = require('chownr'),
fstream = require('fstream'),
merge = require('merge-recursive'),
mkdirp = require('mkdirp'),
rimraf = require('rimraf'),
semver = require('semver'),
suspawn = require('suspawn'),
tar = require('tar'),
uidNumber = require('uid-number'),
Understudy = require('understudy').Understudy;
//
// ### function readErrorLog (file, callback)
// #### @file {string} Full path to the error log to read.
// #### @callback {function} Continuation to respond to.
// Attempts to read the specified error log file, ignores errors
// in-case the file does not exist.
//
function readErrorLog(file, callback) {
fs.readFile(file, 'utf8', function (err, text) {
return text
? callback(null, text)
: callback(null, null);
});
}
//
// ### function ModuleSmith (options)
// #### @options {Object} Options for this instance.
// #### @verions {Array} Set of node versions to build against.
// #### @defaults {Object} Default options for this instance.
// Constructor function for the ModuleSmith object responsible for building
// npm packages using the specified `options`.
//
var ModuleSmith = exports.ModuleSmith = function ModuleSmith(options) {
Understudy.call(this);
EventEmitter.call(this);
options = options || {};
var runningVersion = process.version.slice(1),
versions = options.versions || [runningVersion],
homedir = process.platform === 'win32'
? process.env.HOMEPATH
: process.env.HOME;
this.versions = versions;
this.defaults = merge.recursive({}, {
env: { PATH: process.env.PATH },
directories: {
gyp: path.join(homedir, '.node-gyp'),
home: homedir,
node: '/node'
},
engines: { node: runningVersion },
uid: 'nobody',
os: process.platform,
cpu: process.platform !== 'win32' || process.arch !== 'ia32'
? process.arch
: 'x86'
}, options.defaults || {});
return this;
};
//
// Inherit from EventEmitter.
//
util.inherits(ModuleSmith, EventEmitter);
//
// ### function createModuleSmith (options)
// #### @options {Object} Options for the ModuleSmith instance.
// Creates a new ModuleSmith instance with the specified options.
//
exports.createModuleSmith = function (options) {
return new ModuleSmith(options);
};
//
// ### function build (description, callback)
// #### @description {Object} Description of the build to run.
// #### @callback {function} Continuation to respond to.
// Executes a build run for the specified build `description`.
//
ModuleSmith.prototype.build = function build(description, callback) {
var buildDomain = domain.createDomain(),
finished = false,
self = this;
//
// ### function done ()
// Responds to the callback once.
//
function done() {
if (!finished) {
finished = true;
return callback.apply(null, arguments);
}
}
buildDomain.on('error', done);
buildDomain.run(function () {
async.waterfall([
//
// 1. Configure and spawn npm in a scaffolded directory structure
// for the build description.
//
self.perform.bind(self, 'build.configure', self.getBuildDescription(description)),
self.scaffoldBuild.bind(self),
self.downloadRepository.bind(self),
self.prepareRepository.bind(self),
self.perform.bind(self, 'npm.configure'),
self.spawnNpm.bind(self),
//
// 2. Handle npm output.
//
function handleNpmOutput(description, builder, next) {
self.emit('npm.spawned', description, builder);
builder.stdout.pipe(process.stdout);
builder.stderr.pipe(process.stderr);
builder.on('exit', function (code) {
<<<<<<< HEAD
return code !== 0
? next(new Error('npm exited with code ' + code))
: next(null, description);
});
},
//
// 3. Check the error logs (builderror.log, npm-debug.log) to
// handle silent npm failures.
//
function checkErrorLogs(buildDescription, next) {
var moduledir = buildDescription.directories.module;
async.parallel({
'builderror.log': async.apply(readErrorLog, path.join(moduledir, 'builderror.log')),
'npm-debug.log': async.apply(readErrorLog, path.join(moduledir, 'npm-debug.log'))
}, function (_, logs) {
//
// Remark: There will never be an error because `readErrorLog`
// supresses them.
//
var nonEmpty,
error,
text;
nonEmpty = Object.keys(logs).filter(function (text, file) {
if (logs[file]) {
text = text
? text + '\n' + logs[file]
: logs[file];
}
return !!logs[file];
});
if (nonEmpty.length) {
error = new Error('Error output from ' + nonEmpty.join(', '));
error.log = text;
error.code = 400;
return next(error);
=======
if (code !== 0) {
var err = new Error(description.execPath + ' exited with code ' + code);
err.stream = (fs.createReadStream(path.join(description.directories.rootdir, 'stdio.log'))).pipe(new BufferedStream());
next(err);
}
else {
next(null, description, builder);
>>>>>>> 480c61e... [dist] 0.1.3
}
next(null, buildDescription);
});
},
//
// 4. Remove `node_modules` when the `buildDescription.command` is
// "build". This handles the edge case where a binary module
// depends on **another** binary module.
//
function removeOnBuild(buildDescription, next) {
if (buildDescription.command === 'build') {
return rimraf(
path.join(buildDescription.directories.module, 'node_modules'),
function () { next(null, buildDescription) }
);
}
next(null, buildDescription);
},
//
// 5. Read and rewrite local files to handle
// edge cases in`bundledDependencies`.
//
function readLocalFiles(buildDescription, next) {
var pkgFile = path.join(buildDescription.directories.module, 'package.json');
async.waterfall([
async.parallel.bind(async, {
package: fs.readFile.bind(fs, pkgFile, 'utf8'),
installedDependencies: function (next) {
fs.readdir(path.join(buildDescription.directories.module, 'node_modules'), function (err, installedDependencies) {
if (err) {
return err.code === 'ENOENT'
? next(null, [])
: next(err, null);
}
next(null, installedDependencies.filter(function (dirname) {
return ['.bin'].indexOf(dirname) === -1;
}));
});
}
}),
function (mappings, next) {
<<<<<<< HEAD
var pkg = JSON.parse(mappings.package);
pkg.os = buildDescription.os;
if (buildDescription.command === 'install') {
pkg.bundledDependencies = mappings.installedDependencies;
}
next(null, buildDescription, pkg);
=======
var pkgJSON = JSON.parse(mappings.packageJSON);
if (!buildDescription.dontBundle) {
pkgJSON.os = buildDescription.os;
pkgJSON.bundledDependencies = mappings.installedDependencies;
}
next(null, buildDescription, pkgJSON);
>>>>>>> 480c61e... [dist] 0.1.3
}
], next);
},
self.perform.bind(self, 'npm.package'),
function rewritePackage(description, pkg, next) {
var pkgFile = path.join(description.directories.module, 'package.json');
fs.writeFile(pkgFile, JSON.stringify(pkg, null, 2) + '\n', function (err) {
next(err, description);
});
}
], function (err, buildDescription) {
if (err) {
return done(err);
}
<<<<<<< HEAD
var stream = fstream.Reader({
path: buildDescription.directories.module,
isDirectory: true,
type: 'Directory'
})
.on('error', done)
.pipe(tar.Pack({ noProprietary: true }))
.on('error', done)
.pipe(zlib.Gzip())
.on('error', done)
.pipe(new BufferedStream());
//
// Remove the error listener on the buildDomain to avoid
// mistakenly trapping errors in user code.
//
buildDomain.removeListener('error', done);
self.perform('build.output', buildDescription, stream, done);
=======
var stream = fstream.Reader({ path: buildDescription.directories.moduledir, type: "Directory", isDirectory: true })
.pipe(tar.Pack({ noProprietary: true }))
.pipe(zlib.Gzip())
.pipe(new BufferedStream());
return self.perform('build.output', buildDescription, stream, done);
>>>>>>> 480c61e... [dist] 0.1.3
});
});
};
//
// ### function getPackageNodeVersion (pkg)
// #### @description {Object} Build description to get the node version from.
// Returns the node version for the specified `pkg` to build against.
//
ModuleSmith.prototype.getPackageNodeVersion = function (description) {
var engines = description.engine || description.engines;
return typeof engines !== 'string'
? semver.maxSatisfying(this.versions, engines && engines.node || this.defaults.engines.node)
: semver.maxSatisfying(this.versions, engines);
};
//
// ### function getBuildDescription (description)
// #### @description {Object} Base build description to extend.
// Extends the build `description` with defaults.
//
ModuleSmith.prototype.getBuildDescription = function (description) {
<<<<<<< HEAD
var rootdir = description.directories.root,
builddir = path.join(rootdir, 'build'),
buildDescription;
buildDescription = merge.recursive({}, this.defaults, {
os: this.defaults.os,
cpu: this.defaults.cpu,
=======
var rootdir = description.directories.rootdir;
var builddir = rootdir + '/build';
var buildDescription = merge.recursive({}, this.defaults, {
execPath: description.execPath || this.defaults.execPath,
argv: description.argv || this.defaults.argv,
os: this.defaults.os,
cpu: this.defaults.cpu,
packageJSON: description.packageJSON,
>>>>>>> 480c61e... [dist] 0.1.3
filename: description.filename,
command: description.command || 'install',
directories: {
root: rootdir,
build: builddir,
module: path.join(builddir, 'package'),
npm: path.join(rootdir, 'npm-cache'),
tmp: path.join(rootdir, 'tmp'),
node: !description.directories || !description.directories.node
? this.defaults.directories.node
: description.directories.node
},
options: description.options || [],
uid: description.uid === null ? this.defaults.uid : description.uid,
gid: description.gid === null ? this.defaults.gid : description.gid,
env: merge.recursive({
HOME: rootdir,
ROOT: rootdir,
TMPDIR: path.join(rootdir, 'tmp'),
npm_config_production: true,
npm_config_cache: path.join(rootdir, 'npm-cache'),
npm_config_globalconfig: path.join(rootdir, 'npmglobalrc'),
npm_config_userconfig: path.join(rootdir, 'npmlocalrc'),
'npm_config_node-version': description.version || this.getPackageNodeVersion(description),
npm_config_nodedir : this.defaults.directories.gyp,
npm_config_arch: process.platform !== 'win32' || process.arch !== 'ia32'
? description.cpu
: 'ia32'
}, description.env || {})
});
//
// Streams can cuase recursion problems
//
buildDescription.repository = description.repository;
buildDescription.env.USER = buildDescription.uid;
buildDescription.env.npm_config_user = buildDescription.uid;
return buildDescription;
};
//
// ### function scaffoldBuild (buildDescription, callback)
// #### @buildDescription {Object} Build description to scaffold.
// #### @callback {function} Continuation to respond to.
// Scaffolds the `buildDescription` by creating all the directories
// associated with it.
//
ModuleSmith.prototype.scaffoldBuild = function (buildDescription, callback) {
async.parallel(
Object.keys(buildDescription.directories).filter(function (directory) {
return directory !== 'module';
}).map(function (directory) {
return mkdirp.bind(mkdirp, buildDescription.directories[directory]);
}), function (err) {
callback(err, buildDescription);
}
);
};
//
// ### function downloadRepository (buildDescription, callback)
// #### @buildDescription {Object} Build description to download.
// #### @callback {function} Continuation to respond to.
// Downloads the repository for the `buildDescription` from any
// of the valid sources:
// * git
// * tar
// * npm
//
ModuleSmith.prototype.downloadRepository = function (buildDescription, callback) {
buildDescription.repository.destination = buildDescription.directories.build;
checkout(buildDescription.repository, function (err) {
callback(err, buildDescription);
});
};
//
// ### function prepareRepository (buildDescription, callback)
// #### @buildDescription {Object} Build description to prepare.
// #### @callback {function} Continuation to respond to.
// Prepares the specfied `buildDescription` by:
// 1. Getting the uid and gid
// 2. Reading the package.json
// 3. Getting the node version
// 4. Chown the directories for the uid and gid.
//
ModuleSmith.prototype.prepareRepository = function (buildDescription, callback) {
var pkgFile = path.join(buildDescription.directories.module, 'package.json'),
dir = buildDescription.directories.root,
self = this;
//
// Helper function for updating the build description
// with contents from the local `package.json`
//
function updatePackage(contents, next) {
var pkg = JSON.parse(contents);
buildDescription.version = semver.valid(buildDescription.version)
? buildDescription.version
: self.getPackageNodeVersion(pkg);
if (!buildDescription.version) {
return next(new Error('No matching versions found'));
}
buildDescription.env.npm_config_nodedir = path.join(buildDescription.env.npm_config_nodedir, buildDescription.version);
if (typeof pkg.env === 'object' && pkg.env && !Array.isArray(pkg.env)) {
merge.recursive(buildDescription, { env: pkg.env });
}
if (process.platform === 'win32') {
buildDescription.directories.node = path.join(
buildDescription.directories.node,
'v' + buildDescription.version,
buildDescription.cpu
);
}
next();
}
//
// If there is no `process.setgid` then don't
// bother using `uidNumber` and simply continue
//
if (!process.setgid) {
delete buildDescription.gid;
delete buildDescription.uid;
return async.waterfall([
fs.readFile.bind(fs, pkgFile),
updatePackage,
], function (err) {
callback(err, buildDescription);
});
}
//
// Default to `nobody` gid.
//
buildDescription.gid = buildDescription.gid !== null ? buildDescription.gid : 'nobody';
uidNumber(buildDescription.uid, buildDescription.gid, function (err, uid, gid) {
if (err) {
return callback(err);
}
async.waterfall([
<<<<<<< HEAD
fs.readFile.bind(fs, pkgFile),
updatePackage,
=======
fs.readFile.bind(fs, pkg),
function (contents, callback) {
var pkgJSON = JSON.parse(contents);
buildDescription.version = semver.valid(buildDescription.version) ? buildDescription.version : self.getPackageNodeVersion(pkgJSON);
buildDescription.env['npm_config_node-version'] = buildDescription.version;
if (!buildDescription.version ) {
callback(new Error('No matching versions found'));
return;
}
buildDescription.env.npm_config_nodedir = path.join(buildDescription.env.npm_config_nodedir, buildDescription.version);
if (typeof pkgJSON.env === 'object' && pkgJSON.env && !Array.isArray(pkgJSON.env)) {
merge.recursive(buildDescription, {
env: pkgJSON.env
});
}
callback(null);
},
>>>>>>> 480c61e... [dist] 0.1.3
chownr.bind(chownr, dir, uid, gid)
], function (err) {
callback(err, buildDescription);
});
});
};
<<<<<<< HEAD
//
// ### function spawnNpm (description, callback)
// #### @description {Object} Build description to spawn npm for.
// #### @callback {function} Continuation to respond to.
// Spawns npm for the specified `description`.
//
ModuleSmith.prototype.spawnNpm = function spawnNpm(description, callback) {
var args = [description.command].concat(description.options || []),
cliScript = 'node_modules/npm/bin/npm-cli.js',
options,
builder,
prefix;
options = {
=======
ModuleSmith.prototype.spawnNPM = function spawnNPM(description, callback) {
if (!description.execPath) {
callback(new Error('executable not specified'));
return;
}
var argv = description.execPath.match(/\S+|'(\\[\s\S]|[^'])'|"(\\[\s\S]|[^"])"/g);
var execPath = argv.shift();
argv = argv.concat(description.argv || []);
var builder = suspawn(execPath, argv.concat(description.options), {
>>>>>>> 480c61e... [dist] 0.1.3
uid: description.uid,
gid: description.gid,
cwd: description.directories.module,
env: description.env
<<<<<<< HEAD
};
//
// Do not use `suspawn`, `gid`, or `uid` when on Windows and
// be more liberal about passing through environment variables
// required by Microsoft SDKs
//
if (process.platform === 'win32') {
delete options.uid;
delete options.gid;
//
// Remark: We could be less liberal here, but the **exact** environment
// variables necessary are not yet well known.
//
options.env = Object.keys(process.env).reduce(function (all, key) {
all[key] = process.env[key];
return all;
}, {});
Object.keys(description.env).forEach(function (key) {
if (key === 'npm_config_nodedir') { return }
options.env[key] = description.env[key];
});
//
// Set some known environment variables on Windows.
//
options.env.Path = options.env.path
|| options.env.PATH
|| options.env.Path
|| '';
delete options.env.path;
delete options.env.PATH;
prefix = process.env.SystemDrive
+ (path.join(description.directories.node, '/node_modules/npm/bin/node-gyp-bin').replace('/', '\\'))
+ ';' + process.env.Path;
options.env.Path = options.env.Path
? prefix + ';' + options.env.Path
: options.env.Path;
options.env.npm_config_user =
options.env.USERNAME =
process.env.USERNAME;
options.env.APPDATA = process.env.APPDATA;
options.env.HOMEDRIVE = process.env.HOMEDRIVE;
options.env.HOMEPATH = process.env.HOMEPATH;
//
// Setup spawn on Windows to be more explicit.
// e.g. C:\node\v0.8.25\x86\node.exe C:\node\v0.8.25\x86\node_modules\npm\bin\npm-cli.js
//
args.unshift(path.join(description.directories.node, cliScript));
builder = spawn(path.join(description.directories.node, 'node.exe'), args, options);
return callback(null, description, builder);
}
builder = suspawn('npm', args, options);
=======
});
var log = fs.createWriteStream(path.join(description.directories.rootdir, 'stdio.log'));
builder.stdout.pipe(log);
builder.stderr.pipe(log);
>>>>>>> 480c61e... [dist] 0.1.3
callback(null, description, builder);
};