-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.coffee
60 lines (54 loc) · 1.92 KB
/
index.coffee
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
module.exports = (vorpal, options) ->
defaultPrint = (arg) ->
if vorpal.logger?
vorpal.logger.info arg
else
if vorpal.activeCommand?
vorpal.activeCommand.log arg
else
vorpal.log arg
defaultDescribe = (key) ->
return "set or print #{key}"
defaultFailedValidation = (key, arg) ->
if vorpal.logger?
vorpal.logger.error "#{arg} is an invalid value for #{key}"
else
vorpal.session.log "#{arg} is an invalid value for #{key}"
defaultPassedValidation = (key, arg, value) ->
if vorpal.logger?
vorpal.logger.confirm "set #{key} to #{arg}"
else
vorpal.session.log "set #{key} to #{arg}"
sop =
options: options ? {}
command: (key, obj, options = {}) ->
return vorpal.command "#{key} [#{key}]"
.description sop.options.describe key
.action (args, cb) ->
if args[key]?
if options.validate?
value = options.validate args[key]
else
value = args[key]
if value is null
if options.failedValidation?
options.failedValidation key, args[key]
else
sop.options.failedValidation key, args[key]
else
obj[key] = value
if options.passedValidation?
options.passedValidation key, args[key], value
else
sop.options.passedValidation key, args[key], value
else
if options.print?
options.print obj[key]
else
sop.options.print obj[key]
cb()
sop.options.print = defaultPrint unless sop.options.print?
sop.options.describe = defaultDescribe unless sop.options.describe?
sop.options.failedValidation = defaultFailedValidation unless sop.options.failedValidation?
sop.options.passedValidation = defaultPassedValidation unless sop.options.passedValidation?
vorpal.sop = sop