Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Dec 11, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
Latest commit
411 lines (369 loc) · 10.8 KB
/
Copy pathindex.js
File metadata and controls
411 lines (369 loc) · 10.8 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
varpath=require('path')
,util=require('util')
,middleware=require('cli-middleware')
,utils=require('cli-util')
,define=utils.define
,merge=utils.merge
,funcname=utils.funcname
,logger=require('cli-logger')
,cli=require('cli-define')
,Program=cli.Program
,Option=cli.Option
,Flag=cli.Flag
,Command=cli.Command
,key=cli.key
,clierr=require('cli-error')
,errors=clierr.errors
,ErrorDefinition=clierr.ErrorDefinition
,CliError=clierr.CliError
// TODO: make ExecError available
,errs={}
,types=require('cli-types')
,conflict=require('cli-conflict')
,system=require('cli-system')
,syslog=require('./lib/syslog').log;
vardebug=!!process.env.CLI_TOOLKIT_DEBUG;
// backward compatible property access
system.include(true);
varall=system.standard();
errs.type=types.ArgumentTypeError;
vardefaults=require('./lib/defaults');
varCommandProgram=function(){
Program.apply(this,arguments);
varconf=merge(defaults,{},{copy: true})
// private
define(this,'_middleware',undefined,true);
define(this,'_conf',conf,true);
define(this,'_request',undefined,true);
define(this,'_middlecache',[],true);
// TODO: remove this circular reference
this._conf.stash=this;
// public
define(this,'errors',errors,false);
}
util.inherits(CommandProgram,Program);
/**
* Get or set the middleware request object.
*
* @param req The request object.
*/
functionrequest(req){
if(!arguments.length)returnthis._request;
this._request=req;
returnthis;
}
define(CommandProgram.prototype,'request',request,false);
/**
* Wrap an error from an error definition, string
* or Error instance.
*
* @param err The error definition, string or Error.
* @param parameters The message replacement parameters.
* @param source A source error to wrap.
*/
functionwrap(err,parameters,source){
varmsg='Cannot wrap invalid error \''+err+'\'';
if(!err){
thrownewTypeError(msg);
}
vare,code=err.code||errors.EGENERIC.code;
if(errinstanceofCliError){
e=err;
}elseif(errinstanceofErrorDefinition){
e=err.toError(source,parameters);
if(!source)e.shift();
e.parameters=parameters;
//e.key = err.key;
}elseif(errinstanceofError){
e=newCliError(err,code,parameters);
e.key=err.key||errors.EGENERIC.key;
err.key=e.key;
}elseif(typeoferr==='string'){
e=newCliError(source||err,code,parameters);
}else{
thrownewTypeError(msg);
}
returne;
}
define(CommandProgram.prototype,'wrap',wrap,false);
/**
* Raise an error from an error definition or error
* instance.
*
* @param err The error definition.
* @param parameters The message replacement parameters.
* @param source A source error to wrap.
*/
functionraise(err,parameters,source){
vare=this.wrap(err,parameters,source);
this.emit('raise',e,errors,err,parameters,source);
this.emit('error',e,errors,err,parameters,source);
returne;
}
define(CommandProgram.prototype,'raise',raise,false);
/**
* Define program middleware.
*/
functionuse(middleware){
vari,nm,closure,conf=this.configure();
varind=-1,args=[].slice.call(arguments,1);
if(middleware===false){
this._middlecache=[];
this._middleware=[];
returnthis;
}
if(typeofmiddleware==='number'){
ind=middleware;
middleware=arguments[1];
args=[].slice.call(arguments,2);
}
if(!arguments.length&&this._middleware===undefined){
//if(!all) {
//all = system.standard();
//}
for(i=0;i<all.length;i++){
if(conf&&conf.middleware){
closure=all[i].call(this);
if(typeofclosure!=='function'){
continue;
}
nm=funcname(closure);
//console.log('got name %s', nm);
if(nm&&conf.middleware[nm]===false){
continue;
}
}
this.use(all[i]);
}
returnthis;
}
if(typeofmiddleware!='function'){
thrownewError('Invalid middleware, must be a function');
}
closure=middleware.apply(this,args);
if(this._middlecache&&~this._middlecache.indexOf(middleware)){
thrownewError('Invalid middleware, duplicate detected');
}
if(typeof(closure)=='function'){
if(this._middleware===undefined)this._middleware=[];
if(ind<0||ind>=this._middleware.length){
//console.log('adding closure... %s', closure);
this._middleware.push(closure);
}else{
this._middleware.splice(ind,0,closure);
}
}
this._middlecache.push(middleware);
returnthis;
}
define(CommandProgram.prototype,'use',use,false);
/**
* Default error handler for the error event.
*
* @param e The error instance.
*/
functionerror(e){
varconf=this.configure();
if(!(einstanceofCliError)){
e=this.wrap(e);
}
vartrace=
(conf.trace!==undefined)
? conf.trace : (e.code===errors.EUNCAUGHT.code);
//console.log('conf.trace %s', conf.trace);
//console.log('trace %s', trace);
//console.log('code %s', e.code);
varlogger=this.log&&typeof(this.log.error)==='function';
if(logger&&conf.error.log){
if(conf.error.log.print){
varargs=(e.parameters||[]).slice(0);
args.unshift(e.message);
this.log.error.apply(this.log,args);
if(trace){
varstack=e.stack;
if(stack){
varlines=e._stacktrace||[];
lines=lines.map(function(value){
return' '+value;
})
stack=lines.join('\n');
this.log.error(stack);
}
}
}else{
//console.dir(e);
this.log.error(e);
}
}else{
e.error(trace)
}
if(conf.exit)e.exit();
}
define(CommandProgram.prototype,'error',error,false);
/**
* Assigns configuration information to the program.
*
* @param conf The program configuration.
*/
functionconfigure(conf){
if(!arguments.length)returnthis._conf;
conf=conf||{};
this._conf=merge(conf,this._conf);
if(conf.stash){
this._conf.stash=conf.stash;
}
// load custom error definitions
if(conf&&conf.error
&&typeofconf.error==='object'&&conf.error.locales){
clierr.file(conf.error,function(err){
if(err)throwerr;
})
}
returnthis;
}
define(CommandProgram.prototype,'configure',configure,false);
/**
* Adds a version flag to the program.
*
* @param semver A specific version number.
* @param name The argument name.
* @param description The argument description.
* @param action A function to invoke.
*/
// WARN: do not use, this is too overloaded, will be removed
// WARN: do: cli.use(require('cli-mid-version')) instead for the middleware
functionversion(semver,name,description,action){
if(arguments.length===1&&typeofsemver==='string'){
this._version=semver;
returnthis;
}
if(!arguments.length&&this._options.versionopt)returnthis._version;
returnthis.use(system.version,semver,name,description,action);
}
define(CommandProgram.prototype,'version',version,false);
/**
* Reset all values assigned to options.
*
* Designed to be used in the scenario that you wish
* to re-parse through the same instance and ensure that
* values do not carry through.
*/
functionreset(target){
target=target||this.configure().stash||this;
varopts=target.options(),k;
for(kinopts){
deleteopts[k].value(undefined);
}
varcmds=target.commands();
for(kincmds){
reset(cmds[k]);
}
}
define(CommandProgram.prototype,'reset',reset,false);
/**
* Adds a help flag to the program.
*
* @param name The argument name.
* @param description The argument description.
* @param action A function to invoke.
*/
functionhelp(name,description,action){
returnthis.use(system.help,name,description,action);
}
define(CommandProgram.prototype,'help',help,false);
/**
* Parse the supplied arguments and execute any commands
* found in the arguments, preferring the built in commands
* for help and version.
*
* @param args The arguments to parse, default is process.argv.slice(2).
* @param req An existing request object to use.
* @param cb A complete callback function.
*/
functionparse(args,req,cb){
if(typeofreq==='function'){
cb=req;
req=null;
}
varconf=this.configure();
args=args||process.argv.slice(2);
conflict.call(this);
if(this._middleware===undefined){
this.use();
}
varopts={
syslog: syslog,
list: this._middleware,
errors: this.errors,
bail: conf.bail,
throws: true,
intercept: conf.error.intercept,
scope: this
}
varrunner=middleware(opts);
// NOTE: want to be using this again!
//runner(args, req, cb);
// NOTE: backward compatible argument order switch
// NOTE: allows us to upgrade cli-middleware but not
// NOTE: break absolutely everything
runner(args,req,function(err,req){
if(cb){
cb.call(this,req,err);
}else{
this.emit('complete',req,err);
}
});
returnthis;
}
define(CommandProgram.prototype,'parse',parse,false);
module.exports=function(package,name,description,options){
options=options||{};
varlocales=path.join(__dirname,'lib','error','locales');
clierr.file({locales: locales});
varprogram=cli(package,name,description,CommandProgram);
varlisteners=process.listeners('uncaughtException');
if(!listeners.length){
process.on('uncaughtException',function(err){
err.code=errors.EUNCAUGHT.code;
//console.dir(err);
//console.log(err.stack);
program.raise(err);
})
}
if(debug){
syslog.level(logger.TRACE);
}
if(options.log===false){
syslog.level(logger.NONE);
}
// TODO: allow setting error configuration on the program configuration
clierr({name: program.name()});
returnprogram;
}
// classes
module.exports.Interface=require('./interface');
module.exports.CommandProgram=CommandProgram;
module.exports.Program=Program;
module.exports.Command=Command;
module.exports.Option=Option;
module.exports.Flag=Flag;
module.exports.ErrorDefinition=ErrorDefinition;
module.exports.CliError=CliError;
// library exports
module.exports.defaults=defaults;
// deprecated legacy access via middleware
module.exports.middleware=system;
// new access via system (use this)
module.exports.system=system;
module.exports.help=system.help.action;
module.exports.version=system.version.action;
// dependency exports
module.exports.util=utils;
module.exports.define=cli;
module.exports.logger=logger;
// decorate with internal error classes
clierr.ArgumentTypeError=errs.type;
module.exports.error=clierr;
// internal libraries
module.exports.types=types;
module.exports.log=syslog;