forked from workshopper/workshopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkshopper.js
498 lines (372 loc) · 12.7 KB
/
workshopper.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
const argv = require('optimist').argv
, fs = require('fs')
, path = require('path')
, mkdirp = require('mkdirp')
, map = require('map-async')
, msee = require('msee')
, chalk = require('chalk')
const showMenu = require('./menu')
, print = require('./print-text')
, util = require('./util')
const defaultWidth = 65
function Workshopper (options) {
if (!(this instanceof Workshopper))
return new Workshopper(options)
var stat
, menuJson
, handled = false
, exercise
if (typeof options != 'object')
throw new TypeError('need to provide an options object')
if (typeof options.name != 'string')
throw new TypeError('need to provide a `name` String option')
if (typeof options.title != 'string')
throw new TypeError('need to provide a `title` String option')
if (typeof options.exerciseDir != 'string')
options.exerciseDir = path.join(options.appDir, 'exercises')
stat = fs.statSync(options.exerciseDir)
if (!stat || !stat.isDirectory())
throw new Error('"exerciseDir" [' + options.exerciseDir + '] does not exist or is not a directory')
if (typeof options.appDir != 'string')
throw new TypeError('need to provide an "appDir" String option')
stat = fs.statSync(options.appDir)
if (!stat || !stat.isDirectory())
throw new Error('"appDir" [' + options.appDir + '] does not exist or is not a directory')
menuJson = path.join(options.exerciseDir, 'menu.json')
stat = fs.statSync(menuJson)
if (!stat || !stat.isFile())
throw new Error('[' + menuJson + '] does not exist or is not a file')
this.appName = options.name
this.title = options.title
// optional
this.subtitle = options.subtitle
// optional
this.menuOptions = options.menu
// helpFile is additional to the usage in usage.txt
this.helpFile = options.helpFile
&& fs.existsSync(options.helpFile)
&& options.helpFile
// optional
this.footerFile = options.footerFile === false
? null
: fs.existsSync(options.footerFile)
? options.footerFile
: path.join(__dirname, './footer.md')
this.width = typeof options.width == 'number'
? options.width
: defaultWidth
this.exerciseDir = options.exerciseDir
this.appDir = options.appDir
// an `onComplete` hook function *must* call the callback given to it when it's finished, async or not
this.onComplete = typeof options.onComplete == 'function' && options.onComplete
this.exercises = require(menuJson).filter(function (e) {
return !/^\/\//.test(e)
})
this.dataDir = path.join(
process.env.HOME || process.env.USERPROFILE
, '.config'
, this.appName
)
mkdirp.sync(this.dataDir)
if (argv.v || argv.version || argv._[0] == 'version')
return console.log(
this.appName
+ '@'
+ require(path.join(this.appDir, 'package.json')).version
)
if (argv.h || argv.help || argv._[0] == 'help')
return this._printHelp()
this.current = this.getData('current')
if (options.menuItems && !options.commands)
options.commands = options.menuItems
if (Array.isArray(options.commands)) {
options.commands.forEach(function (item) {
if (argv._[0] == item.name
|| argv[item.name]
|| (item.short && argv[item.short])) {
handled = true
return item.handler(this)
}
}.bind(this))
if (handled)
return
this.commands = options.commands
}
if (argv._[0] == 'list') {
return this.exercises.forEach(function (name) {
console.log(name)
})
}
if (argv._[0] == 'current')
return console.log(this.current)
if (argv._[0] == 'select' || argv._[0] == 'print') {
return onselect.call(this, argv._.length > 1
? argv._.slice(1).join(' ')
: this.current
)
}
if (argv._[0] == 'verify' || argv._[0] == 'run') {
exercise = this.current && this.loadExercise(this.current)
if (!this.current)
return error('No active exercise. Select one from the menu.')
if (!exercise)
return error('No such exercise: ' + name)
if (exercise.requireSubmission !== false && argv._.length == 1)
return error('Usage:', this.appName, argv._[0], 'mysubmission.js')
return this.execute(exercise, argv._[0], argv._.slice(1))
}
if (argv._[0] == 'reset') {
this.reset()
return console.log(this.title + ' progress reset')
}
this.printMenu()
}
Workshopper.prototype.end = function (mode, pass, exercise, callback) {
exercise.end(mode, pass, function (err) {
if (err)
return error('Error cleaning up:' + (err.message || err))
setImmediate(callback || function () {
process.exit(pass ? 0 : -1)
})
})
}
// overall exercise fail
Workshopper.prototype.exerciseFail = function (mode, exercise) {
console.log('\n' + chalk.bold.red('# FAIL') + '\n')
console.log('Your solution to ' + exercise.name + ' didn\'t pass. Try again!\n')
this.end(mode, false, exercise)
}
// overall exercise pass
Workshopper.prototype.exercisePass = function (mode, exercise) {
console.log('\n' + chalk.bold.green('# PASS') + '\n')
console.log(chalk.bold('Your solution to ' + exercise.name + ' passed!') + '\n')
var done = function done () {
var completed = this.getData('completed') || []
, remaining
this.updateData('completed', function (xs) {
if (!xs)
xs = []
return xs.indexOf(exercise.name) >= 0 ? xs : xs.concat(exercise.name)
})
completed = this.getData('completed') || []
remaining = this.exercises.length - completed.length
if (remaining === 0) {
if (this.onComplete)
return this.onComplete(this.end.bind(this, mode, true, exercise))
else
console.log('You\'ve finished all the challenges! Hooray!\n')
} else {
console.log(
'You have '
+ remaining
+ ' challenge'
+ (remaining != 1 ? 's' : '')
+ ' left.'
)
console.log('Type `' + this.appName + '` to show the menu.\n')
}
this.end(mode, true, exercise)
}.bind(this)
if (exercise.hideSolutions)
return done()
exercise.getSolutionFiles(function (err, files) {
if (err)
return error('ERROR: There was a problem printing the solution files: ' + (err.message || err))
if (!files.length)
return done()
console.log('Here\'s the official solution in case you want to compare notes:\n')
function processSolutionFile (file, callback) {
fs.readFile(file, 'utf8', function (err, content) {
if (err)
return callback(err)
var filename = path.basename(file)
// code fencing is necessary for msee to render the solution as code
content = msee.parse('```js\n' + content + '\n```')
callback(null, { name: filename, content: content })
})
}
function printSolutions (err, solutions) {
if (err)
return error('ERROR: There was a problem printing the solution files: ' + (err.message || err))
solutions.forEach(function (file, i) {
console.log(chalk.yellow(util.repeat('\u2500', 80)))
if (solutions.length > 1)
console.log(chalk.bold.yellow(file.name + ':') + '\n')
console.log(file.content.replace(/^\n/m, '').replace(/\n$/m, ''))
if (i == solutions.length - 1)
console.log(chalk.yellow(util.repeat('\u2500', 80)) + '\n')
}.bind(this))
done()
}
map(files, processSolutionFile, printSolutions.bind(this))
}.bind(this))
}
// single 'pass' event for a validation
function onpass (msg) {
console.log(chalk.green.bold('\u2713 ') + msg)
}
// single 'fail' event for validation
function onfail (msg) {
console.log(chalk.red.bold('\u2717 ') + msg)
}
Workshopper.prototype.execute = function (exercise, mode, args) {
// individual validation events
exercise.on('pass', onpass)
exercise.on('fail', onfail)
function done (err, pass) {
var errback
if (err) {
// if there was an error then we need to do this after cleanup
errback = function () {
error('Could not ' + mode + ': ' + (err.message || err))
}
}
if (mode == 'run' || err)
return this.end(mode, true, exercise, errback) // clean up
if (!pass)
return this.exerciseFail(mode, exercise)
this.exercisePass(mode, exercise)
}
exercise[mode](args, done.bind(this))
}
Workshopper.prototype.printMenu = function () {
var menu = showMenu({
name : this.appName
, title : this.title
, subtitle : this.subtitle
, width : this.width
, completed : this.getData('completed') || []
, exercises : this.exercises
, extras : this.commands && this.commands
.filter(function (item) {
return item.menu !== false
})
.map(function (item) {
return item.name.toLowerCase()
})
, menu : this.menuOptions
})
menu.on('select', onselect.bind(this))
menu.on('exit', function () {
console.log()
process.exit(0)
})
menu.on('help', function () {
console.log()
this._printHelp()
}.bind(this))
if (this.commands) {
this.commands.forEach(function (item) {
menu.on('extra-' + item.name, function () {
item.handler(this)
}.bind(this))
}.bind(this))
}
}
Workshopper.prototype.getData = function (name) {
var file = path.resolve(this.dataDir, name + '.json')
try {
return JSON.parse(fs.readFileSync(file, 'utf8'))
} catch (e) {}
return null
}
Workshopper.prototype.updateData = function (id, fn) {
var json = {}
, file
try {
json = this.getData(id)
} catch (e) {}
file = path.resolve(this.dataDir, id + '.json')
fs.writeFileSync(file, JSON.stringify(fn(json)))
}
Workshopper.prototype.reset = function () {
fs.unlink(path.resolve(this.dataDir, 'completed.json'))
fs.unlink(path.resolve(this.dataDir, 'current.json'))
}
Workshopper.prototype.dirFromName = function (name) {
return util.dirFromName(this.exerciseDir, name)
}
Workshopper.prototype._printHelp = function () {
this._printUsage()
if (this.helpFile)
print.file(this.appName, this.appDir, this.helpFile)
}
Workshopper.prototype._printUsage = function () {
print.file(this.appName, this.appDir, path.join(__dirname, './usage.txt'))
}
Workshopper.prototype.getExerciseMeta = function (name) {
name = name.toLowerCase().trim()
var number
, dir
this.exercises.some(function (_name, i) {
if (_name.toLowerCase().trim() != name)
return false
number = i + 1
name = _name
return true
})
if (number === undefined)
return null
dir = this.dirFromName(name)
return {
name : name
, number : number
, dir : dir
, id : util.idFromName(name)
, exerciseFile : path.join(dir, './exercise.js')
}
}
Workshopper.prototype.loadExercise = function (name) {
var meta = this.getExerciseMeta(name)
, stat
, exercise
if (!meta)
return null
try {
stat = fs.statSync(meta.exerciseFile)
} catch (err) {
return error('ERROR:', meta.exerciseFile, 'does not exist!')
}
if (!stat || !stat.isFile())
return error('ERROR:', meta.exerciseFile, 'does not exist!')
exercise = require(meta.exerciseFile)
if (!exercise || typeof exercise.init != 'function')
return error('ERROR:', meta.exerciseFile, 'is not a workshopper exercise')
exercise.init(this, meta.id, meta.name, meta.dir, meta.number)
return exercise
}
function error () {
var pr = chalk.bold.red
console.log(pr.apply(pr, arguments))
process.exit(-1)
}
function onselect (name) {
var exercise = this.loadExercise(name)
if (!exercise)
return error('No such exercise: ' + name)
console.log(
'\n ' + chalk.green.bold(this.title)
+ '\n' + chalk.green.bold(util.repeat('\u2500', chalk.stripColor(this.title).length + 2))
+ '\n ' + chalk.yellow.bold(exercise.name)
+ '\n ' + chalk.yellow.italic('Exercise', exercise.number, 'of', this.exercises.length)
+ '\n'
)
this.current = exercise.name
this.updateData('current', function () {
return exercise.name
})
exercise.prepare(function (err) {
if (err)
return error('Error preparing exercise:', err.message || err)
exercise.getExerciseText(function (err, type, exerciseText) {
if (err)
return error('Error loading exercise text:', err.message || err)
print.text(this.appName, this.appDir, type, exerciseText)
if (this.footerFile)
print.file(this.appName, this.appDir, this.footerFile)
}.bind(this))
}.bind(this))
}
Workshopper.prototype.error = error
Workshopper.prototype.print = print
module.exports = Workshopper