Skip to content

Commit 44b9e0f

Browse files
committed
feat(Script): add evaluate function
1 parent 02391c9 commit 44b9e0f

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/models/Script.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import vm from 'node:vm';
22
import type { Metas } from '../types.js';
33
import { MAIN_FUNCTION_EXEC } from '../const.js';
44
import type { ScriptExecution } from './ScriptExecution.js';
5+
import { ensureError } from '../utils/error.js';
56

67
export type ScriptArguments = {
78
script: string;
@@ -38,8 +39,19 @@ export class Script {
3839
this.tags = (parameters['tags'] as string).split(TAGS_SEPARATOR);
3940
this.bias = parameters['bias'] as number;
4041

41-
// Init script
42-
this.vmScript = new vm.Script(`${this.scriptCode}; ${MAIN_FUNCTION_EXEC}`);
42+
// Init Script
43+
this.vmScript = this.evaluate();
44+
}
45+
46+
evaluate() {
47+
try {
48+
return new vm.Script(`${this.scriptCode}; ${MAIN_FUNCTION_EXEC}`, {
49+
filename: this.name
50+
});
51+
} catch (err) {
52+
const error = ensureError(err);
53+
throw error;
54+
}
4355
}
4456

4557
run(context: ScriptExecution) {

0 commit comments

Comments
 (0)