aboutsummaryrefslogtreecommitdiffstats
path: root/tools/execute_js_code.js
blob: 6bad67a11ed5405c8b19e6317b429dcfc92b34b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
const vm = require('vm');

/**
 * Execute the javascript code in node.js.
 * @typedef {Object} Args
 * @property {string} code - Javascript code to execute, such as `console.log("hello world")`
 * @param {Args} args
 */
exports.run = function run({ code }) {
  const context = vm.createContext({});
  const script = new vm.Script(code);
  return script.runInContext(context);
}