Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
198 lines (173 loc) · 6.04 KB
/
Copy pathcli.js
File metadata and controls
198 lines (173 loc) · 6.04 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
/*!
* 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('./nodist')
, program = require('optimist')
, fs = require('fs')
, path = require('path')
;
var exit = function abort(code, msg) {
if(msg) console.log(msg);
process.exit(code);
}
var abort = function abort(msg) {
exit(1, msg);
}
// get path to the nodist folder
var nodistPath = fs.realpathSync(path.dirname(process.argv[1]+'.'));
process.title = 'nodist';
function help() {
console.log('A node version manager for windows');
console.log('Usage:');
console.log('');
console.log(' nodist List all installed node versions.');
console.log(' nodist list ');
console.log(' nodist ls ');
console.log('');
console.log(' nodist <version> Use the specified node version globally (downloads the executable, if necessary).');
console.log(' nodist latest Use the latest available node version globally (downloads the executable, if necessary).');
console.log('');
console.log(' nodist add <version> Download the specified node version.');
console.log(' nodist + <version> ');
console.log('');
console.log(' nodist run <version> -- <file> Run <file> with the specified node version (downloads the executable, if necessary).');
console.log(' nodist r <version> -- <file> ');
console.log('');
console.log(' nodist rm <version> Uninstall the specified node version.');
console.log(' nodist - <version> ');
console.log('');
console.log(' nodist --help Display this help');
console.log('');
console.log(' nodist -v Display nodist version');
console.log('');
console.log('Examples:');
console.log('');
console.log(' nodist 0.8.1 Use node v0.8.1 globally');
console.log(' nodist v0.5.10 Use node v0.5.10 globally');
console.log(' nodist r 0.8.1 -- foo.js -s Run `foo.js -s` with node v0.8.1, regardless of the global version');
console.log(' nodist - 0.5.10 Uninstall node v0.5.10');
}
var n = new nodist(
(process.env['NODIST_PREFIX']
? process.env['NODIST_PREFIX']
: nodistPath+'/../../' )
+'node.exe',
'http://nodejs.org/dist',
nodistPath+'/v'
);
argv = program.argv;
command = argv._[0];
// Display nodist version
if(argv.v) {
console.log(require('./package.json').version);
exit();
}
// Display help
if(argv.help) {
help();
exit();
}
// bare call -> list
if (!argv._[0] && !process.argv[2]) {
command = 'list';
}
// List all installed buids
if (command == 'list' || command == 'ls') {
nodist.determineVersion(n.target, function (err, current) {
// if(err) -- don't bother, if we don't know current version
// display all versions
n.list(function(err, ls) {
if(err) abort('Reading the version directory '+n.sourceDir+' failed.');
if(ls.length == 0) abort('No builds installed, yet.');
ls.forEach(function(version) {
var del = (version == current) ? '> ' : ' ';// highlight current
console.log(del+version);
});
exit();
});
});
}else
// Remove an installed build
if ((command == 'remove' || command == 'rm' || command == '-') && argv._[1]) {
var version = argv._[1];
// validate version number
version = nodist.validateVersion(version)
if (!version) {
abort('Please provide a valid version number.');
}
n.unlink(version, function() {
exit();
});
}else
// Run a specific build
if ((command == 'run' || command == 'r') && argv._[1]) {
var version = argv._[1];
// validate version number
version = nodist.validateVersion(version)
if (!version) {
abort('Please provide a valid version number.');
}
n.run(version, argv._.splice(2), function(err, code) {
if(err) abort(err.message+' Sorry.');
exit(code);
});
}else
// Fetch a specific build
if ((command == 'add' || command == '+') && argv._[1]) {
var version = argv._[1];
// validate version number
version = nodist.validateVersion(version)
if (!version) {
abort('Please provide a valid version number.');
}
n.fetch(version, n.sourceDir+'/'+version+'.exe', function(err) {
if(err) abort(err.message+'. Sorry.');
exit();
});
}else
// Globally use the latest available node version
if (command == 'latest') {
n.deploy('latest', function(err) {
if(err) abort(err.message+' Sorry.');
exit();
});
}else
// Globally use the specified node version
if (argv._[0] && nodist.validateVersion(argv._[0])) {
// validate version number
version = nodist.validateVersion(version)
if (!version) {
abort('Please provide a valid version number.');
}
n.deploy(version, function(err) {
if(err) abort(err.message+' Sorry.');
exit();
});
}else
// unknown parameters -> display help
{
help();
exit();
}