-
Notifications
You must be signed in to change notification settings - Fork 41
/
adventure.js
110 lines (97 loc) · 3.65 KB
/
adventure.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
const inherits = require('util').inherits
/* jshint -W079 */
const Core = require('./index')
const error = require('./lib/print').error
/* jshint +W079 */
function legacyCommands (item) {
if (!item.aliases) {
item.aliases = []
}
if (item && item.name) {
item.aliases.unshift(item.name)
}
return item
}
function LegacyAdventure (options) {
if (!(this instanceof LegacyAdventure)) {
return new LegacyAdventure(options)
}
if (typeof options === 'string') {
options = { name: options }
}
if (!options) {
options = {}
}
if (typeof options !== 'object') {
return error('You need to provide an options object')
}
if (!options.commands) {
options.commands = options.menuItems
}
if (options.commands) {
options.commands = options.commands.map(legacyCommands)
}
if (options.modifiers) {
options.modifiers = options.modifiers.map(legacyCommands)
}
if (options.helpFile) {
options.help = { file: options.helpFile }
}
if (!options.footer) {
if (options.footerFile) {
options.footer = { file: options.footerFile }
}
}
if (!options.defaultOutputType) {
options.defaultOutputType = 'txt'
}
if (options.hideSolutions === undefined) {
options.hideSolutions = true
}
if (options.hideRemaining === undefined) {
options.hideRemaining = true
}
// an `onComplete` hook function *must* call the callback given to it when it's finished, async or not
if (typeof options.onComplete === 'function') {
this.onComplete = options.onComplete
}
Core.call(this, options)
if (options.execute === 'now') {
this.execute(process.argv.slice(2))
} else if (options.execute === 'immediatly') {
setImmediate(this.execute.bind(this, process.argv.slice(2)))
}
// backwards compatibility support
this.__defineGetter__('title', this.__.bind(this, 'title'))
this.__defineGetter__('subtitle', this.__.bind(this, 'subtitle'))
this.__defineGetter__('name', this.__.bind(this, 'name'))
this.__defineGetter__('appName', this.__.bind(this, 'name'))
this.__defineGetter__('appname', this.__.bind(this, 'name'))
this.__defineGetter__('lang', this.i18n.lang.bind(this.i18n, 'lang'))
this.__defineGetter__('width', function () { return this.menuFactory.options.width }.bind(this))
this.__defineGetter__('helpFile', function () { return this.options.helpFile }.bind(this))
this.__defineGetter__('footer', function () { return this.options.footer }.bind(this))
this.__defineGetter__('defaultLang', function () { return this.options.defaultLang }.bind(this))
this.__defineGetter__('languages', function () { return this.options.languages }.bind(this))
this.__defineGetter__('globalDataDir', function () { return this.globalStorage.dir }.bind(this))
this.__defineGetter__('dataDir', function () { return this.appStorage.dir }.bind(this))
this.__defineGetter__('datadir', function () { return this.appStorage.dir }.bind(this)) // adventure
this.__defineGetter__('appDir', function () { return this.options.appDir }.bind(this))
this.__defineGetter__('exerciseDir', function () { return this.options.exerciseDir }.bind(this))
this.__defineGetter__('current', function () { return this.appStorage.get('current') }.bind(this))
this.__defineGetter__('_adventures', function () { return this.exercises }.bind(this))
this.__defineGetter__('state', function () {
return {
completed: this.appStorage.get('completed'),
current: this.appStorage.get('current')
}
})
}
inherits(LegacyAdventure, Core)
LegacyAdventure.prototype.processResult = function (result, stream) {
if (result) {
stream.append(['```', result, '```'])
}
return stream
}
module.exports = LegacyAdventure