everything-dev 0.2.1 → 0.3.1

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.
@@ -2,156 +2,165 @@ import { existsSync } from "node:fs";
2
2
  import { mkdir, readFile, writeFile } from "node:fs/promises";
3
3
  import { dirname, join } from "node:path";
4
4
  import { Context, Effect, Layer, Ref } from "every-plugin/effect";
5
- import { getConfigDir } from "../config";
6
-
7
- const getPidFilePath = () => join(getConfigDir(), ".bos", "pids.json");
5
+ import { getProjectRoot } from "../config";
6
+
7
+ const getPidFilePath = () => {
8
+ try {
9
+ return join(getProjectRoot(), ".bos", "pids.json");
10
+ } catch {
11
+ // Fallback to cwd if config not loaded
12
+ return join(process.cwd(), ".bos", "pids.json");
13
+ }
14
+ };
8
15
 
9
16
  export interface TrackedProcess {
10
- pid: number;
11
- name: string;
12
- port: number;
13
- startedAt: number;
14
- command: string;
17
+ pid: number;
18
+ name: string;
19
+ port: number;
20
+ startedAt: number;
21
+ command: string;
15
22
  }
16
23
 
17
24
  export interface ProcessRegistry {
18
- readonly tracked: Ref.Ref<Map<number, TrackedProcess>>;
19
- track: (proc: TrackedProcess) => Effect.Effect<void>;
20
- untrack: (pid: number) => Effect.Effect<void>;
21
- getAll: () => Effect.Effect<TrackedProcess[]>;
22
- killAll: (force?: boolean) => Effect.Effect<{ killed: number[]; failed: number[] }>;
23
- persist: () => Effect.Effect<void>;
24
- restore: () => Effect.Effect<void>;
25
+ readonly tracked: Ref.Ref<Map<number, TrackedProcess>>;
26
+ track: (proc: TrackedProcess) => Effect.Effect<void>;
27
+ untrack: (pid: number) => Effect.Effect<void>;
28
+ getAll: () => Effect.Effect<TrackedProcess[]>;
29
+ killAll: (
30
+ force?: boolean,
31
+ ) => Effect.Effect<{ killed: number[]; failed: number[] }>;
32
+ persist: () => Effect.Effect<void>;
33
+ restore: () => Effect.Effect<void>;
25
34
  }
26
35
 
27
36
  const isProcessAlive = (pid: number): boolean => {
28
- try {
29
- process.kill(pid, 0);
30
- return true;
31
- } catch {
32
- return false;
33
- }
37
+ try {
38
+ process.kill(pid, 0);
39
+ return true;
40
+ } catch {
41
+ return false;
42
+ }
34
43
  };
35
44
 
36
45
  const killProcess = (pid: number, signal: NodeJS.Signals): boolean => {
37
- try {
38
- process.kill(pid, signal);
39
- return true;
40
- } catch {
41
- return false;
42
- }
46
+ try {
47
+ process.kill(pid, signal);
48
+ return true;
49
+ } catch {
50
+ return false;
51
+ }
43
52
  };
44
53
 
