-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
68 lines (64 loc) · 1.4 KB
/
index.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
var assert = require('assert');
var fs = require('fs');
var spawnSync = require('child_process').spawnSync;
var log = process.logging ? process.logging : function () {};
// core modules
exports.coreModules = [
'assert',
'child_process',
'cluster',
'crypto',
'dgram',
'dns',
'domain',
'events',
'fs',
'http',
'https',
'net',
'path',
'readline',
'repl',
'stream',
'tls',
'tty',
'url',
'util',
'vm',
'zlib'
].reduce(function (acc, coreModule) {
acc[coreModule] = true;
return acc;
}, {});
module.exports = new Proxy(exports, {
get: function (target, prop, receiver) {
return getModule(prop);
}
});
function getModule(moduleName) {
if (moduleName === 'hoarders') {
return module.exports;
}
var module = null;
try {
module = require(moduleName);
} catch (err) {
assert(
!exports.coreModules[moduleName],
'a core module should require first try'
);
log('error on first require(%module): %err', {
err: err,
module: moduleName
});
log('attempting to npm install %module...', {module: moduleName});
var npmResult = spawnSync('npm', ['install', moduleName], {
stdio: ['pipe', 'inherit', 'inherit']
});
if (npmResult.error) throw npmResult.error;
if (npmResult.status)
throw new Error('npm install returned status: ' + npmResult.status);
module = require(moduleName);
}
return module;
}