-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
311 lines (279 loc) · 8.71 KB
/
index.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
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
var inherits = require('inherits');
var EventEmitter = require('events').EventEmitter;
var mkdirp = require('mkdirp');
var fs = require('fs');
var path = require('path');
var x256 = require('x256');
var through = require('through2');
var split = require('split');
var minimist = require('minimist');
var showMenu = require('./lib/menu.js');
var showHelp = require('./lib/help.js');
module.exports = Shop;
inherits(Shop, EventEmitter);
function Shop (opts) {
if (!(this instanceof Shop)) return new Shop(opts);
if (!opts) opts = {};
if (typeof opts === 'string') opts = { name: opts };
this.name = opts.name;
this.options = opts;
if (!this.name) return this._error(
'Your adventure must have a name! '
+ 'Supply an `opts.name` to adventure().'
);
this.command = opts.command || commandify(this.name);
this.datadir = opts.datadir || path.resolve(
process.env.HOME || process.env.USERPROFILE,
'.config/' + this.name
);
mkdirp.sync(this.datadir);
this.files = {
completed: path.join(this.datadir, 'completed.json'),
current: path.join(this.datadir, 'current.json')
};
this.state = {
completed: [],
current: null
};
try { this.state.completed = require(this.files.completed) }
catch (err) {}
try { this.state.current = require(this.files.current) }
catch (err) {}
this.colors = opts.colors || {};
var c = {
pass: [0,255,0],
fail: [255,0,0],
info: [0,255,255]
};
var colors = Object.keys(c).reduce(function (acc, key) {
acc[key] = '\x1b[38;5;' + x256(c[key]) + 'm';
return acc;
}, {});
if (!this.colors.pass) this.colors.pass = colors.pass;
if (!this.colors.fail) this.colors.fail = colors.fail;
if (!this.colors.info) this.colors.info = colors.info;
this.colors.reset = '\x1b[00m';
this._adventures = [];
}
Shop.prototype.execute = function (args) {
var cmd = args[0];
var argv = minimist(args, { alias: { h: 'help' } });
if (cmd === 'verify') {
this.verify(args.slice(1), this.state.current);
}
else if (cmd === 'run') {
this.run(args.slice(1), this.state.current);
}
else if (cmd === 'help' || argv.help) {
showHelp({ command: this.command });
}
else if (cmd === 'selected') {
console.log(this.state.current);
}
else if (cmd === 'list') {
console.log(this._adventures
.map(function (adv) { return adv.name })
.join('\n')
);
}
else if (cmd === 'completed') {
console.log(this.state.completed.join('\n'));
}
else if (cmd === 'select') {
this.select(args[1]);
}
else if (cmd === 'print') {
this.select(this.state.current);
}
else if (cmd === 'solution') {
var adv = this.find(this.state.current);
if (!adv) {
return console.log(
'No adventure is currently selected. '
+ 'Select an adventure from the menu.'
);
process.exit(1);
}
var p = adv.fn();
if (p.solution) this._show(p.solution);
else console.log('No reference solution available for this adventure.')
}
else if (cmd === 'reset') {
this.state.completed = [];
this.save('completed');
this.state.current = null;
this.save('current');
}
else if (!cmd || cmd === 'menu') {
this.showMenu(this.options);
}
else {
console.log('unrecognized command: ' + cmd);
}
};
Shop.prototype.add = function (name, fn) {
this._adventures.push({ name: name, fn: fn });
};
Shop.prototype.find = function (name) {
for (var i = 0; i < this._adventures.length; i++) {
var adv = this._adventures[i];
if (norm(adv.name) === norm(name)) return adv;
}
function norm (s) { return String(s).replace(/\W/g, '').toLowerCase() }
};
Shop.prototype.verify = function (args, name) {
var self = this;
var adv = this.find(name);
if (!adv) return this._error(
'No adventure is currently selected. '
+ 'Select an adventure from the menu.'
);
var p = adv.fn();
if (!p.verify) return this._error(
"This problem doesn't have a .verify function yet!"
);
if (typeof p.verify !== 'function') return this._error(
'This p.verify is a ' + typeof p.verify
+ '. It should be a function instead.'
);
var s = p.verify(args, function (ok) {
if (ok) self.pass(name, p)
else self.fail(name, p)
});
if (s) this._show(s);
};
Shop.prototype.run = function (args, name) {
var self = this;
var adv = this.find(name);
if (!adv) return this._error(
'No adventure is currently selected. '
+ 'Select an adventure from the menu.'
);
var p = adv.fn();
if (!p.run) return this._error(
"This problem doesn't have a .run function."
);
if (typeof p.run !== 'function') return this._error(
'This p.run is a ' + typeof p.run
+ '. It should be a function instead.'
);
var s = p.run(args);
if (s) this._show(s);
};
Shop.prototype.pass = function (name, p) {
var ix = this.state.completed.indexOf(name);
if (ix < 0) this.state.completed.push(name);
this.save('completed');
if (p.pass) {
this._show(p.pass);
console.log();
}
else {
console.log(
'\n' + this.colors.pass
+ '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
);
console.log(
'@@@' + this.colors.reset
+ ' YOUR SOLUTION IS CORRECT'
+ this.colors.pass + '! @@@'
);
console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@');
console.log(this.colors.reset + '\n');
}
if (p.solution) this._show(p.solution);
this.emit('pass', name);
if (this.state.completed.length === this._adventures.length) {
this.emit('finished');
}
};
Shop.prototype.fail = function (name, p) {
if (p.fail) {
this._show(p.fail);
console.log();
}
else {
console.log(
this.colors.fail
+ '#########################################'
);
console.log(
'###' + this.colors.reset
+ ' YOUR SOLUTION IS NOT CORRECT!'
+ this.colors.fail + ' ###'
);
console.log('#########################################');
console.log(this.colors.reset + '\n');
}
this.emit('fail', name);
};
Shop.prototype.select = function (name) {
var adv = this.find(name);
this.state.current = name;
this.save('current');
var p = adv.fn();
if (!p.problem) {
p.problem = this.colors.info + Array(67).join('!') + '\n'
+ '!!!' + this.colors.reset
+ ' This adventure does not have a .problem description yet! '
+ this.colors.info + ' !!!\n!!!' + this.colors.reset
+ ' Set .problem to a string, buffer, stream or function that'
+ this.colors.info + ' !!!\n!!!' + this.colors.reset
+ ' returns a string, buffer, or stream. '
+ this.colors.info + ' !!!\n' + Array(67).join('!') + '\n'
;
}
if (p.problem) this._show(p.problem);
};
Shop.prototype.showMenu = function (opts) {
var self = this;
if (!opts) opts = {};
var menu = showMenu({
fg: opts.fg,
bg: opts.bg,
command: this.command,
title: opts.title || this.name.toUpperCase(),
names: this._adventures.map(function (x) { return x.name }),
completed: this.state.completed
});
menu.on('select', function (name) {
console.log();
self.select(name);
});
menu.on('exit', function () {
menu.close();
console.log();
});
return menu;
};
Shop.prototype.save = function (key) {
fs.writeFile(this.files[key], JSON.stringify(this.state[key]));
};
Shop.prototype._error = function (msg) {
console.error('ERROR: ' + msg);
process.exit(1);
};
Shop.prototype._show = function (m) {
var self = this;
if (typeof m === 'object' && m.pipe) {
m.pipe(split()).pipe(through(write)).pipe(process.stdout);
}
else if (typeof m === 'function') {
this._show(m());
}
else console.log(replace(m));
function write (buf, enc, next) {
this.push(replace(buf) + '\n');
next();
}
function replace (s) {
if (typeof s !== 'string') s = String(s);
return s
.replace(/\$ADVENTURE_COMMAND/g, self.command)
.replace(/\$ADVENTURE_NAME/g, self.name)
;
}
}
function commandify (s) {
return String(s).toLowerCase().replace(/\s+/g, '-');
}