@visulima/vis 1.0.0-alpha.1 → 1.0.0-alpha.11

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.
Files changed (120) hide show
  1. package/CHANGELOG.md +403 -12
  2. package/LICENSE.md +283 -0
  3. package/README.md +254 -9
  4. package/dist/bin.js +9 -146
  5. package/dist/config/index.d.ts +1818 -0
  6. package/dist/config/index.js +2 -0
  7. package/dist/generate/index.d.ts +157 -0
  8. package/dist/generate/index.js +3 -0
  9. package/dist/packem_chunks/applyDefaults.js +336 -0
  10. package/dist/packem_chunks/bin.js +9577 -0
  11. package/dist/packem_chunks/doctor-probe.js +112 -0
  12. package/dist/packem_chunks/fix.js +234 -0
  13. package/dist/packem_chunks/handler.js +99 -0
  14. package/dist/packem_chunks/handler10.js +53 -0
  15. package/dist/packem_chunks/handler11.js +32 -0
  16. package/dist/packem_chunks/handler12.js +100 -0
  17. package/dist/packem_chunks/handler13.js +25 -0
  18. package/dist/packem_chunks/handler14.js +916 -0
  19. package/dist/packem_chunks/handler15.js +206 -0
  20. package/dist/packem_chunks/handler16.js +124 -0
  21. package/dist/packem_chunks/handler17.js +13 -0
  22. package/dist/packem_chunks/handler18.js +106 -0
  23. package/dist/packem_chunks/handler19.js +19 -0
  24. package/dist/packem_chunks/handler2.js +75 -0
  25. package/dist/packem_chunks/handler20.js +29 -0
  26. package/dist/packem_chunks/handler21.js +222 -0
  27. package/dist/packem_chunks/handler22.js +237 -0
  28. package/dist/packem_chunks/handler23.js +101 -0
  29. package/dist/packem_chunks/handler24.js +110 -0
  30. package/dist/packem_chunks/handler25.js +402 -0
  31. package/dist/packem_chunks/handler26.js +13 -0
  32. package/dist/packem_chunks/handler27.js +63 -0
  33. package/dist/packem_chunks/handler28.js +34 -0
  34. package/dist/packem_chunks/handler29.js +458 -0
  35. package/dist/packem_chunks/handler3.js +95 -0
  36. package/dist/packem_chunks/handler30.js +170 -0
  37. package/dist/packem_chunks/handler31.js +530 -0
  38. package/dist/packem_chunks/handler32.js +214 -0
  39. package/dist/packem_chunks/handler33.js +119 -0
  40. package/dist/packem_chunks/handler34.js +630 -0
  41. package/dist/packem_chunks/handler35.js +283 -0
  42. package/dist/packem_chunks/handler36.js +542 -0
  43. package/dist/packem_chunks/handler37.js +762 -0
  44. package/dist/packem_chunks/handler38.js +989 -0
  45. package/dist/packem_chunks/handler39.js +574 -0
  46. package/dist/packem_chunks/handler4.js +90 -0
  47. package/dist/packem_chunks/handler40.js +1685 -0
  48. package/dist/packem_chunks/handler41.js +1088 -0
  49. package/dist/packem_chunks/handler42.js +797 -0
  50. package/dist/packem_chunks/handler43.js +2658 -0
  51. package/dist/packem_chunks/handler44.js +3886 -0
  52. package/dist/packem_chunks/handler45.js +2574 -0
  53. package/dist/packem_chunks/handler46.js +3769 -0
  54. package/dist/packem_chunks/handler47.js +1491 -0
  55. package/dist/packem_chunks/handler5.js +174 -0
  56. package/dist/packem_chunks/handler6.js +95 -0
  57. package/dist/packem_chunks/handler7.js +115 -0
  58. package/dist/packem_chunks/handler8.js +12 -0
  59. package/dist/packem_chunks/handler9.js +29 -0
  60. package/dist/packem_chunks/heal-accept.js +522 -0
  61. package/dist/packem_chunks/heal.js +673 -0
  62. package/dist/packem_chunks/index.js +873 -0
  63. package/dist/packem_chunks/loader.js +23 -0
  64. package/dist/packem_shared/VisUpdateApp-D-Yz_wvg.js +1316 -0
  65. package/dist/packem_shared/_commonjsHelpers-BqLXS_qQ.js +5 -0
  66. package/dist/packem_shared/ai-analysis-CHeB1joD.js +367 -0
  67. package/dist/packem_shared/ai-cache-Be_jexe4.js +142 -0
  68. package/dist/packem_shared/ai-fix-B9iQVcD2.js +379 -0
  69. package/dist/packem_shared/cache-directory-2qvs4goY.js +98 -0
  70. package/dist/packem_shared/catalog-BJTtyi-O.js +1371 -0
  71. package/dist/packem_shared/dependency-scan-A0KSklpG.js +188 -0
  72. package/dist/packem_shared/docker-2iZzc280.js +181 -0
  73. package/dist/packem_shared/failure-log-Cz3Z4SKL.js +100 -0
  74. package/dist/packem_shared/flakiness-goTxXuCX.js +180 -0
  75. package/dist/packem_shared/otel-DCvqCTz_.js +158 -0
  76. package/dist/packem_shared/otelPlugin-DFaLDvJf.js +3 -0
  77. package/dist/packem_shared/registry-CbqXI0rc.js +272 -0
  78. package/dist/packem_shared/run-summary-utils-PVMl4aIh.js +130 -0
  79. package/dist/packem_shared/runtime-check-Cobi3p6l.js +127 -0
  80. package/dist/packem_shared/selectors-SM69TfqC.js +194 -0
  81. package/dist/packem_shared/symbols-Ta7g2nU-.js +14 -0
  82. package/dist/packem_shared/toolchain-BdZd9eBi.js +975 -0
  83. package/dist/packem_shared/typosquats-C-bCh3PX.js +1210 -0
  84. package/dist/packem_shared/use-measured-height-CNP0vT4M.js +20 -0
  85. package/dist/packem_shared/utils-CthVdBPS.js +40 -0
  86. package/dist/packem_shared/xxh3-Ck8mXNg1.js +239 -0
  87. package/index.js +773 -0
  88. package/package.json +82 -21
  89. package/schemas/project.schema.json +420 -0
  90. package/schemas/vis-config.schema.json +501 -0
  91. package/skills/vis/SKILL.md +96 -0
  92. package/templates/buildkite-ci/.buildkite/pipeline.yml.tera +85 -0
  93. package/templates/buildkite-ci/template.yml +20 -0
  94. package/dist/ai-analysis.d.ts +0 -40
  95. package/dist/ai-cache.d.ts +0 -21
  96. package/dist/bin.d.ts +0 -1
  97. package/dist/catalog.d.ts +0 -110
  98. package/dist/commands/affected.d.ts +0 -3
  99. package/dist/commands/ai.d.ts +0 -3
  100. package/dist/commands/analyze.d.ts +0 -3
  101. package/dist/commands/check.d.ts +0 -3
  102. package/dist/commands/graph.d.ts +0 -3
  103. package/dist/commands/hook/constants.d.ts +0 -8
  104. package/dist/commands/hook/index.d.ts +0 -3
  105. package/dist/commands/hook/install.d.ts +0 -7
  106. package/dist/commands/hook/migrate.d.ts +0 -27
  107. package/dist/commands/hook/uninstall.d.ts +0 -3
  108. package/dist/commands/migrate/constants.d.ts +0 -12
  109. package/dist/commands/migrate/deps.d.ts +0 -32
  110. package/dist/commands/migrate/index.d.ts +0 -3
  111. package/dist/commands/migrate/json.d.ts +0 -20
  112. package/dist/commands/migrate/lint-staged.d.ts +0 -62
  113. package/dist/commands/migrate/types.d.ts +0 -20
  114. package/dist/commands/run.d.ts +0 -3
  115. package/dist/commands/staged.d.ts +0 -3
  116. package/dist/commands/update.d.ts +0 -3
  117. package/dist/config.d.ts +0 -40
  118. package/dist/config.js +0 -1
  119. package/dist/package-manager.d.ts +0 -23
  120. package/dist/workspace.d.ts +0 -58
