Skip to content

Instantly share code, notes, and snippets.

@MeetBhingradiya
Created August 26, 2023 06:23
Show Gist options
  • Save MeetBhingradiya/6dd3b63604ce84ec75abb79bebb5d94b to your computer and use it in GitHub Desktop.
Save MeetBhingradiya/6dd3b63604ce84ec75abb79bebb5d94b to your computer and use it in GitHub Desktop.
TypeScript CLI Command Executor with Colored Output and OneByOne Execute Policy.
import childProcess from 'child_process';
interface ExecuteCommand_Options {
/**
* ? The command to be execute.
*/
command: string
/**
* @default process.cwd()
* ? The current working directory to execute the command in.
*/
cwd?: string
/**
* @default false
* ? Overlap the terminal output as a prefix.
*/
prefix?: string
disableLogs?: boolean
}
interface ExecuteCommands_Options {
/**
* ? Childs Object for @function ExecuteCommand
*/
Childs: Array<ExecuteCommand_Options>
/**
* @default false
* ? If true, the commands will be executed one by one in the order they are passed in.
* ? This is useful when building a project that depends on another project or packages.
*/
oneByOne?: boolean
}
/**
*
* @param param0 command, cwd, prefix
* @returns
*/
async function ExecuteCommand({ command, cwd = process.cwd(), prefix, disableLogs = false }: ExecuteCommand_Options) {
const child = childProcess.spawn(command, {
/** Execute Command in Shell */
shell: true,
/** Current Working Directory */
cwd: cwd,
/** Capture Output */
stdio: 'pipe',
/** Need Color Output */
env: { FORCE_COLOR: '1' },
});
child.stdout?.on('data', (data: Buffer) => {
if (disableLogs) {
return;
}
if (prefix === undefined) {
prefix = '';
}
console.log(`${prefix} ${data.toString()}`);
});
child.stderr?.on('data', (data: Buffer) => {
console.error(`${prefix} ${data.toString()}`);
});
child.on('error', (error: any) => {
console.error(`${prefix} Error: ${error.message}`);
});
child.on('exit', (code: number) => {
if (code !== 0) {
console.error(`${prefix} Command exited with code ${code}`);
}
});
return child;
}
/**
* #### ExecuteCommands - Execute multiple commands for Cli Plugin
* @param `Childs`
* @returns `Promise<childProcess.ChildProcess[]>`
*/
async function ExecuteCommands({ Childs, oneByOne = false }: ExecuteCommands_Options) {
const childProcesses: Promise<childProcess.ChildProcess>[] = [];
for (const child of Childs) {
const childProcessPromise = ExecuteCommand(child);
childProcesses.push(childProcessPromise);
if (oneByOne) {
await childProcessPromise;
}
}
await Promise.all(childProcesses);
return childProcesses.map((promise) => promise.then((child) => child));
}
export {
ExecuteCommand,
ExecuteCommands,
ExecuteCommand_Options,
ExecuteCommands_Options
}
@MeetBhingradiya
Copy link
Author

MeetBhingradiya commented Aug 26, 2023

  • Import the module and the necessary types.
  • Use the ExecuteCommand function to execute a single command with customizable options such as the working directory, prefix, and colored output.
  • Use the ExecuteCommands function to run multiple commands sequentially, optionally waiting for each command to complete before proceeding to the next.
  • This module is designed to enhance the CLI command execution experience in TypeScript projects, providing a flexible and colorful way to interact with the command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment