forked from nodists/nodist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
231 lines (192 loc) · 6.21 KB
/
Copy pathcli.js
File metadata and controls
231 lines (192 loc) · 6.21 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
/*!
* nodist
* A Node version manager for the windows folks out there.
* Copyright 2012 by Marcel Klehr <mklehr@gmx.net>
*
* (MIT LICENSE)
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var version = process.argv[2]
, nodist = require('./lib/nodist')
, path = require('path')
, fs = require('fs')
;
// Exit with a code and (optionally) with a message
var exit = function exit(code, msg) {
if(msg) console.log(msg);
process.exit(code);
};
// Abort with an optional message being displayed
var abort = function abort(msg) {
exit(1, !msg? null : msg.split('. ').join('.\r\n'));
};
// Display the command line help and exit
function help() {
fs.readFile(__dirname+'\\usage.txt', function(err, usage) {
if(err) abort('Couldn\'t fetch help info. You\'ll have to look at the README. Sorry.');
console.log(usage.toString());
exit();
});
}
process.title = 'nodist';
// set up the necessary paths
var nodePath = process.env['NODIST_PREFIX'];
var nodistPath = __dirname;
// set up proxy
var proxy = (process.env.HTTP_PROXY || process.env.http_proxy || process.env.HTTPS_PROXY || process.env.https_proxy || "");
// want x64?
var wantX64 = process.env['NODIST_X64']!=null? process.env['NODIST_X64']==1 : (process.arch=='x64'); // if the env var is set, use its value, other wise use process.arch
// Create a nodist instance
var n = new nodist(
'http://nodejs.org/dist',
(nodePath? nodePath : nodistPath)+'\\v',
proxy.replace("https://", "http://") //replace https for http, nodejs.org/dist doesnt support https
,wantX64
);
// Parse args
argv = process.argv.splice(2);
argv.remainder = [];
if(argv.indexOf('--') !== -1) {
argv.remainder = argv.splice(argv.indexOf('--')).slice(1);
}
args = argv.join(' ');
command = argv[0];
// -V Display nodist version
if (args.match(/-v/i) !== null) {
console.log(require('./package.json').version);
exit();
}
// --HELP Display help
if (args.match(/--help/i)) {
help();
}
// (bare call of 'nodist') -> list
if (!argv[0]) {
command = 'list';
}
// LIST all installed buids
if (command.match(/^list|ls$/i)) {
nodist.determineVersion(__dirname+'\\bin\\node.exe', function (err, current) {
if(err) void(0); //don't bother, if we don't know current version
n.listInstalled(function(err, ls) {
if(err) abort(err.message+'. Sorry.');
if(n.wantX64) console.log(' (x64)')
if(ls.length == 0) abort('No builds installed, yet.');
// display all versions
ls.forEach(function(version) {
var del = (version == current) ? '> ' : ' ';// highlight current
console.log(del+version);
});
exit();
});
});
}else
// DIST list all available buids
if (command.match(/^dist|ds$/i)) {
n.listAvailable(function(err, ls) {
if(err) abort(err.message+'. Sorry.');
if(ls.length == 0) abort('No builds available. Strange...');
// display all versions
ls.forEach(function(version) {
console.log(' '+version);
});
if(n.proxy) console.log('\n (Proxy: '+n.proxy+')')
exit();
});
}else
// ADD fetch a specific build
if ((command.match(/^add|\+$/i)) && argv[1]) {
var version = argv[1];
if(version.match(/^all$/i)) {
n.installAll(function(err, real_version) {
if(err) return console.log(err.message+'.');
console.log('Installed '+real_version);
});
}else
{
n.resolveVersion(version, function(er, v) {
if(er) abort(er.message+'. Sorry.');
n.install(v, function(err) {
if(err) abort(err.message+'. Sorry.');
console.log(v);
exit();
});
})
}
}else
// REMOVE an installed build
if (command.match(/^remove|rm|-$/i) && argv[1]) {
var version = argv[1];
n.resolveVersion(version, function(er, v) {
if(er) abort(er.message+'. Sorry.');
n.remove(v, function() {
exit();
});
});
}else
// RUN a specific build
if (command.match(/^run|r$/i) && argv[1]) {
var version = argv[1];
n.resolveVersion(version, function(er, v) {
if(er) abort(er.message+'. Sorry.');
n.emulate(v, argv.remainder, function(err, code) {
if(err) abort(err.message+'. Sorry.');
exit(code);
});
});
}else
// BIN get the path to a specific version
if (command.match(/^bin$/i) && argv[1]) {
var version = argv[1];
n.resolveVersion(version, function(er, v) {
if(er) abort(er.message+'. Sorry.');
n.install(v, function(err) {
if(err) abort(err.message+'. Sorry.');
console.log(n.getPathToExe(v));
exit();
});
});
}else
// PATH get the directory of a specific version to be added to the path
if (command.match(/^path$/i) && argv[1]) {
var version = argv[1];
n.resolveVersion(version, function(er, v) {
if(er) abort(er.message+'. Sorry.');
n.install(v, function(err, v) {
if(err) abort(err.message+'. Sorry.');
console.log(path.dirname(n.getPathToExe(v)));
});
});
}else
// DEPLOY globally use the specified node version
if (argv[0]) {
var version = argv[0];
n.resolveVersion(version, function(er, v) {
if(er) abort(er.message+'. Sorry.');
n.deploy(v, function(err) {
if(err) abort(err.message+'. Sorry.');
console.log(v);
exit();
});
});
}else
// HELP display help for unknown cli parameters
{
help();
}