-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathnotify.ts
More file actions
223 lines (203 loc) · 6.1 KB
/
Copy pathnotify.ts
File metadata and controls
223 lines (203 loc) · 6.1 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { spawn, spawnSync, type SpawnOptions } from "child_process";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
type NotifyChildProcess = {
once(event: "error", listener: (error: NodeJS.ErrnoException) => void): NotifyChildProcess;
unref(): void;
};
export type NotifySpawn = (
command: string,
args: string[],
options: Pick<SpawnOptions, "cwd" | "detached" | "env" | "stdio">
) => NotifyChildProcess;
export function formatDurationSeconds(durationMs: number): string {
const safeMs = Number.isFinite(durationMs) ? Math.max(0, durationMs) : 0;
return String(Math.floor(safeMs / 1000));
}
export type NotifyContext = {
status?: string;
failReason?: string;
question?: string;
body?: string;
title?: string;
};
export function buildNotifyEnv(
durationMs: number,
baseEnv: NodeJS.ProcessEnv = process.env,
context: NotifyContext = {}
): NodeJS.ProcessEnv {
const env: NodeJS.ProcessEnv = {
...baseEnv,
DURATION: formatDurationSeconds(durationMs),
};
delete env.STATUS;
delete env.FAIL_REASON;
delete env.QUESTION;
delete env.BODY;
delete env.TITLE;
if (context.status) {
env.STATUS = context.status;
}
if (context.failReason) {
env.FAIL_REASON = context.failReason;
}
if (context.question) {
env.QUESTION = context.question;
}
if (context.body) {
env.BODY = context.body;
}
if (context.title) {
env.TITLE = context.title;
}
return env;
}
export function launchNotifyScript(
notifyPath: string | undefined,
durationMs: number,
workingDirectory?: string,
spawnProcess: NotifySpawn = spawn as unknown as NotifySpawn,
configuredEnv: Record<string, string> = {},
context: NotifyContext = {}
): void {
const commandPath = notifyPath?.trim();
if (!commandPath) {
return;
}
const options = {
cwd: workingDirectory,
detached: process.platform !== "win32",
env: buildNotifyEnv(durationMs, { ...process.env, ...configuredEnv }, context),
stdio: "ignore" as const,
};
try {
const child = spawnProcess(commandPath, [], options);
child.once("error", (error) => {
if (process.platform === "win32") {
return;
}
if (error.code !== "EACCES" && error.code !== "ENOEXEC") {
return;
}
// Fall back to /bin/sh so plain shell scripts still run without execute permissions.
try {
const fallbackChild = spawnProcess("/bin/sh", [commandPath], options);
fallbackChild.once("error", () => undefined);
fallbackChild.unref();
} catch {
// Ignore notification failures.
}
});
child.unref();
} catch {
// Ignore notification failures.
}
}
/**
* Resolve the bundled built-in notification script shipped with the CLI.
* The esbuild ESM bundle does not inject `__dirname`, so we replicate the
* same fallback pattern used by `getExtensionRoot`.
*/
export function resolveBuiltinNotifyPath(): string | null {
if (process.platform !== "win32") {
return null;
}
try {
const moduleDir =
typeof __dirname !== "undefined"
? path.resolve(__dirname)
: path.resolve(path.dirname(fileURLToPath(import.meta.url)));
const candidates = [
path.resolve(moduleDir, "..", "templates", "tools", "deepcode-notify.ps1"),
path.resolve(moduleDir, "..", "..", "templates", "tools", "deepcode-notify.ps1"),
];
return candidates.find((candidate) => fs.existsSync(candidate)) ?? null;
} catch {
return null;
}
}
export function captureForegroundWindowHwnd(): string | undefined {
if (process.platform !== "win32") {
return undefined;
}
const script = [
"$code = @'",
"using System;",
"using System.Runtime.InteropServices;",
"public static class DCForeground {",
' [DllImport("user32.dll")]',
" public static extern IntPtr GetForegroundWindow();",
"}",
"'@",
"Add-Type -TypeDefinition $code",
"[DCForeground]::GetForegroundWindow().ToInt64()",
].join("\n");
try {
const result = spawnSync("powershell.exe", ["-ExecutionPolicy", "Bypass", "-NoProfile", "-Command", script], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
timeout: 700,
windowsHide: true,
});
if (result.status !== 0) {
return undefined;
}
const hwnd = result.stdout.trim();
return /^[1-9]\d*$/.test(hwnd) ? hwnd : undefined;
} catch {
return undefined;
}
}
function getBuiltinNotifyEnv(
durationMs: number,
configuredEnv: Record<string, string>,
context: NotifyContext,
workingDirectory?: string
): NodeJS.ProcessEnv {
const env = buildNotifyEnv(durationMs, { ...process.env, ...configuredEnv }, context);
env.DEEPCODE_NOTIFY_PROCESS_PID ??= String(process.pid);
env.DEEPCODE_NOTIFY_PARENT_PID ??= String(process.ppid);
const debugEnabled = env.DEEPCODE_NOTIFY_DEBUG === "1" || env.DEEPCODE_NOTIFY_DEBUG === "true";
if (debugEnabled && !env.DEEPCODE_NOTIFY_DEBUG_LOG) {
env.DEEPCODE_NOTIFY_DEBUG_LOG = path.join(workingDirectory ?? process.cwd(), ".deepcode", "notify.log");
}
return env;
}
/**
* Launch the built-in Windows notification (PowerShell BalloonTip with
* click-to-focus behaviour). Has no effect on non-Windows platforms.
*
* This is intentionally separate from `launchNotifyScript` so that callers
* can decide whether to prefer a user-configured external script or the
* built-in one.
*/
export function launchBuiltinNotify(
durationMs: number,
workingDirectory?: string,
spawnProcess: NotifySpawn = spawn as unknown as NotifySpawn,
configuredEnv: Record<string, string> = {},
context: NotifyContext = {}
): void {
const scriptPath = resolveBuiltinNotifyPath();
if (!scriptPath) {
return;
}
const options = {
cwd: workingDirectory,
detached: false,
env: getBuiltinNotifyEnv(durationMs, configuredEnv, context, workingDirectory),
stdio: "ignore" as const,
};
try {
const child = spawnProcess(
"powershell.exe",
["-ExecutionPolicy", "Bypass", "-NoProfile", "-File", scriptPath],
options
);
child.once("error", () => undefined);
child.unref();
} catch {
// Ignore notification failures.
}
}