-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexec-wrap.js
More file actions
90 lines (70 loc) · 2.34 KB
/
exec-wrap.js
File metadata and controls
90 lines (70 loc) · 2.34 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
const path = require('path')
, fs = require('fs')
, myargre = /^\$execwrap\$(.*)$/
, myargs = process.argv.map(function (a) {
return myargre.test(a) && a.replace(myargre, '$1')
}).filter(Boolean)
, modFiles = JSON.parse(myargs[0])
, mods = []
, ctxFile = myargs[1]
, ctx = JSON.parse(fs.readFileSync(ctxFile, 'utf8'))
, mainProgram = ctx.mainProgram = fs.realpathSync(path.resolve(process.cwd(), myargs[2]))
, prexit = process.exit
, isSolution = myargs[3] === 'solution'
, isSubmission = myargs[3] === 'submission'
// remove evidence of main, modFiles, ctx
process.argv = process.argv.filter(function (a) {
return !myargre.test(a)
})
// replace original main at position 2
process.argv.splice(1, 1, mainProgram)
// utility to catpture a stack trace at a particular method in an array
ctx.$captureStack = function captureStack (fn) {
var err = new Error
, stack
Error._prepareStackTrace = Error.prepareStackTrace
Error.prepareStackTrace = function (err, stack) { return stack }
Error.captureStackTrace(err, fn)
stack = err.stack // touch it to capture it
Error.prepareStackTrace = Error._prepareStackTrace
return stack
}
for (var i = 0; i < modFiles.length; i++) {
try {
// load module
var mod = require(modFiles[i])
// include in submission? defaults to true
if (isSubmission && mod.wrapSubmission === false) continue
// include in solution? defaults to false
if (isSolution && !mod.wrapSolution) continue
mods.unshift(mod)
// give it the ctx if it exports a function
if (typeof mod == 'function')
mod(ctx)
} catch (e) {
console.error('Internal error loading wrapped module', modFiles[i])
}
}
var wrote = false
// write back the context data so it can be read by the parent
function finish () {
if (wrote)
return
for (var i = 0; i < mods.length; i++) {
if (mods[i] && typeof mods[i].finish == 'function')
mods[i].finish(ctx)
}
fs.writeFileSync(ctxFile, JSON.stringify(ctx), 'utf8')
wrote = true
}
// just in case they use it
process.exit = function () {
finish()
prexit.apply(process, arguments)
}
try {
// run original main as if it were loaded directly
require(mainProgram)
} finally {
finish()
}