-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathclient.ts
More file actions
119 lines (102 loc) · 2.97 KB
/
Copy pathclient.ts
File metadata and controls
119 lines (102 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { exec } from "node:child_process"
import { promisify } from "node:util"
import inquirer from "inquirer"
import pc from "picocolors"
const execAsync = promisify(exec)
async function isClientRunning(client?: string): Promise<boolean> {
if (!client) return false
try {
const platform = process.platform
const clientProcess =
{
claude: "Claude",
}[client] || client
if (platform === "win32") {
const { stdout } = await execAsync(
`tasklist /FI "IMAGENAME eq ${clientProcess}.exe" /NH`,
)
return stdout.includes(`${clientProcess}.exe`)
} else if (platform === "darwin") {
const { stdout } = await execAsync(`pgrep -x "${clientProcess}"`)
return !!stdout.trim()
} else if (platform === "linux") {
const { stdout } = await execAsync(
`pgrep -f "${clientProcess.toLowerCase()}"`,
)
return !!stdout.trim()
}
return false
} catch (_error) {
return false
}
}
async function restartClient(client: string): Promise<void> {
const clientProcess =
{
claude: "Claude",
}[client] || client
try {
const platform = process.platform
const isRunning = await isClientRunning(client)
if (isRunning) {
if (platform === "win32") {
await execAsync(`taskkill /F /IM "${clientProcess}.exe"`)
} else if (platform === "darwin") {
await execAsync(`killall "${clientProcess}"`)
} else if (platform === "linux") {
await execAsync(`pkill -f "${clientProcess.toLowerCase()}"`)
}
await new Promise((resolve) => setTimeout(resolve, 2000))
}
if (platform === "win32") {
await execAsync(`start "" "${clientProcess}.exe"`)
} else if (platform === "darwin") {
await execAsync(`open -a "${clientProcess}"`)
} else if (platform === "linux") {
await execAsync(clientProcess.toLowerCase())
}
console.log(`${clientProcess} has been restarted.`)
} catch (error) {
console.error(`Failed to restart ${clientProcess}:`, error)
}
}
export async function promptForRestart(client?: string): Promise<boolean> {
if (!client) return false
const isRunning = await isClientRunning(client)
if (!isRunning) {
return false
}
// Non-TTY: skip interactive prompt to avoid hanging in CI/agent environments
if (!process.stdin.isTTY) {
console.log(
`The ${client} app is running. Restart it manually to apply changes.`,
)
return false
}
const { shouldRestart } = await inquirer.prompt<{ shouldRestart: boolean }>([
{
type: "confirm",
name: "shouldRestart",
message: `Would you like to restart the ${client} app to apply changes?`,
default: true,
},
])
if (shouldRestart) {
console.log(`Restarting ${client} app...`)
await restartClient(client)
}
return shouldRestart
}
export function showPostInstallHint(client: string): void {
const cliClients: Record<string, string> = {
"claude-code": "Claude Code",
"gemini-cli": "Gemini CLI",
codex: "Codex",
}
const label = cliClients[client]
if (label) {
console.log(
pc.cyan(`ℹ You may need to restart ${label} for changes to take effect.`),
)
}
}