45
54
  const make = Effect.gen(function* () {
46
- const tracked = yield* Ref.make(new Map<number, TrackedProcess>());
47
- const pidFile = getPidFilePath();
48
-
49
- const track: ProcessRegistry["track"] = (proc) =>
50
- Effect.gen(function* () {
51
- yield* Ref.update(tracked, (m) => new Map(m).set(proc.pid, proc));
52
- yield* persist();
53
- });
54
-
55
- const untrack: ProcessRegistry["untrack"] = (pid) =>
56
- Effect.gen(function* () {
57
- yield* Ref.update(tracked, (m) => {
58
- const copy = new Map(m);
59
- copy.delete(pid);
60
- return copy;
61
- });
62
- yield* persist();
63
- });
64
-
65
- const getAll: ProcessRegistry["getAll"] = () =>
66
- Ref.get(tracked).pipe(Effect.map((m) => Array.from(m.values())));
67
-
68
- const killAll: ProcessRegistry["killAll"] = (force = false) =>
69
- Effect.gen(function* () {
70
- const procs = yield* getAll();
71
- const killed: number[] = [];
72
- const failed: number[] = [];
73
-
74
- for (const proc of procs) {
75
- if (!isProcessAlive(proc.pid)) {
76
- yield* untrack(proc.pid);
77
- continue;
78
- }
79
-
80
- const signal = force ? "SIGKILL" : "SIGTERM";
81
- if (killProcess(proc.pid, signal)) {
82
- killed.push(proc.pid);
83
- yield* untrack(proc.pid);
84
- } else {
85
- failed.push(proc.pid);
86
- }
87
- }
88
-
89
- if (!force && failed.length > 0) {
90
- yield* Effect.sleep("500 millis");
91
- for (const pid of [...failed]) {
92
- if (killProcess(pid, "SIGKILL")) {
93
- const idx = failed.indexOf(pid);
94
- if (idx !== -1) {
95
- failed.splice(idx, 1);
96
- killed.push(pid);
97
- }
98
- yield* untrack(pid);
99
- }
100
- }
101
- }
102
-
103
- yield* persist();
104
- return { killed, failed };
105
- });
106
-
107
- const persist: ProcessRegistry["persist"] = () =>
108
- Effect.gen(function* () {
109
- const procs = yield* getAll();
110
- const dir = dirname(pidFile);
111
-
112
- yield* Effect.tryPromise({
113
- try: async () => {
114
- if (!existsSync(dir)) {
115
- await mkdir(dir, { recursive: true });
116
- }
117
- await writeFile(pidFile, JSON.stringify(procs, null, 2));
118
- },
119
- catch: () => new Error("Failed to persist PIDs"),
120
- }).pipe(Effect.catchAll(() => Effect.void));
121
- });
122
-
123
- const restore: ProcessRegistry["restore"] = () =>
124
- Effect.gen(function* () {
125
- if (!existsSync(pidFile)) return;
126
-
127
- const content = yield* Effect.tryPromise({
128
- try: () => readFile(pidFile, "utf8"),
129
- catch: () => new Error("Failed to read PID file"),
130
- }).pipe(Effect.catchAll(() => Effect.succeed("")));
131
-
132
- if (!content) return;
133
-
134
- let procs: TrackedProcess[];
135
- try {
136
- procs = JSON.parse(content) as TrackedProcess[];
137
- } catch {
138
- return;
139
- }
140
-
141
- const alive = procs.filter((p) => isProcessAlive(p.pid));
142
- yield* Ref.set(tracked, new Map(alive.map((p) => [p.pid, p])));
143
- });
144
-
145
- yield* restore();
146
-
147
- return { tracked, track, untrack, getAll, killAll, persist, restore };
55
+ const tracked = yield* Ref.make(new Map<number, TrackedProcess>());
56
+ const pidFile = getPidFilePath();
57
+
58
+ const track: ProcessRegistry["track"] = (proc) =>
59
+ Effect.gen(function* () {
60
+ yield* Ref.update(tracked, (m) => new Map(m).set(proc.pid, proc));
61
+ yield* persist();
62
+ });
63
+
64
+ const untrack: ProcessRegistry["untrack"] = (pid) =>
65
+ Effect.gen(function* () {
66
+ yield* Ref.update(tracked, (m) => {
67
+ const copy = new Map(m);
68
+ copy.delete(pid);
69
+ return copy;
70
+ });
71
+ yield* persist();
72
+ });
73
+
74
+ const getAll: ProcessRegistry["getAll"] = () =>
75
+ Ref.get(tracked).pipe(Effect.map((m) => Array.from(m.values())));
76
+
77
+ const killAll: ProcessRegistry["killAll"] = (force = false) =>
78
+ Effect.gen(function* () {
79
+ const procs = yield* getAll();
80
+ const killed: number[] = [];
81
+ const failed: number[] = [];
82
+
83
+ for (const proc of procs) {
84
+ if (!isProcessAlive(proc.pid)) {
85
+ yield* untrack(proc.pid);
86
+ continue;
87
+ }
88
+
89
+ const signal = force ? "SIGKILL" : "SIGTERM";
90
+ if (killProcess(proc.pid, signal)) {
91
+ killed.push(proc.pid);
92
+ yield* untrack(proc.pid);
93
+ } else {
94
+ failed.push(proc.pid);
95
+ }
96
+ }
97
+
98
+ if (!force && failed.length > 0) {
99
+ yield* Effect.sleep("500 millis");
100
+ for (const pid of [...failed]) {
101
+ if (killProcess(pid, "SIGKILL")) {
102
+ const idx = failed.indexOf(pid);
103
+ if (idx !== -1) {
104
+ failed.splice(idx, 1);
105
+ killed.push(pid);
106
+ }
107
+ yield* untrack(pid);
108
+ }
109
+ }
110
+ }
111
+
112
+ yield* persist();
113
+ return { killed, failed };
114
+ });
115
+
116
+ const persist: ProcessRegistry["persist"] = () =>
117
+ Effect.gen(function* () {
118
+ const procs = yield* getAll();
119
+ const dir = dirname(pidFile);
120
+
121
+ yield* Effect.tryPromise({
122
+ try: async () => {
123
+ if (!existsSync(dir)) {
124
+ await mkdir(dir, { recursive: true });
125
+ }
126
+ await writeFile(pidFile, JSON.stringify(procs, null, 2));
127
+ },
128
+ catch: () => new Error("Failed to persist PIDs"),
129
+ }).pipe(Effect.catchAll(() => Effect.void));
130
+ });
131
+
132
+ const restore: ProcessRegistry["restore"] = () =>
133
+ Effect.gen(function* () {
134
+ if (!existsSync(pidFile)) return;
135
+
136
+ const content = yield* Effect.tryPromise({
137
+ try: () => readFile(pidFile, "utf8"),
138
+ catch: () => new Error("Failed to read PID file"),
139
+ }).pipe(Effect.catchAll(() => Effect.succeed("")));
140
+
141
+ if (!content) return;
142
+
143
+ let procs: TrackedProcess[];
144
+ try {
145
+ procs = JSON.parse(content) as TrackedProcess[];
146
+ } catch {
147
+ return;
148
+ }
149
+
150
+ const alive = procs.filter((p) => isProcessAlive(p.pid));
151
+ yield* Ref.set(tracked, new Map(alive.map((p) => [p.pid, p])));
152
+ });
153
+
154
+ yield* restore();
155
+
156
+ return { tracked, track, untrack, getAll, killAll, persist, restore };
148
157
  });
149
158
 
150
159
  export class ProcessRegistryService extends Context.Tag("bos/ProcessRegistry")<
151
- ProcessRegistryService,
152
- ProcessRegistry
160
+ ProcessRegistryService,
161
+ ProcessRegistry
153
162
  >() {
154
- static Live = Layer.effect(ProcessRegistryService, make);
163
+ static Live = Layer.effect(ProcessRegistryService, make);
155
164
  }
156
165
 
157
166
  export const createProcessRegistry = (): Effect.Effect<ProcessRegistry> => make;