-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (129 loc) · 3.27 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
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
var Promise = require('bluebird');
var os = require('os');
var exec = require('child_process').exec;
var wget = require('wget-improved');
var checkExists = function(path){
return new Promise(function(resolve, reject){
return require('fs').stat(path, function(err, stats){
if(err) {
if(err.code === "ENOENT") {
return resolve(false);
} else {
reject(err);
}
}
return resolve(true);
});
});
};
var osSuffix = os.type() === "Darwin" ? "darwin-x64" : "linux-x64";
var NODE = "/bin/node";
var NPM = "/bin/npm";
var TARGZ = ".tar.gz";
exports.version = "v4.4.2";
exports.downloadBaseURL = "https://nodejs.org/dist";
exports.node = function(){
var argv = normalizeOptions(arguments);
return Promise.resolve()
.then(fetchNode.bind(null, argv[1].NODE_ROOT))
.then(function(){
return node.apply(null, argv);
});
};
exports.npm = function(){
var argv = normalizeOptions(arguments);
return Promise.resolve()
.then(fetchNode.bind(null, argv[1].NODE_ROOT))
.then(function(){
return npm.apply(null, argv);
});
};
exports.fetch = _fetchNode;
function url(){
return exports.downloadBaseURL + "/" + exports.version;
}
function version(){
return exports.version;
}
function normalizeOptions(argv){
var opts = argv["1"] || {};
return [argv["0"], opts];
}
function env(){
var NAME = "node-" + exports.version + "-" + osSuffix;
var NODE_PATH = process.cwd() + "/" + NAME + NODE;
var NPM_PATH = process.cwd() + "/" + NAME + NPM;
return {
NAME: NAME,
NODE_PATH: NODE_PATH,
NPM_PATH: NPM_PATH
};
}
function pexec(cmd){
return new Promise(function(resolve, reject){
exec(cmd, function(error, stdout, stderr){
if(error) {
error.stderr = stderr;
error.stdout = stdout;
return reject(error);
}
resolve({
stdout: stdout,
stderr: stderr
});
});
});
}
function fetchNode(root){
if(root) {
return Promise.resolve();
}
return _fetchNode();
}
function _fetchNode(dir){
var cwd = dir || process.cwd();
var e = env();
return checkExists(cwd + "/" + e.NAME).then(function(yes){
if(!yes) {
return new Promise(function(resolve, reject){
var download = wget.download(url() + "/" + e.NAME + TARGZ, cwd + "/" + e.NAME + TARGZ);
download.on('error', reject);
download.on('start', function(fileSize) {
console.log("Downloading... " + url() + "/" + e.NAME + TARGZ);
});
download.on('end', function(){
extractTarGZ(cwd + "/" + e.NAME, cwd).then(function(){
resolve();
});
});
});
}
});
}
function node(cmd, opts){
if(opts.NODE_ROOT) {
opts.NODE_ROOT+=NODE;
}
return normalizeCommand(opts.NODE_ROOT || env().NODE_PATH, cmd, opts);
}
function npm(cmd, opts){
if(opts.NODE_ROOT) {
opts.NODE_ROOT+=NPM;
}
return normalizeCommand(opts.NODE_ROOT || env().NPM_PATH, cmd, opts);
}
function normalizeCommand(cmd, arg, opts){
if(!opts.workingDir) {
opts.workingDir = process.cwd();
}
return pexec([
"cd",
opts.workingDir,
"&&",
cmd,
arg
].join(" "));
}
function extractTarGZ(path, outDir){
return pexec("tar zxvf " + path + TARGZ + " -C " + outDir + " >/dev/null 2>&1");
}