forked from workshopper/workshopper
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworkshopper.js
More file actions
437 lines (324 loc) · 11.3 KB
/
Copy pathworkshopper.js
File metadata and controls
437 lines (324 loc) · 11.3 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
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
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
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(this.helpFile)
&& options.helpFile
// optional
this.footerFile = 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
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()
if (Array.isArray(options.menuItems)) {
options.menuItems.forEach(function (item) {
if (argv._[0] == item.name || argv[item.name]) {
handled = true
return item.handler(this)
}
})
if (handled)
return
this.menuItems = options.menuItems
}
if (argv._[0] == 'list') {
return this.exercises.forEach(function (name) {
console.log(name)
})
}
if (argv._[0] == 'current')
return console.log(this.getData('current'))
if (argv._[0] == 'select' || argv._[0] == 'print') {
return onselect.call(this, argv._.length > 1
? argv._.slice(1).join(' ')
: this.getData('current')
)
}
if (argv._[0] == 'verify' || argv._[0] == 'run') {
if (argv._.length == 1)
return error('Usage:', this.appName, argv._[0], 'mysubmission.js')
return this.execute(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) {
exercise.end(mode, pass, function (err) {
if (err)
return error('Error cleaning up:' + (err.message || err))
setImmediate(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')
if (exercise.hideSolutions)
return
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
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))
var completed = this.getData('completed') || []
, remaining
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, ''))
if (i == solutions.length - 1)
console.log(chalk.yellow(util.repeat('\u2500', 80)) + '\n')
}.bind(this))
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) {
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)
}
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 (mode, args) {
var current = this.getData('current')
, exercise = current && this.loadExercise(current)
if (!current)
return error('No active exercise. Select one from the menu.')
if (!exercise)
return error('No such exercise: ' + name)
// individual validation events
exercise.on('pass', onpass)
exercise.on('fail', onfail)
function done (err, pass) {
if (err)
return error('Could not ' + mode + ': ' + (err.message || err))
if (mode == 'run')
return this.end(mode, true, exercise) // 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.menuItems && this.menuItems.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.menuItems) {
this.menuItems.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.unlinkSync(path.resolve(this.dataDir, 'completed.json'))
fs.unlinkSync(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.loadExercise = function (name) {
name = name.toLowerCase().trim()
var number
, dir
, id
, exerciseFile
, stat
, exercise
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)
id = util.idFromName(name)
exerciseFile = path.join(dir, './exercise.js')
stat = fs.statSync(exerciseFile)
if (!stat || !stat.isFile())
return error('ERROR:', exerciseFile, 'does not exist!')
exercise = require(exerciseFile)
if (!exercise || typeof exercise.init != 'function')
return error('ERROR:', exerciseFile, 'is not a workshopper exercise')
exercise.init(this, id, name, dir, number)
return exercise
}
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.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)
print.file(this.appName, this.appDir, this.footerFile)
}.bind(this))
}.bind(this))
}
function error () {
var pr = chalk.bold.red
console.log(pr.apply(pr, arguments))
process.exit(-1)
}
module.exports = Workshopper