-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_modules
executable file
·58 lines (42 loc) · 1.38 KB
/
fetch_modules
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
#!/usr/bin/env node
var fs = require('fs')
var http = require('http')
var path = require('path')
var url = require('url')
var es = require('esprima')
var estraverse = require('estraverse')
var mkdirp = require('mkdirp')
var base = process.argv[2]
function readFile(base) {
try {
var code = fs.readFileSync(base).toString()
estraverse.traverse(es.parse(code), {
enter: function (node, parent) {
if (node.type == 'VariableDeclarator' && node.init.callee.name == 'require') {
var p = node.init.arguments[0].value
var match = p.indexOf(':') > -1
if (match) {
var origin = url.parse(p)
var fullpath = path.join(__dirname, '/node_modules', origin.hostname, origin.pathname)
var dirname = path.dirname(fullpath)
var filename = path.basename(fullpath)
mkdirp.sync(dirname)
var writestream = fs.createWriteStream(fullpath)
var req = http.request(url.parse(p), function(res) {
res.setEncoding('utf8')
res.pipe(writestream)
})
req.on('error', function(e) {
console.log('problem getting module: ' + e.message);
})
req.end()
}
else {
readFile(path.resolve(p) + '.js')
}
}
}
})
} catch($) { return }
}
readFile(base)