yeoman-generator
Version:
Rails-inspired generator system that provides scaffolding for your apps
63 lines (62 loc) • 2.31 kB
JavaScript
import { existsSync, mkdirSync } from 'node:fs';
import { execa, execaSync, parseCommandString, } from 'execa';
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
*/
resolveSpawnOptions(opt) {
const cwd = opt?.cwd ?? this.destinationRoot();
if (!existsSync(cwd)) {
mkdirSync(cwd, { recursive: true });
}
return { ...opt, cwd };
}
/**
* 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(command, opt) {
const [file, ...args] = parseCommandString(command);
return this.spawn(file, args, opt);
}
/**
* Normalize a command across OS and spawn it (asynchronously).
*
* @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(command, args, opt) {
return execa(command, args, this.resolveSpawnOptions(opt));
}
/**
* 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(command, opt) {
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(command, args, opt) {
return execaSync(command, args, this.resolveSpawnOptions(opt));
}
}