create-prisma-php-app 5.1.0-alpha.11 → 5.1.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,15 @@
1
- import { spawn } from "node:child_process";
1
+ import { execFile, spawn } from "node:child_process";
2
+ import { mkdir, readFile, readFileSync, rm, rmSync, writeFile } from "node:fs";
3
+ import { promisify } from "node:util";
4
+ import { dirname, join } from "node:path";
2
5
  import process from "node:process";
3
6
 
4
- const mode: "watch" | "build" =
5
- process.argv[2] === "watch" ? "watch" : "build";
7
+ const mode: "watch" | "build" = process.argv[2] === "watch" ? "watch" : "build";
8
+ const watcherPidFile = join(process.cwd(), ".pp", "postcss-watch.pid");
9
+ const mkdirAsync = promisify(mkdir);
10
+ const readFileAsync = promisify(readFile);
11
+ const rmAsync = promisify(rm);
12
+ const writeFileAsync = promisify(writeFile);
6
13
 
7
14
  const args: string[] = [
8
15
  "--max-old-space-size=6144",
@@ -16,19 +23,183 @@ if (mode === "watch") {
16
23
  args.push("--watch");
17
24
  }
18
25
 
26
+ function isValidPid(value: string): boolean {
27
+ return /^\d+$/.test(value.trim());
28
+ }
29
+
30
+ function isProcessRunning(pid: number): boolean {
31
+ try {
32
+ process.kill(pid, 0);
33
+ return true;
34
+ } catch {
35
+ return false;
36
+ }
37
+ }
38
+
39
+ function killWindowsProcessTree(pid: number): Promise<void> {
40
+ return new Promise((resolve) => {
41
+ const killer = execFile("taskkill", ["/F", "/T", "/PID", String(pid)], () =>
42
+ resolve(),
43
+ );
44
+
45
+ killer.on("error", () => resolve());
46
+ });
47
+ }
48
+
49
+ async function killProcessTree(pid: number): Promise<void> {
50
+ if (process.platform === "win32") {
51
+ await killWindowsProcessTree(pid);
52
+ return;
53
+ }
54
+
55
+ try {
56
+ process.kill(pid, "SIGTERM");
57
+ } catch {
58
+ return;
59
+ }
60
+ }
61
+
62
+ async function ensurePidDirectory(): Promise<void> {
63
+ await mkdirAsync(dirname(watcherPidFile), { recursive: true });
64
+ }
65
+
66
+ async function clearPidFile(): Promise<void> {
67
+ try {
68
+ const storedPid = (await readFileAsync(watcherPidFile, "utf8")).trim();
69
+ if (storedPid === String(process.pid)) {
70
+ await rmAsync(watcherPidFile, { force: true });
71
+ }
72
+ } catch {}
73
+ }
74
+
75
+ function clearPidFileSync(): void {
76
+ try {
77
+ const storedPid = readFileSync(watcherPidFile, "utf8").trim();
78
+ if (storedPid === String(process.pid)) {
79
+ rmSync(watcherPidFile, { force: true });
80
+ }
81
+ } catch {}
82
+ }
83
+
84
+ async function cleanupStaleWatcher(): Promise<void> {
85
+ if (mode !== "watch") {
86
+ return;
87
+ }
88
+
89
+ await ensurePidDirectory();
90
+
91
+ let storedPid = "";
92
+
93
+ try {
94
+ storedPid = (await readFileAsync(watcherPidFile, "utf8")).trim();
95
+ } catch {
96
+ return;
97
+ }
98
+
99
+ if (!isValidPid(storedPid)) {
100
+ await rmAsync(watcherPidFile, { force: true });
101
+ return;
102
+ }
103
+
104
+ const pid = Number(storedPid);
105
+
106
+ if (pid === process.pid) {
107
+ return;
108
+ }
109
+
110
+ if (!isProcessRunning(pid)) {
111
+ await rmAsync(watcherPidFile, { force: true });
112
+ return;
113
+ }
114
+
115
+ console.warn(
116
+ `[tailwind] Found stale PostCSS watcher (PID ${pid}), stopping it before restart.`,
117
+ );
118
+ await killProcessTree(pid);
119
+ await rmAsync(watcherPidFile, { force: true });
120
+ }
121
+
122
+ async function writePidFile(): Promise<void> {
123
+ if (mode !== "watch") {
124
+ return;
125
+ }
126
+
127
+ await ensurePidDirectory();
128
+ await writeFileAsync(watcherPidFile, `${process.pid}\n`, "utf8");
129
+ }
130
+
131
+ await cleanupStaleWatcher();
132
+ await writePidFile();
133
+
19
134
  const child = spawn(process.execPath, args, {
20
135
  stdio: "inherit",
136
+ windowsHide: true,
21
137
  env: {
22
138
  ...process.env,
23
139
  PP_POSTCSS_MODE: mode,
24
140
  },
25
141
  });
26
142
 
27
- child.on("exit", (code, signal) => {
143
+ let shuttingDown = false;
144
+
145
+ child.on("error", (error) => {
146
+ console.error(error);
147
+ void shutdown(1);
148
+ });
149
+
150
+ async function shutdown(exitCode: number): Promise<void> {
151
+ if (shuttingDown) {
152
+ return;
153
+ }
154
+
155
+ shuttingDown = true;
156
+
157
+ if (child.pid && child.exitCode === null && !child.killed) {
158
+ await killProcessTree(child.pid);
159
+ }
160
+
161
+ await clearPidFile();
162
+ process.exit(exitCode);
163
+ }
164
+
165
+ child.on("exit", async (code, signal) => {
166
+ await clearPidFile();
167
+
168
+ if (shuttingDown) {
169
+ process.exit(code ?? 0);
170
+ return;
171
+ }
172
+
28
173
  if (signal) {
29
- process.kill(process.pid, signal);
174
+ try {
175
+ process.kill(process.pid, signal);
176
+ } catch {
177
+ process.exit(1);
178
+ }
30
179
  return;
31
180
  }
32
181
 
33
182
  process.exit(code ?? 0);
34
183
  });
184
+
185
+ process.once("SIGINT", () => {
186
+ void shutdown(0);
187
+ });
188
+
189
+ process.once("SIGTERM", () => {
190
+ void shutdown(0);
191
+ });
192
+
193
+ process.once("uncaughtException", (error) => {
194
+ console.error(error);
195
+ void shutdown(1);
196
+ });
197
+
198
+ process.once("unhandledRejection", (reason) => {
199
+ console.error(reason);
200
+ void shutdown(1);
201
+ });
202
+
203
+ process.once("exit", () => {
204
+ clearPidFileSync();
205
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "5.1.0-alpha.11",
3
+ "version": "5.1.0-alpha.12",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",