import { existsSync, mkdirSync } from 'node:fs'; import { type Options as ExecaOptions, type ResultPromise, type SyncOptions, type SyncResult, execa, execaSync, parseCommandString, } from 'execa'; import type { BaseGenerator } from '../generator.js'; export class SpawnCommandMixin { /** * Resolve the options to spawn a command with, defaulting `cwd` to the destination root. * A command cannot be spawned in a directory that does not exist, so create it on demand. * * @param opt execa options * @returns the options with `cwd` set */ private resolveSpawnOptions( this: BaseGenerator, opt?: OptionsType, ): OptionsType { const cwd = opt?.cwd ?? this.destinationRoot(); if (!existsSync(cwd)) { mkdirSync(cwd, { recursive: true }); } return { ...opt, cwd } as OptionsType; } /** * Normalize a command across OS and spawn it (asynchronously). * * @param command program to execute * @param opt execa options options * @see https://github.com/sindresorhus/execa/blob/main/docs/execution.md#command-string */ spawnCommand( this: BaseGenerator, command: string, opt?: OptionsType, ): ResultPromise { const [file, ...args] = parseCommandString(command); return this.spawn(file, args, opt); } /** * Normalize a command across OS and spawn it (asynchronously). It is cancelled when the adapter of the environment is * aborted, unless the options give a `cancelSignal`. * * @param command program to execute * @param args list of arguments to pass to the program * @param opt execa options options * @see https://github.com/sindresorhus/execa#execafile-arguments-options */ spawn( this: BaseGenerator, command: string, args?: readonly string[], opt?: OptionsType, ): ResultPromise { // Cancelled with the operation the generator runs in, the adapter aborted: unless the options give a cancelSignal. const cancelSignal = this.env?.adapter?.signal; return execa( command, args, this.resolveSpawnOptions(cancelSignal ? { cancelSignal, ...opt } : opt), ) as any; } /** * Normalize a command across OS and spawn it (synchronously). * * @param command program to execute * @param opt execa options options * @see https://github.com/sindresorhus/execa/blob/main/docs/execution.md#command-string */ spawnCommandSync( this: BaseGenerator, command: string, opt?: OptionsType, ): SyncResult { const [file, ...args] = parseCommandString(command); return this.spawnSync(file, args, opt); } /** * Normalize a command across OS and spawn it (synchronously). * * @param command program to execute * @param args list of arguments to pass to the program * @param opt execa options options * @see https://github.com/sindresorhus/execa#execafile-arguments-options */ spawnSync( this: BaseGenerator, command: string, args?: readonly string[], opt?: OptionsType, ): SyncResult { return execaSync(command, args, this.resolveSpawnOptions(opt)); } }