@@ -0,0 +1,542 @@
1
+ import { createRequire as __cjs_createRequire } from "node:module";
2
+
3
+ const __cjs_require = __cjs_createRequire(import.meta.url);
4
+
5
+ const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
6
+
7
+ const __cjs_getBuiltinModule = (module) => {
8
+ // Check if we're in Node.js and version supports getBuiltinModule
9
+ if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
10
+ const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
11
+ // Node.js 20.16.0+ and 22.3.0+
12
+ if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
13
+ return __cjs_getProcess.getBuiltinModule(module);
14
+ }
15
+ }
16
+ // Fallback to createRequire
17
+ return __cjs_require(module);
18
+ };
19
+
20
+ const {
21
+ open,
22
+ stat,
23
+ watch
24
+ } = __cjs_getBuiltinModule("node:fs/promises");
25
+ import { isAccessible } from '@visulima/fs';
26
+ import { p as pail, Y as loadVisTaskConfigsForWorkspace, d as discoverWorkspace, Z as loadEnvFile } from './bin.js';
27
+ import { join } from '@visulima/path';
28
+ import { R as REGISTRY_FILE_MODE, w as withServiceLock, r as readEntry, i as isAlive, d as deleteEntry, g as getRegistryDir, s as slugify, a as writeEntry, b as runReadiness, p as pruneDead, S as ServiceReadinessError, c as readAllEntries } from '../packem_shared/registry-CbqXI0rc.js';
29
+ const {
30
+ spawn
31
+ } = __cjs_getBuiltinModule("node:child_process");
32
+ import { formatAge } from './handler14.js';
33
+
34
+ const isWindows = process.platform === "win32";
35
+ const spawnDetached = async (input) => {
36
+ const { command, cwd, env, logFile } = input;
37
+ const logHandle = await open(logFile, "a", REGISTRY_FILE_MODE);
38
+ let child;
39
+ try {
40
+ const shell = isWindows ? "cmd" : "/bin/sh";
41
+ const args = isWindows ? ["/d", "/s", "/c", command] : ["-c", command];
42
+ child = spawn(shell, args, {
43
+ cwd,
44
+ detached: true,
45
+ env: { ...process.env, ...env },
46
+ stdio: ["ignore", logHandle.fd, logHandle.fd],
47
+ // Windows: spawn in a new console so the child isn't tied
48
+ // to this terminal's lifetime.
49
+ windowsHide: true
50
+ });
51
+ } finally {
52
+ await logHandle.close().catch(() => {
53
+ });
54
+ }
55
+ if (child.pid === void 0) {
56
+ await new Promise((resolve, reject) => {
57
+ child.once("spawn", () => resolve());
58
+ child.once("error", (error) => reject(error));
59
+ });
60
+ }
61
+ if (child.pid === void 0) {
62
+ throw new Error(`Failed to spawn detached process for command: ${command}`);
63
+ }
64
+ child.unref();
65
+ return { pid: child.pid };
66
+ };
67
+
68
+ const DEFAULT_KILL_GRACE_MS = 5e3;
69
+ const startService = async (input) => withServiceLock(input.workspaceRoot, input.id, async () => {
70
+ const existing = await readEntry(input.workspaceRoot, input.id);
71
+ if (existing && isAlive(existing.pid)) {
72
+ throw new Error(`Service ${input.id} is already running (pid ${String(existing.pid)})`);
73
+ }
74
+ if (existing) {
75
+ await deleteEntry(input.workspaceRoot, input.id, existing);
76
+ }
77
+ const registryDirectory = await getRegistryDir(input.workspaceRoot);
78
+ const slug = slugify(input.id);
79
+ const logFile = join(registryDirectory, `${slug}.log`);
80
+ const { pid } = await spawnDetached({
81
+ command: input.command,
82
+ cwd: input.cwd,
83
+ env: input.env,
84
+ logFile
85
+ });
86
+ const entry = {
87
+ command: input.command,
88
+ config: input.config,
89
+ cwd: input.cwd,
90
+ env: input.config.env ?? {},
91
+ id: input.id,
92
+ logFile,
93
+ pid,
94
+ slug,
95
+ startedAt: (/* @__PURE__ */ new Date()).toISOString(),
96
+ visVersion: process.env["VIS_VERSION"] ?? "0.0.0"
97
+ };
98
+ await writeEntry(input.workspaceRoot, entry);
99
+ if (input.skipReadiness !== true) {
100
+ try {
101
+ await runReadiness(input.config, { timeoutMs: input.readinessTimeoutMs });
102
+ } catch (error) {
103
+ await stopServiceByPid(pid, input.config.killGracePeriodMs ?? DEFAULT_KILL_GRACE_MS).catch(() => {
104
+ });
105
+ await deleteEntry(input.workspaceRoot, input.id, entry).catch(() => {
106
+ });
107
+ throw error;
108
+ }
109
+ }
110
+ return { entry };
111
+ });
112
+ const stopService = async (input) => withServiceLock(input.workspaceRoot, input.id, async () => {
113
+ const entry = await readEntry(input.workspaceRoot, input.id);
114
+ if (!entry) {
115
+ return { stopped: false };
116
+ }
117
+ if (!isAlive(entry.pid)) {
118
+ await deleteEntry(input.workspaceRoot, input.id, entry);
119
+ return { stopped: false };
120
+ }
121
+ const graceMs = input.graceMs ?? entry.config.killGracePeriodMs ?? DEFAULT_KILL_GRACE_MS;
122
+ await stopServiceByPid(entry.pid, graceMs);
123
+ await deleteEntry(input.workspaceRoot, input.id, entry);
124
+ return { stopped: true };
125
+ });
126
+ const stopServiceByPid = async (pid, graceMs) => {
127
+ sendSignalToServiceGroup(pid, "SIGTERM");
128
+ const start = Date.now();
129
+ while (Date.now() - start < graceMs) {
130
+ if (!isAlive(pid)) {
131
+ return;
132
+ }
133
+ await new Promise((resolve) => {
134
+ setTimeout(resolve, 100);
135
+ });
136
+ }
137
+ if (isAlive(pid)) {
138
+ sendSignalToServiceGroup(pid, "SIGKILL");
139
+ }
140
+ };
141
+ const sendSignalToServiceGroup = (pid, signal) => {
142
+ try {
143
+ if (process.platform === "win32") {
144
+ process.kill(pid, signal);
145
+ } else {
146
+ process.kill(-pid, signal);
147
+ }
148
+ } catch (error) {
149
+ if (error.code === "ESRCH") {
150
+ return;
151
+ }
152
+ throw error;
153
+ }
154
+ };
155
+
156
+ const splitTargetId = (raw) => {
157
+ const id = raw.trim();
158
+ const idx = id.lastIndexOf(":");
159
+ if (idx <= 0 || idx === id.length - 1) {
160
+ return void 0;
161
+ }
162
+ return { project: id.slice(0, idx), target: id.slice(idx + 1) };
163
+ };
164
+ const resolveCwd = (workspaceRoot, projectRoot, runFromWorkspaceRoot) => {
165
+ if (runFromWorkspaceRoot || !projectRoot) {
166
+ return workspaceRoot;
167
+ }
168
+ return projectRoot.startsWith("/") ? projectRoot : `${workspaceRoot}/${projectRoot}`;
169
+ };
170
+ const resolveTarget = async (workspaceRoot, visConfig, targetId) => {
171
+ const split = splitTargetId(targetId);
172
+ if (!split) {
173
+ pail.error(`Invalid target id "${targetId}". Expected "<project>:<target>", e.g. "@my/api:db".`);
174
+ return void 0;
175
+ }
176
+ const taskConfigs = await loadVisTaskConfigsForWorkspace(workspaceRoot);
177
+ const { projectOptions, workspace } = discoverWorkspace(workspaceRoot, visConfig, taskConfigs);
178
+ const project = workspace.projects[split.project];
179
+ const target = projectOptions.get(split.project)?.[split.target];
180
+ if (!project || !target) {
181
+ pail.error(`Target "${targetId}" not found in this workspace.`);
182
+ return void 0;
183
+ }
184
+ const service = target.options?.service;
185
+ if (!service) {
186
+ pail.error(`Target "${targetId}" is not a service. Add an \`options.service\` block to make it eligible for \`vis service\`.`);
187
+ return void 0;
188
+ }
189
+ if (!target.command) {
190
+ pail.error(`Target "${targetId}" has no command — services must be runnable.`);
191
+ return void 0;
192
+ }
193
+ const cwd = resolveCwd(workspaceRoot, project.root, target.options?.runFromWorkspaceRoot === true);
194
+ const envFromFiles = target.options?.envFile ? loadEnvFile(cwd, target.options.envFile) : {};
195
+ return {
196
+ command: target.command,
197
+ cwd,
198
+ env: { ...envFromFiles, ...service.env },
199
+ service,
200
+ target,
201
+ targetId
202
+ };
203
+ };
204
+ const requireWorkspace = (wsRoot) => {
205
+ if (!wsRoot) {
206
+ throw new Error("Could not determine workspace root. Run `vis service` inside a workspace.");
207
+ }
208
+ return wsRoot;
209
+ };
210
+ const serviceStartExecute = async ({ argument, options, visConfig, workspaceRoot: wsRoot }) => {
211
+ const workspaceRoot = requireWorkspace(wsRoot);
212
+ const targetId = argument[0]?.trim();
213
+ if (!targetId) {
214
+ pail.error("Missing target id. Usage: vis service start <project>:<target>");
215
+ process.exitCode = 1;
216
+ return;
217
+ }
218
+ const resolved = await resolveTarget(workspaceRoot, visConfig, targetId);
219
+ if (!resolved) {
220
+ process.exitCode = 1;
221
+ return;
222
+ }
223
+ try {
224
+ const { entry } = await startService({
225
+ command: resolved.command,
226
+ config: resolved.service,
227
+ cwd: resolved.cwd,
228
+ env: resolved.env,
229
+ id: targetId,
230
+ readinessTimeoutMs: options.timeout,
231
+ skipReadiness: options.noReadiness === true,
232
+ workspaceRoot
233
+ });
234
+ pail.success(`Started ${targetId} (pid ${String(entry.pid)})`);
235
+ pail.info(` log: ${entry.logFile}`);
236
+ } catch (error) {
237
+ const message = error instanceof Error ? error.message : String(error);
238
+ if (error instanceof ServiceReadinessError) {
239
+ pail.error(`Readiness probe failed for ${targetId}: ${message}`);
240
+ } else {
241
+ pail.error(`Failed to start ${targetId}: ${message}`);
242
+ }
243
+ process.exitCode = 1;
244
+ }
245
+ };
246
+ const stopOne = async (workspaceRoot, id, graceMs) => {
247
+ const { stopped } = await stopService({ graceMs, id, workspaceRoot });
248
+ if (stopped) {
249
+ pail.success(`Stopped ${id}`);
250
+ return true;
251
+ }
252
+ pail.info(`No running service registered for ${id}`);
253
+ return false;
254
+ };
255
+ const serviceStopExecute = async ({ argument, options, workspaceRoot: wsRoot }) => {
256
+ const workspaceRoot = requireWorkspace(wsRoot);
257
+ const { graceMs } = options;
258
+ const targetId = argument[0]?.trim();
259
+ if (options.all === true) {
260
+ if (targetId) {
261
+ pail.error("Cannot combine --all with a target id. Use one or the other.");
262
+ process.exitCode = 1;
263
+ return;
264
+ }
265
+ const entries = await readAllEntries(workspaceRoot);
266
+ if (entries.length === 0) {
267
+ pail.info("No running services registered for this workspace.");
268
+ return;
269
+ }
270
+ for (const entry of entries) {
271
+ await stopOne(workspaceRoot, entry.id, graceMs);
272
+ }
273
+ return;
274
+ }
275
+ if (!targetId) {
276
+ pail.error("Missing target id. Usage: vis service stop <project>:<target> | --all");
277
+ process.exitCode = 1;
278
+ return;
279
+ }
280
+ const stopped = await stopOne(workspaceRoot, targetId, graceMs);
281
+ if (!stopped) {
282
+ process.exitCode = 1;
283
+ }
284
+ };
285
+ const formatPort = (entry) => {
286
+ const port = entry.config.readiness?.tcp.port ?? entry.config.port;
287
+ return port === void 0 ? "—" : String(port);
288
+ };
289
+ const VALID_LIST_FORMATS = /* @__PURE__ */ new Set(["json", "table"]);
290
+ const serviceListExecute = async ({ logger, options, workspaceRoot: wsRoot }) => {
291
+ const workspaceRoot = requireWorkspace(wsRoot);
292
+ const format = options.format ?? "table";
293
+ if (!VALID_LIST_FORMATS.has(format)) {
294
+ pail.error(`Invalid --format "${format}". Expected one of: ${[...VALID_LIST_FORMATS].sort().join(", ")}.`);
295
+ process.exitCode = 1;
296
+ return;
297
+ }
298
+ const { surviving: entries } = await pruneDead(workspaceRoot);
299
+ if (format === "json") {
300
+ const now = Date.now();
301
+ process.stdout.write(
302
+ `${JSON.stringify(
303
+ entries.map((entry) => {
304
+ const startedMs = Date.parse(entry.startedAt);
305
+ return {
306
+ ageMs: Number.isFinite(startedMs) ? now - startedMs : null,
307
+ alive: isAlive(entry.pid),
308
+ command: entry.command,
309
+ cwd: entry.cwd,
310
+ env: entry.env,
311
+ id: entry.id,
312
+ logFile: entry.logFile,
313
+ pid: entry.pid,
314
+ port: entry.config.readiness?.tcp.port ?? entry.config.port ?? null,
315
+ startedAt: entry.startedAt,
316
+ visVersion: entry.visVersion
317
+ };
318
+ }),
319
+ void 0,
320
+ 2
321
+ )}
322
+ `
323
+ );
324
+ return;
325
+ }
326
+ if (entries.length === 0) {
327
+ pail.info("No running services registered for this workspace.");
328
+ return;
329
+ }
330
+ const renderedAt = Date.now();
331
+ const rows = entries.map((entry) => {
332
+ const startedMs = Date.parse(entry.startedAt);
333
+ return {
334
+ age: Number.isFinite(startedMs) ? formatAge(startedMs, renderedAt) : "?",
335
+ id: entry.id,
336
+ log: entry.logFile,
337
+ pid: String(entry.pid),
338
+ port: formatPort(entry)
339
+ };
340
+ });
341
+ const idWidth = Math.max(2, ...rows.map((r) => r.id.length));
342
+ const pidWidth = Math.max(3, ...rows.map((r) => r.pid.length));
343
+ const portWidth = Math.max(4, ...rows.map((r) => r.port.length));
344
+ const ageWidth = Math.max(3, ...rows.map((r) => r.age.length));
345
+ logger.info(` ${"id".padEnd(idWidth)} ${"pid".padEnd(pidWidth)} ${"port".padEnd(portWidth)} ${"age".padEnd(ageWidth)} log`);
346
+ logger.info(` ${"-".repeat(idWidth)} ${"-".repeat(pidWidth)} ${"-".repeat(portWidth)} ${"-".repeat(ageWidth)} ---`);
347
+ for (const row of rows) {
348
+ logger.info(` ${row.id.padEnd(idWidth)} ${row.pid.padEnd(pidWidth)} ${row.port.padEnd(portWidth)} ${row.age.padEnd(ageWidth)} ${row.log}`);
349
+ }
350
+ };
351
+ const serviceStatusExecute = async ({ argument, options, workspaceRoot: wsRoot }) => {
352
+ const workspaceRoot = requireWorkspace(wsRoot);
353
+ const targetId = argument[0]?.trim();
354
+ if (!targetId) {
355
+ pail.error("Missing target id. Usage: vis service status <project>:<target>");
356
+ process.exitCode = 1;
357
+ return;
358
+ }
359
+ const entry = await readEntry(workspaceRoot, targetId);
360
+ if (!entry) {
361
+ pail.error(`No service registered for ${targetId}.`);
362
+ process.exitCode = 1;
363
+ return;
364
+ }
365
+ if (!isAlive(entry.pid)) {
366
+ pail.error(`Service ${targetId} is not running (pid ${String(entry.pid)} is dead). Run \`vis service start ${targetId}\` to recover.`);
367
+ process.exitCode = 1;
368
+ return;
369
+ }
370
+ try {
371
+ await runReadiness(entry.config, { timeoutMs: options.timeout });
372
+ pail.success(`${targetId} healthy (pid ${String(entry.pid)})`);
373
+ } catch (error) {
374
+ const message = error instanceof Error ? error.message : String(error);
375
+ pail.error(`${targetId} probe failed: ${message}`);
376
+ process.exitCode = 1;
377
+ }
378
+ };
379
+ const serviceRestartExecute = async ({
380
+ argument,
381
+ options,
382
+ visConfig,
383
+ workspaceRoot: wsRoot
384
+ }) => {
385
+ const workspaceRoot = requireWorkspace(wsRoot);
386
+ const targetId = argument[0]?.trim();
387
+ if (!targetId) {
388
+ pail.error("Missing target id. Usage: vis service restart <project>:<target>");
389
+ process.exitCode = 1;
390
+ return;
391
+ }
392
+ await stopService({ graceMs: options.graceMs, id: targetId, workspaceRoot });
393
+ const resolved = await resolveTarget(workspaceRoot, visConfig, targetId);
394
+ if (!resolved) {
395
+ process.exitCode = 1;
396
+ return;
397
+ }
398
+ try {
399
+ const { entry } = await startService({
400
+ command: resolved.command,
401
+ config: resolved.service,
402
+ cwd: resolved.cwd,
403
+ env: resolved.env,
404
+ id: targetId,
405
+ readinessTimeoutMs: options.timeout,
406
+ skipReadiness: options.noReadiness === true,
407
+ workspaceRoot
408
+ });
409
+ pail.success(`Restarted ${targetId} (pid ${String(entry.pid)})`);
410
+ } catch (error) {
411
+ const message = error instanceof Error ? error.message : String(error);
412
+ pail.error(`Failed to restart ${targetId}: ${message}`);
413
+ process.exitCode = 1;
414
+ }
415
+ };
416
+ const TAIL_POLL_MS = 200;
417
+ const TAIL_MAX_CHUNK_BYTES = 1024 * 1024;
418
+ const tailLog = async (logFile) => {
419
+ let position = 0;
420
+ try {
421
+ const initial = await stat(logFile);
422
+ position = initial.size;
423
+ } catch {
424
+ position = 0;
425
+ }
426
+ const controller = new AbortController();
427
+ let sigintReceived = false;
428
+ const onSigint = () => {
429
+ sigintReceived = true;
430
+ controller.abort();
431
+ };
432
+ const onSigterm = () => {
433
+ controller.abort();
434
+ };
435
+ process.on("SIGINT", onSigint);
436
+ process.on("SIGTERM", onSigterm);
437
+ let tickInFlight = false;
438
+ const tickOnce = async () => {
439
+ if (tickInFlight) {
440
+ return;
441
+ }
442
+ tickInFlight = true;
443
+ try {
444
+ const stats = await stat(logFile).catch(() => void 0);
445
+ if (!stats) {
446
+ return;
447
+ }
448
+ if (stats.size < position) {
449
+ position = 0;
450
+ }
451
+ if (stats.size === position) {
452
+ return;
453
+ }
454
+ const available = stats.size - position;
455
+ const chunkSize = Math.min(available, TAIL_MAX_CHUNK_BYTES);
456
+ const handle = await open(logFile, "r");
457
+ try {
458
+ const buffer = Buffer.alloc(chunkSize);
459
+ await handle.read(buffer, 0, chunkSize, position);
460
+ process.stdout.write(buffer);
461
+ position += chunkSize;
462
+ } finally {
463
+ await handle.close().catch(() => {
464
+ });
465
+ }
466
+ } finally {
467
+ tickInFlight = false;
468
+ }
469
+ };
470
+ try {
471
+ const watcher = (async () => {
472
+ try {
473
+ for await (const _event of watch(logFile, { signal: controller.signal })) {
474
+ await tickOnce();
475
+ }
476
+ } catch {
477
+ }
478
+ })();
479
+ while (!controller.signal.aborted) {
480
+ await tickOnce();
481
+ await new Promise((resolve) => {
482
+ const timer = setTimeout(resolve, TAIL_POLL_MS);
483
+ controller.signal.addEventListener(
484
+ "abort",
485
+ () => {
486
+ clearTimeout(timer);
487
+ resolve();
488
+ },
489
+ { once: true }
490
+ );
491
+ });
492
+ }
493
+ await watcher.catch(() => {
494
+ });
495
+ await tickOnce().catch(() => {
496
+ });
497
+ } finally {
498
+ process.off("SIGINT", onSigint);
499
+ process.off("SIGTERM", onSigterm);
500
+ }
501
+ if (sigintReceived) {
502
+ process.exitCode = 130;
503
+ }
504
+ };
505
+ const serviceLogsExecute = async ({ argument, options, workspaceRoot: wsRoot }) => {
506
+ const workspaceRoot = requireWorkspace(wsRoot);
507
+ const targetId = argument[0]?.trim();
508
+ if (!targetId) {
509
+ pail.error("Missing target id. Usage: vis service logs <project>:<target>");
510
+ process.exitCode = 1;
511
+ return;
512
+ }
513
+ const entry = await readEntry(workspaceRoot, targetId);
514
+ if (!entry) {
515
+ pail.error(`No service registered for ${targetId}.`);
516
+ process.exitCode = 1;
517
+ return;
518
+ }
519
+ if (!await isAccessible(entry.logFile)) {
520
+ pail.warn(`Log file is missing for ${targetId}: ${entry.logFile}`);
521
+ process.exitCode = 1;
522
+ return;
523
+ }
524
+ if (options.follow === true) {
525
+ await tailLog(entry.logFile);
526
+ return;
527
+ }
528
+ const handle = await open(entry.logFile, "r");
529
+ try {
530
+ const stream = handle.createReadStream();
531
+ await new Promise((resolve, reject) => {
532
+ stream.on("end", resolve);
533
+ stream.on("error", reject);
534
+ stream.pipe(process.stdout, { end: false });
535
+ });
536
+ } finally {
537
+ await handle.close().catch(() => {
538
+ });
539
+ }
540
+ };
541
+
542
+ export { serviceListExecute, serviceLogsExecute, serviceRestartExecute, serviceStartExecute, serviceStatusExecute, serviceStopExecute };