2019-04-15 13:12:18 +03:00
|
|
|
const Module = require('module');
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
function AppSandbox(opts) {
|
2019-04-15 13:12:18 +03:00
|
|
|
this.opts = opts;
|
2014-02-01 02:53:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AppSandbox.prototype.loadApp = function loadAppSandboxed(appPath) {
|
|
|
|
// Set loaded modules parent to this
|
2019-04-15 13:12:18 +03:00
|
|
|
const parentModulePath = this.opts.parent || module.parent;
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
// Resolve the modules path
|
2019-04-15 13:12:18 +03:00
|
|
|
const resolvedModulePath = Module._resolveFilename(appPath, parentModulePath);
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
// Instantiate a Node Module class
|
2019-04-15 13:12:18 +03:00
|
|
|
const currentModule = new Module(resolvedModulePath, parentModulePath);
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
currentModule.load(currentModule.id);
|
|
|
|
|
|
|
|
return currentModule.exports;
|
|
|
|
};
|
|
|
|
|
2014-02-27 06:44:09 +04:00
|
|
|
module.exports = AppSandbox;
|