diff --git a/source/ports/node_port/CMakeLists.txt b/source/ports/node_port/CMakeLists.txt index 61f02ba13..dfd349e89 100644 --- a/source/ports/node_port/CMakeLists.txt +++ b/source/ports/node_port/CMakeLists.txt @@ -106,3 +106,22 @@ test_environment_variables(${target} ${TESTS_ENVIRONMENT_VARIABLES} ${TESTS_ENVIRONMENT_VARIABLES_COB} ) + +# Detect exec with MetaCall CLI when this is being run with node +set(NODEJS_EXECUTABLE_ONLY ON) + +find_package(NodeJS 10.22.0) + +if(NOT NODEJS_FOUND) + message(STATUS "NodeJS libraries not found") + return() +endif() + +add_test(NAME ${target}_node_binary +COMMAND ${TEST_COMMAND} "${NODEJS_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/node_integration.js | ${GREP_COMMAND} \"NodeJS Integration Test Passed\"" + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} +) +test_environment_variables(${target}_node_binary + "" + ${TESTS_ENVIRONMENT_VARIABLES} +) diff --git a/source/ports/node_port/index.js b/source/ports/node_port/index.js index 2fc714e99..d72b62a98 100644 --- a/source/ports/node_port/index.js +++ b/source/ports/node_port/index.js @@ -27,10 +27,42 @@ const addon = (() => { try { /* This forces metacall port to be run always by metacall cli */ return process.binding('node_loader_port_module'); - } catch (e) { - console.error('MetaCall failed to load, probably you are importing this file from NodeJS directly.'); - console.error('You should use MetaCall CLI instead. Install it from: https://github.com/metacall/install'); - throw e; + } catch (_) { + const write = (data, cb) => { + if (!process.stdout.write(data)) { + process.stdout.once('drain', cb); + } else { + process.nextTick(cb); + } + }; + + /* Notify synchronously that we are launching MetaCall */ + write('NodeJS detected, launching MetaCall...\n', () => { + try { + const { spawnSync } = require('child_process'); + const args = [...process.argv]; + + args.shift(); + + const result = spawnSync('metacall', args, {}); + + if (result.error && result.error.code === 'ENOENT') { + write('MetaCall not found. Please install MetaCall from: https://github.com/metacall/install and run it again.\n', () => { + process.exit(1); + }); + } + + process.exit(result.status !== null ? result.status : 1); + } catch (e) { + const message = 'MetaCall failed to load, probably you are importing this file from NodeJS directly.\n' + + e.message + '\n' + + 'Install MetaCall from: https://github.com/metacall/install and run it again.\n'; + + write(message, () => { + throw e; + }); + } + }); } })(); diff --git a/source/ports/node_port/test/node_integration.js b/source/ports/node_port/test/node_integration.js new file mode 100644 index 000000000..a53c62894 --- /dev/null +++ b/source/ports/node_port/test/node_integration.js @@ -0,0 +1,23 @@ +/* + * MetaCall NodeJS Port by Parra Studios + * A complete infrastructure for supporting multiple language bindings in MetaCall. + * + * Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +require('../index.js'); + +console.log('NodeJS Integration Test Passed');