/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }
node.exec(file);
node.exec(file);
@Test public void testExecNodeScript() throws IOException { assumeFalse(skipMessage, skipTest()); // conditional skip nodeJS.release(); File testScript = createTemporaryScriptFile("global.passed = true;", "testScript"); nodeJS = NodeJS.createNodeJS(); nodeJS.exec(testScript); runMessageLoop(); assertEquals(true, nodeJS.getRuntime().getBoolean("passed")); testScript.delete(); }
/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }