Skip to content

Instantly share code, notes, and snippets.

@paulbatum
Created November 25, 2012 18:36
Show Gist options
  • Save paulbatum/4144724 to your computer and use it in GitHub Desktop.
Save paulbatum/4144724 to your computer and use it in GitHub Desktop.
Magic buckets
var vm = require('vm');
var fs = require('fs');
var logger = {
log: function() {
console.log('<foo>');
console.log.apply(null, arguments);
console.log('</foo>');
}
}
var context = vm.createContext({ console: logger, require: require});
var script = fs.readFileSync('script.js', 'utf8');
vm.runInContext(script, context);
module.exports.writeToLog = function() {
console.log("this is from a module");
}
<foo>
this is from the script
</foo>
this is from a module
var module = require('./module.js');
console.log("this is from the script");
module.writeToLog();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment