Skip to content

Commit

Permalink
Add trick in NodeJS port to support executing it from node command.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Jan 20, 2021
1 parent ee02b7b commit 9bc7050
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
19 changes: 19 additions & 0 deletions source/ports/node_port/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
40 changes: 36 additions & 4 deletions source/ports/node_port/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
});
}
})();

Expand Down
23 changes: 23 additions & 0 deletions source/ports/node_port/test/node_integration.js
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* 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');

0 comments on commit 9bc7050

Please sign in to comment.