-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwrappedexec.js
86 lines (69 loc) · 2.75 KB
/
wrappedexec.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
const execWrapPath = require.resolve('./exec-wrap')
, os = require('os')
, path = require('path')
, fs = require('fs')
, assert = require('assert')
, xtend = require('xtend')
, after = require('after')
os.tmpDir = os.tmpdir;
function fix (exercise) {
var dataPath = path.join(os.tmpDir(), '~workshopper.wraptmp.' + process.pid)
var submissionPath = dataPath + '-submission'
var solutionPath = dataPath + '-solution'
exercise.wrapData = {}
exercise.wrapMods = []
exercise.addVerifySetup(function setup (callback) {
this.solutionArgs.unshift('$execwrap$solution') // flag if solution
this.solutionArgs.unshift('$execwrap$' + this.solution) // original main cmd
this.solutionArgs.unshift('$execwrap$' + solutionPath) // path to context data
this.solutionArgs.unshift('$execwrap$' + JSON.stringify(exercise.wrapMods)) // list of mods to load
this.solution = execWrapPath
this.submissionArgs.unshift('$execwrap$submission') // flag if submission
this.submissionArgs.unshift('$execwrap$' + this.submission) // original main cmd
this.submissionArgs.unshift('$execwrap$' + submissionPath) // path to context data
this.submissionArgs.unshift('$execwrap$' + JSON.stringify(exercise.wrapMods)) // list of mods to load
this.submission = execWrapPath
var wrapData = JSON.stringify(exercise.wrapData)
var files = [ submissionPath , solutionPath ]
var done = after(files.length, function(err) {
if (err)
return callback(new Error('Error writing execwrap data: ' + (err.message || err), err))
callback()
})
files.forEach(function (path) {
fs.writeFile(path, wrapData, 'utf8', done)
})
})
exercise.addVerifyProcessor(function (callback) {
// sync... yes.. unfortunately, otherwise we'll have timing problems
fs.readFile(submissionPath, 'utf8', function (err, data) {
if (err)
return callback(new Error('Error reading execwrap data: ' + (err.message || err), err))
exercise.wrapData = JSON.parse(data)
fs.unlink(submissionPath, function () {
callback(null, true) // pass, nothing to fail here
})
})
})
// convenience
exercise.wrapModule = function (mod) {
exercise.wrapMods.push(mod)
}
// convenience
exercise.wrapSet = function (k, v) {
if (typeof k == 'string')
exercise.wrapData[k] = v
else if (typeof k == 'object')
exercise.wrapData = xtend(exercise.wrapData, k)
}
// convenience
exercise.wrapGet = function (k) {
return exercise.wrapData[k]
}
}
function wrappedexec (exercise) {
if (typeof exercise.wrapInject != 'function' || typeof exercise.wrapSet != 'function')
fix(exercise)
return exercise
}
module.exports = wrappedexec