Skip to content

Commit

Permalink
Merge pull request #325 from elsoul/utils
Browse files Browse the repository at this point in the history
rm isLog from execAsync
  • Loading branch information
POPPIN-FUMI authored Mar 13, 2024
2 parents 9db0ff2 + bc20ae4 commit daec801
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-guests-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@skeet-framework/utils": patch
---

rm isLog from execAsync
15 changes: 4 additions & 11 deletions packages/utils/src/lib/execAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ const execAsyncCmd = promisify(exec)
* Asynchronously executes a shell command in a child process, optionally logs the output, and allows execution in a specific working directory.
*
* This function provides an async/await interface for executing shell commands using Node.js's `child_process.exec` method, simplified by `promisify`.
* It executes the command provided as a string, captures stdout and stderr, and based on the `isLog` parameter, it may log these outputs to the console.
* It executes the command provided as a string, captures stdout and stderr, it may log these outputs to the console.
*
* @param command - A string representing the command and its arguments to be executed.
* @param cwd - The current working directory in which the command should be executed. Defaults to '.'.
* @param isLog - A boolean indicating whether to log stdout and stderr to the console. Defaults to true.
* @returns A promise that resolves to an object containing `stdout` and `stderr` from the executed command. In case of execution error, `stdout` will be an empty string, and `stderr` will contain the error message.
*
* @example
* // Example of executing a shell command
* async function runCommand() {
* const command = 'ls -l';
* const cwd = '/usr';
* const isLog = true;
* const cwd = '.';
*
* const { stdout, stderr } = await execAsync(command, cwd, isLog);
* const { stdout, stderr } = await execAsync(command, cwd);
* // Outputs are logged based on the isLog parameter
* }
*
Expand All @@ -30,18 +28,13 @@ const execAsyncCmd = promisify(exec)
* });
*/

export const execAsync = async (command: string, cwd = '.', isLog = true) => {
export const execAsync = async (command: string, cwd = '.') => {
try {
const { stdout, stderr } = await execAsyncCmd(command, {
cwd,
})
if (isLog) console.log(stdout)
if (stderr) {
if (isLog) console.error(stderr)
}
return { stdout, stderr }
} catch (error) {
if (isLog) console.error('Error executing command:', error)
return { stdout: '', stderr: String(error) }
}
}

0 comments on commit daec801

Please sign in to comment.