Skip to content

Commit f26a99e

Browse files
committed
refactor(utils): optimize process.exec by adding options for output control
1 parent 565f66d commit f26a99e

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "commitfy",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"main": "lib/index.js",
55
"repository": "https://github.com/ribeirogab/commitfy.git",
66
"author": "ribeirogab <[email protected]>",

src/commands/generate-commit.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export class GenerateCommit {
2929
process.exit(0);
3030
}
3131

32-
const diff = await this.processUtils.exec('git diff --cached');
32+
const diff = await this.processUtils.exec('git diff --cached', {
33+
showStdout: false,
34+
});
3335

3436
if (!diff) {
3537
console.error(`${this.appUtils.name}: no changes to commit.`);
@@ -57,7 +59,9 @@ export class GenerateCommit {
5759
return this.execute();
5860
}
5961

60-
await this.processUtils.exec(`git commit -m "${response}"`);
62+
await this.processUtils.exec(`git commit -m "${response}"`, {
63+
showStdout: true,
64+
});
6165

6266
process.exit(0);
6367
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export type ProcessUtilsExecOptions = {
2+
showStdout: boolean;
3+
};
4+
15
export interface ProcessUtils {
2-
exec(command: string): Promise<string>;
6+
exec(command: string, options?: ProcessUtilsExecOptions): Promise<string>;
37
}

src/utils/process.utils.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,48 @@ import { exec } from 'node:child_process';
22

33
import type {
44
AppUtils,
5+
ProcessUtilsExecOptions,
56
ProcessUtils as ProcessUtilsInterface,
67
} from '../interfaces';
78

89
export class ProcessUtils implements ProcessUtilsInterface {
910
constructor(private readonly appUtils: AppUtils) {}
1011

11-
public async exec(command: string): Promise<string> {
12+
public async exec(
13+
command: string,
14+
options: ProcessUtilsExecOptions = { showStdout: false },
15+
): Promise<string> {
1216
return new Promise((resolve, reject) => {
13-
exec(command, (error, stdout, stderr) => {
14-
if (error) {
15-
this.appUtils.logger.error(
16-
`Error executing command '${command}'\n`,
17-
error,
18-
);
19-
20-
reject(stderr);
21-
} else {
22-
resolve(stdout);
17+
const childProcess = exec(command);
18+
let stdout = '';
19+
20+
childProcess.stdout.on('data', (data) => {
21+
if (options.showStdout) {
22+
process.stdout.write(data);
2323
}
2424

25-
process.stdout.on('data', (data) => {
26-
console.log(data.toString());
27-
});
25+
stdout += data.toString();
26+
});
27+
28+
childProcess.stderr.on('data', (data) => {
29+
process.stderr.write(data);
30+
});
31+
32+
childProcess.on('error', (error) => {
33+
this.appUtils.logger.error(
34+
`Error executing command '${command}'\n`,
35+
error,
36+
);
37+
38+
reject(error);
39+
});
2840

29-
process.stderr.on('data', (data) => {
30-
console.error(data.toString());
31-
});
41+
childProcess.on('close', (code) => {
42+
if (code !== 0) {
43+
reject(`Command failed with exit code ${code}`);
44+
} else {
45+
resolve(stdout);
46+
}
3247
});
3348
});
3449
}

0 commit comments

Comments
 (0)