Skip to content

Commit 9bc7050

Browse files
committed
Add trick in NodeJS port to support executing it from node command.
1 parent ee02b7b commit 9bc7050

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

source/ports/node_port/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,22 @@ test_environment_variables(${target}
106106
${TESTS_ENVIRONMENT_VARIABLES}
107107
${TESTS_ENVIRONMENT_VARIABLES_COB}
108108
)
109+
110+
# Detect exec with MetaCall CLI when this is being run with node
111+
set(NODEJS_EXECUTABLE_ONLY ON)
112+
113+
find_package(NodeJS 10.22.0)
114+
115+
if(NOT NODEJS_FOUND)
116+
message(STATUS "NodeJS libraries not found")
117+
return()
118+
endif()
119+
120+
add_test(NAME ${target}_node_binary
121+
COMMAND ${TEST_COMMAND} "${NODEJS_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/node_integration.js | ${GREP_COMMAND} \"NodeJS Integration Test Passed\""
122+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
123+
)
124+
test_environment_variables(${target}_node_binary
125+
""
126+
${TESTS_ENVIRONMENT_VARIABLES}
127+
)

source/ports/node_port/index.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,42 @@ const addon = (() => {
2727
try {
2828
/* This forces metacall port to be run always by metacall cli */
2929
return process.binding('node_loader_port_module');
30-
} catch (e) {
31-
console.error('MetaCall failed to load, probably you are importing this file from NodeJS directly.');
32-
console.error('You should use MetaCall CLI instead. Install it from: https://github.com/metacall/install');
33-
throw e;
30+
} catch (_) {
31+
const write = (data, cb) => {
32+
if (!process.stdout.write(data)) {
33+
process.stdout.once('drain', cb);
34+
} else {
35+
process.nextTick(cb);
36+
}
37+
};
38+
39+
/* Notify synchronously that we are launching MetaCall */
40+
write('NodeJS detected, launching MetaCall...\n', () => {
41+
try {
42+
const { spawnSync } = require('child_process');
43+
const args = [...process.argv];
44+
45+
args.shift();
46+
47+
const result = spawnSync('metacall', args, {});
48+
49+
if (result.error && result.error.code === 'ENOENT') {
50+
write('MetaCall not found. Please install MetaCall from: https://github.com/metacall/install and run it again.\n', () => {
51+
process.exit(1);
52+
});
53+
}
54+
55+
process.exit(result.status !== null ? result.status : 1);
56+
} catch (e) {
57+
const message = 'MetaCall failed to load, probably you are importing this file from NodeJS directly.\n'
58+
+ e.message + '\n'
59+
+ 'Install MetaCall from: https://github.com/metacall/install and run it again.\n';
60+
61+
write(message, () => {
62+
throw e;
63+
});
64+
}
65+
});
3466
}
3567
})();
3668

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* MetaCall NodeJS Port by Parra Studios
3+
* A complete infrastructure for supporting multiple language bindings in MetaCall.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
require('../index.js');
22+
23+
console.log('NodeJS Integration Test Passed');

0 commit comments

Comments
 (0)