forked from eclipse-thingweb/node-wot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.ts
More file actions
53 lines (48 loc) · 2.11 KB
/
Copy pathexecutor.ts
File metadata and controls
53 lines (48 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
import { createLoggers, Helpers } from "@node-wot/core";
const { debug } = createLoggers("cli", "executor");
export interface WoTContext {
runtime: typeof WoT;
helpers: Helpers;
}
export class Executor {
public async exec(file: string, wotContext: WoTContext): Promise<unknown> {
debug(`Executing WoT script from file: ${file}`);
const userScriptPathArg = file;
const isTypeScriptScript =
userScriptPathArg && (userScriptPathArg.endsWith(".ts") || userScriptPathArg.endsWith(".tsx"));
global.WoT = wotContext.runtime;
if (isTypeScriptScript === true) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
require("ts-node/register");
}
try {
// Execute the user's script
// Node.js will now handle .ts files automatically if ts-node is registered
// TODO: For ESM modules a more complex check might be needed.
if (file.endsWith(".mjs")) {
return await import(`file:///${file}`);
} else {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(file);
}
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error running WoT script:", error);
process.exit(1);
}
}
}