import type { SandstoneCore } from 'sandstone/core' import type { CommandNode } from 'sandstone/core/nodes' import type { SandstonePack } from 'sandstone/pack' import type { SandstoneCommands } from './commands' type InstanceTypeOr CommandNode) | undefined, X> = NODE extends undefined ? X : NODE extends new ( ...args: any ) => infer R ? R : never /** * This is what final commands return. * It has a protected access to the node that was executed, allowing users to access the node's properties. * * This should only be used by users for advanced usage. */ export class FinalCommandOutput { constructor(protected node: CommandNode) {} } export type CommandNodeConstructor = new (...args: any) => CommandNode export type CommandArgumentsConstructor = new (...args: any) => any export abstract class CommandArguments< NODE extends CommandNodeConstructor | undefined = CommandNodeConstructor | undefined, > { protected NodeType?: NODE protected sandstoneCore: SandstoneCore protected sandstoneCommands: SandstoneCommands constructor( protected sandstonePack: SandstonePack, protected readonly isMacro: boolean = false, protected previousNode?: CommandNode, protected autoCommit = true, ) { this.sandstoneCore = sandstonePack.core this.sandstoneCommands = sandstonePack.commands } protected getNode: () => InstanceTypeOr = () => { if (this.previousNode) { // This is not a root-level command, so we can use the previous node. return this.previousNode } // Automatically create the node for root-level commands. if (this.NodeType) { /* Typescript does not manage to remove undefined for some reasons */ /* @ts-ignore */ const node = new this.NodeType(this.sandstonePack) as CommandNode // Propagate the macro context flag from the wrapping command class so // visitors (e.g. WithNodeVisitor) can inspect it before serialization // runs `getValue()` to set it lazily. node.isMacro = node.isMacro || this.isMacro return node as any } throw new Error('No node type specified & no previous node for a non-root-level command') } protected finalCommand = ( args?: NODE extends CommandNodeConstructor ? InstanceType['args'] : any[], currentNode?: InstanceTypeOr | undefined, ): FinalCommandOutput => { // No followup. We can add arguments & commit. const node = currentNode ?? this.getNode() if (args) { node.args.push(...args) } if (this.autoCommit) { node.commit() } return new FinalCommandOutput(node) } protected subCommand = ( args: NODE extends CommandNodeConstructor ? InstanceType['args'] : any[], NextArgumentType: NEXT_ARGUMENT, executable = false, additionalNextArgs: unknown[] = [], currentNode?: InstanceTypeOr | undefined, ): NEXT_ARGUMENT extends CommandArgumentsConstructor ? InstanceType : FinalCommandOutput => { const node = currentNode ?? this.getNode() /* * The command has followup arguments. We need to append the arguments to the node, and return a new instance of the followup command. * If the command is executable at this point, we can commit it. */ if (args) { node.args.push(...args) } if (executable && this.autoCommit) { node.commit() } return new NextArgumentType(this.sandstonePack, this.isMacro, node, this.autoCommit, ...additionalNextArgs) as any } }