Skip to content

Commit ad03171

Browse files
committed
fix: don't use spawnSync, but a non-blocking alternative
1 parent 4797028 commit ad03171

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/gitManager/simpleGit.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { spawnSync } from "child_process";
21
import debug from "debug";
32
import * as fsPromises from "fs/promises";
43
import type { FileSystemAdapter } from "obsidian";
@@ -27,7 +26,7 @@ import type {
2726
Status,
2827
} from "../types";
2928
import { CurrentGitAction, NoNetworkError } from "../types";
30-
import { impossibleBranch, splitRemoteBranch } from "../utils";
29+
import { impossibleBranch, spawnAsync, splitRemoteBranch } from "../utils";
3130
import { GitManager } from "./gitManager";
3231

3332
export class SimpleGit extends GitManager {
@@ -40,7 +39,7 @@ export class SimpleGit extends GitManager {
4039
}
4140

4241
async setGitInstance(ignoreError = false): Promise<void> {
43-
if (this.isGitInstalled()) {
42+
if (await this.isGitInstalled()) {
4443
const adapter = this.app.vault.adapter as FileSystemAdapter;
4544
const vaultBasePath = adapter.getBasePath();
4645
let basePath = vaultBasePath;
@@ -736,7 +735,7 @@ export class SimpleGit extends GitManager {
736735
async checkRequirements(): Promise<
737736
"valid" | "missing-repo" | "missing-git"
738737
> {
739-
if (!this.isGitInstalled()) {
738+
if (!(await this.isGitInstalled())) {
740739
return "missing-git";
741740
}
742741
if (!(await this.git.checkIsRepo())) {
@@ -1045,21 +1044,19 @@ export class SimpleGit extends GitManager {
10451044
}
10461045
}
10471046

1048-
private isGitInstalled(): boolean {
1047+
private async isGitInstalled(): Promise<boolean> {
10491048
// https://github.com/steveukx/git-js/issues/402
10501049
const gitPath = this.plugin.localStorage.getGitPath();
1051-
const command = spawnSync(gitPath || "git", ["--version"], {
1052-
stdio: "ignore",
1053-
});
1050+
const command = await spawnAsync(gitPath || "git", ["--version"], {});
10541051

10551052
if (command.error) {
10561053
if (Platform.isWin && !gitPath) {
10571054
this.plugin.log(
10581055
`Git not found in PATH. Checking standard installation path(${DEFAULT_WIN_GIT_PATH}) of Git for Windows.`
10591056
);
1060-
const command = spawnSync(DEFAULT_WIN_GIT_PATH, ["--version"], {
1061-
stdio: "ignore",
1062-
});
1057+
const command = await spawnAsync(DEFAULT_WIN_GIT_PATH, [
1058+
"--version",
1059+
]);
10631060
if (command.error) {
10641061
console.error(command.error);
10651062
return false;

src/utils.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,13 @@ export function spawnAsync(
237237
command: string,
238238
args: string[],
239239
options: SpawnOptionsWithoutStdio = {}
240-
): Promise<{ stdout: string; stderr: string; code: number }> {
241-
return new Promise((resolve, reject) => {
240+
): Promise<{
241+
stdout: string;
242+
stderr: string;
243+
code: number;
244+
error: Error | undefined;
245+
}> {
246+
return new Promise((resolve, _) => {
242247
// Spawn the child process
243248
const child = spawn(command, args, options);
244249

@@ -257,13 +262,12 @@ export function spawnAsync(
257262

258263
// Handle process errors (e.g., command not found)
259264
child.on("error", (err) => {
260-
reject(
261-
new Error(
262-
`Failed to start subprocess. Command: '${command} ${args.join(
263-
" "
264-
)}', Error: ${err.message}`
265-
)
266-
);
265+
resolve({
266+
error: new Error(err.message),
267+
stdout: stdoutBuffer,
268+
stderr: stdoutBuffer,
269+
code: 1,
270+
});
267271
});
268272

269273
// Handle process exit
@@ -273,6 +277,7 @@ export function spawnAsync(
273277
stdout: stdoutBuffer,
274278
stderr: stderrBuffer,
275279
code: code ?? 1,
280+
error: undefined,
276281
});
277282
});
278283
});

0 commit comments

Comments
 (0)