@venturewild/workspace 0.1.1 → 0.1.3
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.
- package/LICENSE +21 -21
- package/README.md +112 -73
- package/package.json +75 -75
- package/server/bin/wild-workspace.mjs +725 -402
- package/server/src/agent-readiness.mjs +200 -0
- package/server/src/agent.mjs +356 -356
- package/server/src/config.mjs +302 -272
- package/server/src/daemon-bin.mjs +6 -2
- package/server/src/daemon-supervisor.mjs +216 -216
- package/server/src/doctor.mjs +246 -0
- package/server/src/inbox.mjs +86 -86
- package/server/src/index.mjs +1330 -1099
- package/server/src/logpaths.mjs +97 -0
- package/server/src/observability.mjs +45 -0
- package/server/src/operator.mjs +65 -0
- package/server/src/service.mjs +297 -0
- package/server/src/session-reporter.mjs +201 -0
- package/server/src/supervisor.mjs +217 -0
- package/server/src/sync.mjs +248 -248
- package/server/src/transcript.mjs +121 -0
- package/web/dist/assets/index-Bj-mdLGj.css +1 -0
- package/web/dist/assets/index-DLRgyr9j.js +89 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-B2EifA0K.js +0 -89
- package/web/dist/assets/index-CsFUQhvj.css +0 -1
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// `wild-workspace doctor` — one pre/post-flight diagnostic for a real user's
|
|
2
|
+
// machine. The riskiest moment for a brand-new (non-technical) user is the
|
|
3
|
+
// install itself: no Claude yet, wrong Node, a busy port, a daemon binary that
|
|
4
|
+
// didn't resolve, an unclaimed slug. When something breaks we need to SEE it —
|
|
5
|
+
// ideally fix it — without making them feel stupid (docs/user-experience.md §5).
|
|
6
|
+
//
|
|
7
|
+
// runDoctor() returns a structured report (every check is { id, label, status,
|
|
8
|
+
// detail, hint }); the CLI renders it with ✅/⚠️/❌ and the operator channel
|
|
9
|
+
// serves the same JSON. Every external touch-point (agent detect, auth probe,
|
|
10
|
+
// daemon resolve, port check, account load, service status, registry fetch) is
|
|
11
|
+
// an injected seam so the test suite never spawns a process or hits the network.
|
|
12
|
+
|
|
13
|
+
import os from 'node:os';
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
|
|
17
|
+
import { buildConfig, APP_VERSION } from './config.mjs';
|
|
18
|
+
import { detectAgents, pickDefaultAgent } from './agent.mjs';
|
|
19
|
+
import { probeAgentReadiness } from './agent-readiness.mjs';
|
|
20
|
+
import { resolveDaemonBinary } from './daemon-bin.mjs';
|
|
21
|
+
import { checkPort } from './preview.mjs';
|
|
22
|
+
import { loadAccount } from './account.mjs';
|
|
23
|
+
import { serviceStatus } from './service.mjs';
|
|
24
|
+
import { probeHealth } from './supervisor.mjs';
|
|
25
|
+
import { listLogs, diagnosticsDir } from './logpaths.mjs';
|
|
26
|
+
|
|
27
|
+
const STATUS_ICON = { ok: '✅', warn: '⚠️', fail: '❌', info: 'ℹ️' };
|
|
28
|
+
|
|
29
|
+
// Native installer is Claude's canonical path today (npm i -g still works as a
|
|
30
|
+
// fallback). Shown verbatim to the user, so keep it copy-pasteable.
|
|
31
|
+
const CLAUDE_INSTALL_HINT =
|
|
32
|
+
'Install Claude Code: curl -fsSL https://claude.ai/install.sh | bash (Windows: irm https://claude.ai/install.ps1 | iex)';
|
|
33
|
+
|
|
34
|
+
function nodeMajor(version = process.version) {
|
|
35
|
+
const m = /^v?(\d+)/.exec(String(version));
|
|
36
|
+
return m ? Number(m[1]) : 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Reach the bmo-sync registry: resolve the user's slug if linked, else /health.
|
|
40
|
+
async function probeRegistry(config, fetchImpl) {
|
|
41
|
+
const base = String(config.bmoSyncServerUrl || '').replace(/\/$/, '');
|
|
42
|
+
const slug = config.account?.slug || null;
|
|
43
|
+
const url = slug
|
|
44
|
+
? `${base}/api/slug/resolve/${encodeURIComponent(slug)}`
|
|
45
|
+
: `${base}/api/health`;
|
|
46
|
+
const ctrl = new AbortController();
|
|
47
|
+
const timer = setTimeout(() => ctrl.abort(), 5000);
|
|
48
|
+
try {
|
|
49
|
+
const res = await fetchImpl(url, { signal: ctrl.signal });
|
|
50
|
+
return { reachable: true, status: res.status, slug, url };
|
|
51
|
+
} catch (e) {
|
|
52
|
+
return { reachable: false, error: String(e?.message || e), slug, url };
|
|
53
|
+
} finally {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Run every diagnostic check. All deps are injectable for testing.
|
|
60
|
+
* @returns {{version,generatedAt,platform,summary,checks,logs}}
|
|
61
|
+
*/
|
|
62
|
+
export async function runDoctor(opts = {}, deps = {}) {
|
|
63
|
+
const config = opts.config || deps.config || buildConfig({});
|
|
64
|
+
const env = deps.env || process.env;
|
|
65
|
+
const d = {
|
|
66
|
+
detectAgents: deps.detectAgents || detectAgents,
|
|
67
|
+
probeReadiness: deps.probeAgentReadiness || probeAgentReadiness,
|
|
68
|
+
resolveDaemon: deps.resolveDaemonBinary || resolveDaemonBinary,
|
|
69
|
+
checkPort: deps.checkPort || checkPort,
|
|
70
|
+
loadAccount: deps.loadAccount || loadAccount,
|
|
71
|
+
serviceStatus: deps.serviceStatus || serviceStatus,
|
|
72
|
+
listLogs: deps.listLogs || listLogs,
|
|
73
|
+
fetchImpl: deps.fetchImpl || ((...a) => globalThis.fetch(...a)),
|
|
74
|
+
};
|
|
75
|
+
const checks = [];
|
|
76
|
+
const add = (c) => checks.push(c);
|
|
77
|
+
// Run a check body, capturing a thrown error as a non-fatal 'warn' so one bad
|
|
78
|
+
// check never aborts the whole report.
|
|
79
|
+
const guarded = async (id, label, body) => {
|
|
80
|
+
try {
|
|
81
|
+
add({ id, label, ...(await body()) });
|
|
82
|
+
} catch (e) {
|
|
83
|
+
add({ id, label, status: 'warn', detail: `check errored: ${String(e?.message || e)}`, hint: null });
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// 1. Node runtime
|
|
88
|
+
await guarded('node', 'Node.js runtime', async () => {
|
|
89
|
+
const major = nodeMajor();
|
|
90
|
+
const detail = `${process.version} on ${os.platform()}-${os.arch()}`;
|
|
91
|
+
return major >= 18
|
|
92
|
+
? { status: 'ok', detail, hint: null }
|
|
93
|
+
: { status: 'fail', detail, hint: 'Claude Code + wild-workspace need Node 18 or newer. Update Node, then retry.' };
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// 2. Claude installed?
|
|
97
|
+
let claude = null;
|
|
98
|
+
await guarded('agent', 'Claude Code installed', async () => {
|
|
99
|
+
const agents = await d.detectAgents();
|
|
100
|
+
claude = (agents || []).find((a) => a.id === 'claude' && a.available) || null;
|
|
101
|
+
if (!claude) {
|
|
102
|
+
const fallback = pickDefaultAgent(agents || []);
|
|
103
|
+
if (fallback?.available) {
|
|
104
|
+
return { status: 'info', detail: `Claude not found; using ${fallback.label}.`, hint: null };
|
|
105
|
+
}
|
|
106
|
+
return { status: 'fail', detail: 'no `claude` on PATH', hint: CLAUDE_INSTALL_HINT };
|
|
107
|
+
}
|
|
108
|
+
return { status: 'ok', detail: claude.resolvedPath || claude.binary, hint: null };
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// 3. Claude signed in AND able to run turns?
|
|
112
|
+
if (claude) {
|
|
113
|
+
await guarded('agentAuth', 'Claude ready to think', async () => {
|
|
114
|
+
const v = await d.probeReadiness(claude, undefined, env);
|
|
115
|
+
switch (v.status) {
|
|
116
|
+
case 'ready':
|
|
117
|
+
return { status: 'ok', detail: v.email ? `signed in as ${v.email}` : 'signed in', hint: null };
|
|
118
|
+
case 'subscribe':
|
|
119
|
+
return {
|
|
120
|
+
status: 'warn',
|
|
121
|
+
detail: v.email ? `signed in as ${v.email}, no active plan` : 'signed in, no active plan',
|
|
122
|
+
hint: 'Claude Code needs a Claude Pro plan (or higher). Subscribe at claude.ai, then retry.',
|
|
123
|
+
};
|
|
124
|
+
case 'login':
|
|
125
|
+
return { status: 'fail', detail: 'not signed in', hint: 'Run `claude auth login`, sign in, then retry.' };
|
|
126
|
+
case 'missing':
|
|
127
|
+
return { status: 'fail', detail: 'Claude not installed', hint: CLAUDE_INSTALL_HINT };
|
|
128
|
+
default:
|
|
129
|
+
return { status: 'info', detail: `readiness unknown (${v.status})`, hint: 'Will be confirmed on the first agent turn.' };
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 4. bmo-sync daemon binary resolvable?
|
|
135
|
+
await guarded('daemonBinary', 'Sync daemon binary', async () => {
|
|
136
|
+
const r = d.resolveDaemon({ env });
|
|
137
|
+
if (!r) {
|
|
138
|
+
return { status: 'fail', detail: 'WILD_WORKSPACE_DAEMON_BIN is set but the file is missing', hint: 'Unset it or point it at a real binary.' };
|
|
139
|
+
}
|
|
140
|
+
if (r.source === 'path') {
|
|
141
|
+
return {
|
|
142
|
+
status: 'warn',
|
|
143
|
+
detail: 'no bundled daemon found — relying on PATH; cross-device sync may be off',
|
|
144
|
+
hint: 'Reinstall: npm i -g @venturewild/workspace (pulls the daemon for your platform).',
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return { status: 'ok', detail: `${r.path} (${r.source})`, hint: null };
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// 5. Workspace port
|
|
151
|
+
await guarded('port', `Workspace port :${config.port}`, async () => {
|
|
152
|
+
const inUse = await d.checkPort(config.port);
|
|
153
|
+
return inUse
|
|
154
|
+
? { status: 'info', detail: 'in use — your workspace is likely already running (or another app holds it)', hint: null }
|
|
155
|
+
: { status: 'ok', detail: 'free', hint: null };
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// 6. Account linked (slug)
|
|
159
|
+
let account = null;
|
|
160
|
+
await guarded('account', 'Workspace account linked', async () => {
|
|
161
|
+
account = d.loadAccount(config.dataDir);
|
|
162
|
+
if (account?.slug) {
|
|
163
|
+
return { status: 'ok', detail: `${account.slug} (${account.email || 'no email'}) → https://${account.slug}.venturewild.llc`, hint: null };
|
|
164
|
+
}
|
|
165
|
+
return { status: 'warn', detail: 'not linked yet', hint: 'Run `wild-workspace login <blob>` with the code from workspace.venturewild.llc.' };
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// 7. Registry reachable + slug status
|
|
169
|
+
await guarded('registry', 'Sync server reachable', async () => {
|
|
170
|
+
const r = await probeRegistry({ ...config, account: account || config.account }, d.fetchImpl);
|
|
171
|
+
if (!r.reachable) {
|
|
172
|
+
return { status: 'fail', detail: `can't reach ${r.url}: ${r.error}`, hint: 'Check the internet connection, then retry.' };
|
|
173
|
+
}
|
|
174
|
+
if (r.slug) {
|
|
175
|
+
if (r.status === 200) return { status: 'ok', detail: `slug "${r.slug}" is claimed`, hint: null };
|
|
176
|
+
if (r.status === 404) return { status: 'warn', detail: `slug "${r.slug}" is not claimed on the server`, hint: 'Re-run the claim, or `wild-workspace login` with a fresh blob.' };
|
|
177
|
+
return { status: 'warn', detail: `slug resolve returned HTTP ${r.status}`, hint: null };
|
|
178
|
+
}
|
|
179
|
+
return r.status < 500
|
|
180
|
+
? { status: 'ok', detail: `reachable (HTTP ${r.status})`, hint: null }
|
|
181
|
+
: { status: 'warn', detail: `server returned HTTP ${r.status}`, hint: null };
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// 8. Always-on / autostart
|
|
185
|
+
await guarded('service', 'Always-on (autostart)', async () => {
|
|
186
|
+
const s = await d.serviceStatus({ port: config.port }, { probeImpl: (p) => probeHealth(p) });
|
|
187
|
+
if (s.supported === false) {
|
|
188
|
+
return { status: 'info', detail: `not yet on ${s.platform} — run \`wild-workspace\` to start it`, hint: null };
|
|
189
|
+
}
|
|
190
|
+
const bits = [`installed=${s.installed ? 'yes' : 'no'}`, `supervisor=${s.supervisorAlive ? 'up' : 'down'}`, `server=${s.serverUp ? 'up' : 'down'}`];
|
|
191
|
+
return { status: s.installed ? 'ok' : 'info', detail: bits.join(' '), hint: s.installed ? null : 'Enable with `wild-workspace service install`.' };
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const logs = d.listLogs(env);
|
|
195
|
+
const summary = checks.reduce(
|
|
196
|
+
(acc, c) => ((acc[c.status] = (acc[c.status] || 0) + 1), acc),
|
|
197
|
+
{ ok: 0, warn: 0, fail: 0, info: 0 },
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
version: APP_VERSION,
|
|
202
|
+
generatedAt: new Date().toISOString(),
|
|
203
|
+
platform: `${os.platform()}-${os.arch()}`,
|
|
204
|
+
summary,
|
|
205
|
+
checks,
|
|
206
|
+
logs,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Render a report to a human string (used by the CLI). The operator channel
|
|
211
|
+
// sends the JSON instead.
|
|
212
|
+
export function renderDoctor(report) {
|
|
213
|
+
const lines = [];
|
|
214
|
+
lines.push(`wild-workspace doctor — v${report.version} (${report.platform})`);
|
|
215
|
+
lines.push('');
|
|
216
|
+
for (const c of report.checks) {
|
|
217
|
+
lines.push(`${STATUS_ICON[c.status] || '•'} ${c.label}: ${c.detail}`);
|
|
218
|
+
if (c.hint && (c.status === 'fail' || c.status === 'warn')) {
|
|
219
|
+
lines.push(` → ${c.hint}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
lines.push('');
|
|
223
|
+
const { ok, warn, fail } = report.summary;
|
|
224
|
+
lines.push(`Summary: ${ok} ok · ${warn} warning${warn === 1 ? '' : 's'} · ${fail} problem${fail === 1 ? '' : 's'}`);
|
|
225
|
+
lines.push('');
|
|
226
|
+
lines.push('Logs:');
|
|
227
|
+
for (const l of report.logs) {
|
|
228
|
+
lines.push(` ${l.exists ? '·' : ' '} ${l.name.padEnd(10)} ${l.file}${l.exists ? ` (${l.size} bytes)` : ' (none yet)'}`);
|
|
229
|
+
}
|
|
230
|
+
return lines.join('\n');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Persist the JSON report under ~/.wild-workspace/diagnostics/. Returns the
|
|
234
|
+
// file path (or null if it couldn't be written). Best-effort.
|
|
235
|
+
export function writeDoctorBundle(report, env = process.env) {
|
|
236
|
+
try {
|
|
237
|
+
const dir = diagnosticsDir(env);
|
|
238
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
239
|
+
const stamp = report.generatedAt.replace(/[:.]/g, '-');
|
|
240
|
+
const file = path.join(dir, `doctor-${stamp}.json`);
|
|
241
|
+
fs.writeFileSync(file, JSON.stringify(report, null, 2));
|
|
242
|
+
return file;
|
|
243
|
+
} catch {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}
|
package/server/src/inbox.mjs
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
// Component System inbox surfacing.
|
|
2
|
-
// When `.wild/inbox.md` exists or changes, emit notifications.
|
|
3
|
-
// Drives the "I noticed you imported X — want me to walk integration?" cue in chat.
|
|
4
|
-
|
|
5
|
-
import fs from 'node:fs/promises';
|
|
6
|
-
import { existsSync } from 'node:fs';
|
|
7
|
-
import path from 'node:path';
|
|
8
|
-
import chokidar from 'chokidar';
|
|
9
|
-
import { EventEmitter } from 'node:events';
|
|
10
|
-
|
|
11
|
-
export class InboxWatcher extends EventEmitter {
|
|
12
|
-
constructor(workspaceDir) {
|
|
13
|
-
super();
|
|
14
|
-
this.workspaceDir = workspaceDir;
|
|
15
|
-
this.inboxPath = path.join(workspaceDir, '.wild', 'inbox.md');
|
|
16
|
-
this.installedPath = path.join(workspaceDir, '.wild', 'installed.json');
|
|
17
|
-
this.watcher = null;
|
|
18
|
-
// Resolves once the watcher's initial scan is done — see start().
|
|
19
|
-
this.ready = Promise.resolve();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
start() {
|
|
23
|
-
this.watcher = chokidar.watch(
|
|
24
|
-
[this.inboxPath, this.installedPath, path.join(this.workspaceDir, '.wild', 'imports')],
|
|
25
|
-
{
|
|
26
|
-
ignoreInitial: false,
|
|
27
|
-
persistent: true,
|
|
28
|
-
depth: 3,
|
|
29
|
-
awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 100 },
|
|
30
|
-
},
|
|
31
|
-
);
|
|
32
|
-
this.watcher.on('add', () => this._refresh('add'));
|
|
33
|
-
this.watcher.on('change', () => this._refresh('change'));
|
|
34
|
-
this.watcher.on('unlink', () => this._refresh('unlink'));
|
|
35
|
-
// Resolves once chokidar's initial scan completes and it is actively
|
|
36
|
-
// watching — anything created after this point is reliably detected.
|
|
37
|
-
this.ready = new Promise((resolve) => this.watcher.once('ready', resolve));
|
|
38
|
-
return this;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async _refresh(kind) {
|
|
42
|
-
const snapshot = await this.snapshot();
|
|
43
|
-
this.emit('change', { kind, snapshot });
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async snapshot() {
|
|
47
|
-
const out = {
|
|
48
|
-
hasInbox: existsSync(this.inboxPath),
|
|
49
|
-
inboxContent: '',
|
|
50
|
-
installed: {},
|
|
51
|
-
imports: [],
|
|
52
|
-
};
|
|
53
|
-
if (out.hasInbox) {
|
|
54
|
-
try {
|
|
55
|
-
out.inboxContent = await fs.readFile(this.inboxPath, 'utf8');
|
|
56
|
-
} catch {
|
|
57
|
-
out.inboxContent = '';
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (existsSync(this.installedPath)) {
|
|
61
|
-
try {
|
|
62
|
-
const raw = await fs.readFile(this.installedPath, 'utf8');
|
|
63
|
-
out.installed = JSON.parse(raw);
|
|
64
|
-
} catch {
|
|
65
|
-
out.installed = {};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
const importsDir = path.join(this.workspaceDir, '.wild', 'imports');
|
|
69
|
-
if (existsSync(importsDir)) {
|
|
70
|
-
try {
|
|
71
|
-
const entries = await fs.readdir(importsDir, { withFileTypes: true });
|
|
72
|
-
out.imports = entries
|
|
73
|
-
.filter((e) => e.isDirectory())
|
|
74
|
-
.map((e) => e.name);
|
|
75
|
-
} catch {
|
|
76
|
-
out.imports = [];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return out;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
stop() {
|
|
83
|
-
if (this.watcher) this.watcher.close();
|
|
84
|
-
this.watcher = null;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
1
|
+
// Component System inbox surfacing.
|
|
2
|
+
// When `.wild/inbox.md` exists or changes, emit notifications.
|
|
3
|
+
// Drives the "I noticed you imported X — want me to walk integration?" cue in chat.
|
|
4
|
+
|
|
5
|
+
import fs from 'node:fs/promises';
|
|
6
|
+
import { existsSync } from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import chokidar from 'chokidar';
|
|
9
|
+
import { EventEmitter } from 'node:events';
|
|
10
|
+
|
|
11
|
+
export class InboxWatcher extends EventEmitter {
|
|
12
|
+
constructor(workspaceDir) {
|
|
13
|
+
super();
|
|
14
|
+
this.workspaceDir = workspaceDir;
|
|
15
|
+
this.inboxPath = path.join(workspaceDir, '.wild', 'inbox.md');
|
|
16
|
+
this.installedPath = path.join(workspaceDir, '.wild', 'installed.json');
|
|
17
|
+
this.watcher = null;
|
|
18
|
+
// Resolves once the watcher's initial scan is done — see start().
|
|
19
|
+
this.ready = Promise.resolve();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
start() {
|
|
23
|
+
this.watcher = chokidar.watch(
|
|
24
|
+
[this.inboxPath, this.installedPath, path.join(this.workspaceDir, '.wild', 'imports')],
|
|
25
|
+
{
|
|
26
|
+
ignoreInitial: false,
|
|
27
|
+
persistent: true,
|
|
28
|
+
depth: 3,
|
|
29
|
+
awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 100 },
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
this.watcher.on('add', () => this._refresh('add'));
|
|
33
|
+
this.watcher.on('change', () => this._refresh('change'));
|
|
34
|
+
this.watcher.on('unlink', () => this._refresh('unlink'));
|
|
35
|
+
// Resolves once chokidar's initial scan completes and it is actively
|
|
36
|
+
// watching — anything created after this point is reliably detected.
|
|
37
|
+
this.ready = new Promise((resolve) => this.watcher.once('ready', resolve));
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async _refresh(kind) {
|
|
42
|
+
const snapshot = await this.snapshot();
|
|
43
|
+
this.emit('change', { kind, snapshot });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async snapshot() {
|
|
47
|
+
const out = {
|
|
48
|
+
hasInbox: existsSync(this.inboxPath),
|
|
49
|
+
inboxContent: '',
|
|
50
|
+
installed: {},
|
|
51
|
+
imports: [],
|
|
52
|
+
};
|
|
53
|
+
if (out.hasInbox) {
|
|
54
|
+
try {
|
|
55
|
+
out.inboxContent = await fs.readFile(this.inboxPath, 'utf8');
|
|
56
|
+
} catch {
|
|
57
|
+
out.inboxContent = '';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (existsSync(this.installedPath)) {
|
|
61
|
+
try {
|
|
62
|
+
const raw = await fs.readFile(this.installedPath, 'utf8');
|
|
63
|
+
out.installed = JSON.parse(raw);
|
|
64
|
+
} catch {
|
|
65
|
+
out.installed = {};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const importsDir = path.join(this.workspaceDir, '.wild', 'imports');
|
|
69
|
+
if (existsSync(importsDir)) {
|
|
70
|
+
try {
|
|
71
|
+
const entries = await fs.readdir(importsDir, { withFileTypes: true });
|
|
72
|
+
out.imports = entries
|
|
73
|
+
.filter((e) => e.isDirectory())
|
|
74
|
+
.map((e) => e.name);
|
|
75
|
+
} catch {
|
|
76
|
+
out.imports = [];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
stop() {
|
|
83
|
+
if (this.watcher) this.watcher.close();
|
|
84
|
+
this.watcher = null;
|
|
85
|
+
}
|
|
86
|
+
}
|