Skip to content

Commit 578e75a

Browse files
committed
feat: auto-install globally in background when run via npx
1 parent d7ca4a4 commit 578e75a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/react-doctor/src/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { scan } from "./scan.js";
99
import { selectProjects } from "./utils/select-projects.js";
1010
import { prompts } from "./utils/prompts.js";
1111
import { maybePromptSkillInstall } from "./utils/skill-prompt.js";
12+
import { maybeInstallGlobally } from "./utils/global-install.js";
1213

1314
const VERSION = process.env.VERSION ?? "0.0.0";
1415

@@ -193,6 +194,7 @@ program.addCommand(fixCommand);
193194
program.addCommand(installAmiCommand);
194195

195196
const main = async () => {
197+
maybeInstallGlobally();
196198
await program.parseAsync();
197199
};
198200

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { spawn, execSync } from "node:child_process";
2+
3+
const isGloballyInstalled = (): boolean => {
4+
try {
5+
const globalBinPath = execSync("which react-doctor", {
6+
stdio: "pipe",
7+
encoding: "utf-8",
8+
}).trim();
9+
return !globalBinPath.includes("/_npx/");
10+
} catch {
11+
return false;
12+
}
13+
};
14+
15+
export const maybeInstallGlobally = (): void => {
16+
try {
17+
if (isGloballyInstalled()) return;
18+
19+
const child = spawn("npm", ["install", "-g", "react-doctor@latest"], {
20+
detached: true,
21+
stdio: "ignore",
22+
});
23+
child.on("error", () => {});
24+
child.unref();
25+
} catch {
26+
// noop
27+
}
28+
};

0 commit comments

Comments
 (0)