forked from jlord/workshopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify-calls.js
32 lines (28 loc) · 1.05 KB
/
verify-calls.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
const bold = require('./term-util').bold
, red = require('./term-util').red
function verifyOnlyAsyncOrSync (sync, trackFile, callback) {
var track = require(trackFile)
, fscalls = track.calls.filter(function (call) {
return call.module == 'fs'
&& call.stack
&& call.stack[0].file != 'module.js'
&& call.stack[0].file != 'fs.js'
})
, badCalls = fscalls.filter(function (call) {
return sync != (/Sync$/).test(call.fn)
})
if (!badCalls.length)
return callback() // yay!
console.log(
'\nYou got the correct answer but used the following '
+ bold((sync ? 'a' : '') + 'synchronous')
+ ' calls:'
)
badCalls.forEach(function (call) {
console.log('\t' + bold(red('fs.' + call.fn + '()')))
})
console.log('\nThis problem requires you to only use ' + bold((sync ? '' : 'a') + 'synchronous') + ' calls.\n')
callback('bzzt!')
}
module.exports.verifyOnlyAsync = verifyOnlyAsyncOrSync.bind(null, false)
module.exports.verifyOnlySync = verifyOnlyAsyncOrSync.bind(null, true)