-
Notifications
You must be signed in to change notification settings - Fork 7
/
gasrequire.js
65 lines (54 loc) · 1.85 KB
/
gasrequire.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
var fs = require('fs');
var vm = require('vm');
var path = require('path');
var util = require('util');
var debug = util.debuglog('gas-local:require');
/**
* Loads all js files from folder as single module
*
* @param {string} folderPath to folder with downloaded app scripts
* @param {object} globalObject to pass to module. globalMock will be passed if not specified
* @param {object} options that give more control over behavour. options.filter can be to determine which files are loaded into the context
* @returns module
*/
function gasrequire(folderPath, globalObject, options) {
if (!globalObject) {
debug('no globalObject passed. use default mock');
globalObject = require('./globalmock-default');
}
options = typeof options === 'object' ? options : {};
var filterFunc = options.filter ? options.filter : function(f) {
var ext = path.extname(f);
return ext == '.js';
};
debug('loading from folder: %s...', folderPath)
var files = walk(folderPath);
var gsFiles = files.filter(filterFunc);
var ctx = vm.createContext(globalObject);
for (var i = 0; i < gsFiles.length; i++) {
var fpath = gsFiles[i];
debug('loading file: %s...', fpath);
var code = fs.readFileSync(fpath);
try {
//without try/catch envelop node runtime hangs if any error in loaded script (e.g. syntax or RefeenceError)
vm.runInContext(code, ctx, fpath);
}
catch (error) {
//rethrow the error and stop loading other files.
throw error;
}
}
return ctx;
}
function walk(dir) {
var results = []
var list = fs.readdirSync(dir)
list.forEach(function(file) {
file = dir + '/' + file
var stat = fs.statSync(file)
if (stat && stat.isDirectory()) results = results.concat(walk(file))
else results.push(file)
})
return results
}
module.exports = gasrequire;