refactor: yarn runner man

This commit is contained in:
2026-03-03 15:09:25 +08:00
parent 8d5b15ad51
commit e13ead2309
5 changed files with 46 additions and 30 deletions
+7
View File
@@ -499,4 +499,11 @@ export class ExpressionEvaluator {
getVariable(name: string): unknown {
return this.variables[name];
}
registerFunctions(functions: Record<string, (...args: unknown[]) => unknown>){
this.functions = {
...this.functions,
...functions
}
}
}
+17 -3
View File
@@ -28,7 +28,6 @@ type CompiledOption = {
export class YarnRunner {
private readonly program: IRProgram;
private readonly variables: Record<string, unknown>;
private readonly functions: Record<string, (...args: unknown[]) => unknown>;
private readonly handleCommand?: (command: string, parsed?: ReturnType<typeof parseCommand>) => void;
private readonly commandHandler: CommandHandler;
private readonly evaluator: ExpressionEvaluator;
@@ -59,7 +58,7 @@ export class YarnRunner {
this.variables[normalizedKey] = value;
}
}
this.functions = {
let functions = {
// Default conversion helpers
string: (v: unknown) => String(v ?? ""),
number: (v: unknown) => Number(v),
@@ -115,12 +114,25 @@ export class YarnRunner {
} as Record<string, (...args: unknown[]) => unknown>;
this.handleCommand = opts.handleCommand;
this.onStoryEnd = opts.onStoryEnd;
this.evaluator = new ExpressionEvaluator(this.variables, this.functions, this.program.enums);
this.evaluator = new ExpressionEvaluator(this.variables, functions, this.program.enums);
this.commandHandler = opts.commandHandler ?? new CommandHandler(this.variables);
this.nodeTitle = opts.startAt;
this.step();
}
public registerFunctions(functions: Record<string, (...args: unknown[]) => unknown>){
this.evaluator.registerFunctions(functions);
}
public registerCommands(commands: Record<string, (args: unknown[], evaluator?: ExpressionEvaluator) => void | Promise<void>>) {
for(const key in commands){
this.commandHandler.register(key, (args, evaluator) => {
if(!evaluator) return;
commands[key].call(this, args.map(arg => evaluator.evaluateExpression(arg)));
});
}
};
/**
* Resolve a node title to an actual node (handling node groups).
@@ -599,6 +611,8 @@ export class YarnRunner {
* Get variable value.
*/
getVariable(name: string): unknown {
if(this.evaluator.isSmartVariable(name))
return this.evaluator.evaluateExpression(`$${name}`);
return this.variables[name];
}