-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdoctor.mjs
More file actions
612 lines (567 loc) · 20.4 KB
/
Copy pathdoctor.mjs
File metadata and controls
612 lines (567 loc) · 20.4 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// Copyright (c) Meta Platforms, Inc. and affiliates.
/**
* @file Health-check engine for `astryx doctor`.
*
* Runs a series of diagnostic checks against the user's project and
* environment, returning a structured report. Each check is a small,
* self-contained function that returns a {@link DoctorCheck} record, so
* adding a new diagnostic is just appending a function to {@link SYNC_CHECKS}.
*
* The engine is intentionally side-effect-free: it only *reads* the
* filesystem, environment, and package metadata. It never installs, writes,
* or mutates anything. That makes it safe to run in CI as a gate (exit 1 on
* any FAIL) and safe for AI agents to invoke with `--json`.
*
* Status semantics:
* - 'pass' — everything is healthy.
* - 'warn' — non-fatal; the setup works but could be improved.
* - 'fail' — something is broken and should be fixed (drives exit 1).
* - 'info' — purely informational; never affects exit code.
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import {MIN_NODE_VERSION, isNodeVersionSupported} from '../../foundation/env/node-version.mjs';
import {CLI_ROOT, findCoreDir} from '../../foundation/fs/paths.mjs';
import {detectPackageManager, getCliInvocation} from '../../foundation/env/package-manager.mjs';
import {findConfigPath, Project} from '../../foundation/config/project.mjs';
import {semverCompare, isValidSemver, satisfiesRange} from '../../foundation/env/semver.mjs';
/**
* @typedef {'pass'|'warn'|'fail'|'info'} DoctorStatus
*
* @typedef {object} DoctorCheck
* @property {string} id - Stable machine-readable id (e.g. 'node-version').
* @property {string} label - Human-readable check name.
* @property {DoctorStatus} status
* @property {string} message - One-line result summary.
* @property {string} [fix] - Actionable remediation, present when not 'pass'.
*
* @typedef {object} DoctorReport
* @property {DoctorCheck[]} checks
* @property {{pass: number, warn: number, fail: number, info: number}} summary
*
* @typedef {object} DoctorContext
* @property {string} cwd - Directory to diagnose.
* @property {string} nodeVersion - Running Node version.
* @property {string|null} coreDir - Resolved core package directory, or null.
* @property {string|null} configPath - Resolved astryx.config.mjs path, or null.
* @property {string|null} configTheme - theme value read from config, or null.
* @property {Error|null} [configError] - Error thrown while resolving the config
* path (e.g. multiple config files present), surfaced by checkConfig as a FAIL.
*/
/* ── helpers ──────────────────────────────────────────────────────────── */
/**
* Safely read + parse a package.json. Returns null on any failure.
* @param {string} pkgPath
* @returns {Record<string, any>|null}
*/
function readPkg(pkgPath) {
try {
return JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
} catch {
return null;
}
}
/**
* Read the version of an installed package from a resolved directory.
* @param {string|null} dir
* @returns {string|null}
*/
function pkgVersion(dir) {
if (!dir) return null;
const pkg = readPkg(path.join(dir, 'package.json'));
return pkg?.version ?? null;
}
/**
* Walk up from `startDir` to locate the nearest node_modules directory.
* @param {string} startDir
* @returns {string|null}
*/
function findNodeModules(startDir) {
let dir = startDir;
for (let i = 0; i < 6; i++) {
const candidate = path.join(dir, 'node_modules');
if (fs.existsSync(candidate)) return candidate;
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
return null;
}
/**
* Locate a package inside the project's own node_modules chain.
*
* Deliberately not `require.resolve`: Node folds NODE_PATH and the global
* folders into resolution even when `paths` is given, so a package merely
* reachable from the ambient environment would read as installed in the
* user's project. Reading package.json off disk also sidesteps packages that
* don't export it, so the version is always available to range-check.
*
* @param {string} startDir
* @param {string} name
* @returns {string|null} the package directory, or null when not installed
*/
function findInstalledPackage(startDir, name) {
let dir = startDir;
for (let i = 0; i < 6; i++) {
const candidate = path.join(dir, 'node_modules', ...name.split('/'));
if (fs.existsSync(path.join(candidate, 'package.json'))) return candidate;
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
return null;
}
/**
* Find every installed @astryxdesign/theme-* package under node_modules.
* @param {string} cwd
* @returns {Array<{name: string, version: string|null}>}
*/
function findThemePackages(cwd) {
const nm = findNodeModules(cwd);
/** @type {Array<{name: string, version: string|null}>} */
const found = [];
if (!nm) return found;
const scopeDir = path.join(nm, '@astryxdesign');
if (!fs.existsSync(scopeDir)) return found;
let entries;
try {
entries = fs.readdirSync(scopeDir, {withFileTypes: true});
} catch {
return found;
}
for (const entry of entries) {
if (!entry.name.startsWith('theme-')) continue;
const dir = path.join(scopeDir, entry.name);
// pnpm installs packages as symlinks into node_modules/.pnpm, and a
// symlink dirent reports isDirectory() as false — stat the target instead.
let isDir = entry.isDirectory();
if (!isDir && entry.isSymbolicLink()) {
try {
isDir = fs.statSync(dir).isDirectory();
} catch {
isDir = false;
}
}
if (!isDir) continue;
const name = `@astryxdesign/${entry.name}`;
found.push({name, version: pkgVersion(dir)});
}
return found;
}
/**
* Detect whether a theme appears to be wired up via the ASTRYX_THEME env var or
* an `xds.theme` field in the nearest package.json. Config-based wiring is
* handled by the caller (ctx.configTheme). This only inspects static signals.
* @param {string} cwd
* @returns {{wired: boolean, source: string|null}}
*/
function detectThemeWiring(cwd) {
if (process.env.ASTRYX_THEME) return {wired: true, source: 'ASTRYX_THEME env var'};
const nm = findNodeModules(cwd);
const projectDir = nm ? path.dirname(nm) : cwd;
const pkg = readPkg(path.join(projectDir, 'package.json'));
if (pkg?.astryx?.theme) return {wired: true, source: 'package.json astryx.theme'};
return {wired: false, source: null};
}
/* ── individual checks ────────────────────────────────────────────────── */
/**
* Check 1 — running Node version meets the CLI's minimum.
* @param {DoctorContext} ctx
* @returns {DoctorCheck}
*/
export function checkNodeVersion(ctx) {
const supported = isNodeVersionSupported(ctx.nodeVersion);
return {
id: 'node-version',
label: 'Node.js version',
status: supported ? 'pass' : 'fail',
message: supported
? `Node v${ctx.nodeVersion} meets the minimum (>=${MIN_NODE_VERSION}).`
: `Node v${ctx.nodeVersion} is below the required minimum (>=${MIN_NODE_VERSION}).`,
...(supported
? {}
: {fix: `Upgrade Node.js to >=${MIN_NODE_VERSION} and re-run.`}),
};
}
/**
* Check 2 — @astryxdesign/core is installed and resolvable from the project.
* @param {DoctorContext} ctx
* @returns {DoctorCheck}
*/
export function checkCoreInstalled(ctx) {
const found = Boolean(ctx.coreDir);
const version = pkgVersion(ctx.coreDir);
return {
id: 'core-installed',
label: '@astryxdesign/core installed',
status: found ? 'pass' : 'fail',
message: found
? `@astryxdesign/core resolved${version ? ` (v${version})` : ''}.`
: '@astryxdesign/core could not be resolved from this project.',
...(found
? {}
: {fix: 'Install the design system: `npm install @astryxdesign/core` (or yarn/pnpm/bun).'}),
};
}
/**
* Check 3 — installed @astryxdesign/core is in step with @astryxdesign/cli (major/minor drift).
* @param {DoctorContext} ctx
* @returns {DoctorCheck}
*/
export function checkVersionAlignment(ctx) {
const coreVersion = pkgVersion(ctx.coreDir);
const cliPkg = readPkg(path.join(CLI_ROOT, 'package.json'));
const cliVersion = cliPkg?.version ?? null;
if (!coreVersion || !cliVersion) {
return {
id: 'version-alignment',
label: '@astryxdesign/core <-> @astryxdesign/cli alignment',
status: 'info',
message: 'Skipped — could not read both @astryxdesign/core and @astryxdesign/cli versions.',
};
}
// A monorepo/linked install often pins a non-semver range like `workspace:*`
// or `link:...`. `'workspace:*'.split('.').map(Number)` yields NaN, and
// `NaN !== cliMajor` is always true — that produced a spurious drift WARN
// with a `NaN.undefined.x` fix string. If either version isn't real semver,
// there's nothing to compare: skip.
if (!isValidSemver(coreVersion) || !isValidSemver(cliVersion)) {
return {
id: 'version-alignment',
label: '@astryxdesign/core <-> @astryxdesign/cli alignment',
status: 'info',
message:
`Skipped — @astryxdesign/core v${coreVersion} / @astryxdesign/cli ` +
`v${cliVersion} are not both comparable semver.`,
};
}
const [coreMajor, coreMinor] = coreVersion.split('.').map(Number);
const [cliMajor, cliMinor] = cliVersion.split('.').map(Number);
const drift = coreMajor !== cliMajor || coreMinor !== cliMinor;
return {
id: 'version-alignment',
label: '@astryxdesign/core <-> @astryxdesign/cli alignment',
status: drift ? 'warn' : 'pass',
message: drift
? `@astryxdesign/core v${coreVersion} drifts from @astryxdesign/cli v${cliVersion} (major/minor mismatch).`
: `@astryxdesign/core v${coreVersion} is in step with @astryxdesign/cli v${cliVersion}.`,
...(drift
? {
fix:
semverCompare(cliVersion, coreVersion) > 0
? `Update @astryxdesign/core to ${cliMajor}.${cliMinor}.x to match the CLI.`
: `Update @astryxdesign/cli to ${coreMajor}.${coreMinor}.x to match @astryxdesign/core.`,
}
: {}),
};
}
/**
* Check 4 — at least one @astryxdesign/theme-* is installed and a theme is wired.
* @param {DoctorContext} ctx
* @returns {DoctorCheck}
*/
export function checkThemes(ctx) {
const themes = findThemePackages(ctx.cwd);
const wiring = detectThemeWiring(ctx.cwd);
const hasConfigTheme = Boolean(ctx.configTheme);
const wired = wiring.wired || hasConfigTheme;
if (themes.length === 0) {
return {
id: 'themes',
label: 'Theme packages',
status: 'warn',
message: 'No @astryxdesign/theme-* packages are installed.',
fix: 'Install a theme, e.g. `npm install @astryxdesign/theme-neutral`, then import its CSS or set astryx.theme.',
};
}
const names = themes.map(t => t.name).join(', ');
if (!wired) {
return {
id: 'themes',
label: 'Theme packages',
status: 'warn',
message: `Theme package(s) installed (${names}) but no theme appears wired.`,
fix: 'Wire a theme via the `astryx.theme` field in package.json, the ASTRYX_THEME env var, or your astryx.config.mjs.',
};
}
const source = hasConfigTheme ? 'astryx.config.mjs theme' : wiring.source;
return {
id: 'themes',
label: 'Theme packages',
status: 'pass',
message: `Theme package(s) installed (${names}); wired via ${source}.`,
};
}
/**
* Check 5 — astryx.config.mjs (if present) loads and has a valid shape.
* @param {DoctorContext} ctx
* @returns {Promise<DoctorCheck>}
*/
export async function checkConfig(ctx) {
// A resolution error (e.g. multiple astryx.config.* files) is exactly the
// kind of setup problem doctor should report — not crash on.
if (ctx.configError) {
return {
id: 'config',
label: 'astryx.config.mjs',
status: 'fail',
message: ctx.configError.message,
fix: 'Keep exactly one astryx.config.{ts,mjs,js} at your project root.',
};
}
if (!ctx.configPath) {
return {
id: 'config',
label: 'astryx.config.mjs',
status: 'info',
message: 'No astryx.config.mjs found — using defaults.',
};
}
// Project.load swallows nothing — it surfaces a genuine load failure — but
// the config check wants to report a bad default export precisely, so we
// re-import directly to surface a genuine load failure as a FAIL.
try {
const {pathToFileURL} = await import('node:url');
const mod = await import(pathToFileURL(ctx.configPath).href);
const config = mod.default;
if (config !== undefined && (typeof config !== 'object' || config === null)) {
return {
id: 'config',
label: 'astryx.config.mjs',
status: 'fail',
message: `astryx.config.mjs default export is not an object (got ${typeof config}).`,
fix: 'Export a default object from astryx.config.mjs, e.g. `export default { integrations: [] };`.',
};
}
return {
id: 'config',
label: 'astryx.config.mjs',
status: 'pass',
message: `astryx.config.mjs loaded cleanly (${path.relative(ctx.cwd, ctx.configPath) || ctx.configPath}).`,
};
} catch (err) {
return {
id: 'config',
label: 'astryx.config.mjs',
status: 'fail',
message: `astryx.config.mjs failed to load: ${/** @type {any} */ (err).message}`,
fix: 'Fix the syntax/runtime error in astryx.config.mjs so it imports cleanly.',
};
}
}
/**
* Check 6 — agent docs exist and contain the Astryx section markers.
* @param {DoctorContext} ctx
* @returns {DoctorCheck}
*/
export function checkAgentDocs(ctx) {
const candidates = [
'AGENTS.md',
'CLAUDE.md',
path.join('.claude', 'CLAUDE.md'),
'.cursorrules',
];
const present = candidates.filter(rel => fs.existsSync(path.join(ctx.cwd, rel)));
if (present.length === 0) {
return {
id: 'agent-docs',
label: 'AI agent docs',
status: 'info',
message: 'No agent docs (CLAUDE.md / AGENTS.md / .cursorrules) found.',
fix: `Generate agent docs with \`${getCliInvocation(ctx.cwd)} init --features agents\`.`,
};
}
const withMarkers = present.filter(rel => {
try {
const content = fs.readFileSync(path.join(ctx.cwd, rel), 'utf-8');
return (
(content.includes('<!-- ASTRYX:START -->') || content.includes('<!-- XDS:START -->')) &&
(content.includes('<!-- ASTRYX:END -->') || content.includes('<!-- XDS:END -->'))
);
} catch {
return false;
}
});
if (withMarkers.length === 0) {
return {
id: 'agent-docs',
label: 'AI agent docs',
status: 'warn',
message: `Agent docs present (${present.join(', ')}) but no Astryx section markers found.`,
fix: `Add the Astryx section to your agent docs with \`${getCliInvocation(ctx.cwd)} init --features agents\`.`,
};
}
return {
id: 'agent-docs',
label: 'AI agent docs',
status: 'pass',
message: `Astryx agent docs section present in ${withMarkers.join(', ')}.`,
};
}
/**
* Check 7 — @astryxdesign/core peer dependencies are satisfied by installed packages.
* @param {DoctorContext} ctx
* @returns {DoctorCheck}
*/
export function checkPeerDeps(ctx) {
if (!ctx.coreDir) {
return {
id: 'peer-deps',
label: '@astryxdesign/core peer dependencies',
status: 'info',
message: 'Skipped — @astryxdesign/core is not installed.',
};
}
const corePkg = readPkg(path.join(ctx.coreDir, 'package.json'));
const peers = corePkg?.peerDependencies ?? {};
const peerNames = Object.keys(peers);
if (peerNames.length === 0) {
return {
id: 'peer-deps',
label: '@astryxdesign/core peer dependencies',
status: 'info',
message: '@astryxdesign/core declares no peer dependencies.',
};
}
const missing = [];
/** @type {Array<{name: string, want: string, have: string}>} */
const mismatched = [];
for (const name of peerNames) {
const want = peers[name];
const installedDir = findInstalledPackage(ctx.cwd, name);
if (!installedDir) {
missing.push(`${name}@${want}`);
continue;
}
// Present and version-readable: verify it actually satisfies the range,
// not just that the package exists (a bare `npm install` can resolve an
// out-of-range version from a stale consumer range and still "look" fine).
const have = pkgVersion(installedDir);
if (have && !satisfiesRange(have, want)) {
mismatched.push({name, want, have});
}
}
if (missing.length > 0 || mismatched.length > 0) {
const problems = [];
if (missing.length) problems.push(`missing: ${missing.join(', ')}`);
if (mismatched.length) {
problems.push(
`out of range: ${mismatched
.map(m => `${m.name}@${m.have} (needs ${m.want})`)
.join(', ')}`,
);
}
// Pin the required range for anything wrong so the hint fixes it even when a
// stale consumer range would otherwise resolve an incompatible version.
// Quote targets containing shell metacharacters (e.g. `react@>=19.0.0`).
const quote = (/** @type {string} */ s) => (/[<>|() ]/.test(s) ? `'${s}'` : s);
const targets = [...missing, ...mismatched.map(m => `${m.name}@${m.want}`)].map(quote);
return {
id: 'peer-deps',
label: '@astryxdesign/core peer dependencies',
status: 'warn',
message: `Peer dependency issues — ${problems.join('; ')}.`,
fix: `Install compatible peers: \`npm install ${targets.join(' ')}\`.`,
};
}
return {
id: 'peer-deps',
label: '@astryxdesign/core peer dependencies',
status: 'pass',
message: `All peer dependencies satisfied (${peerNames.join(', ')}).`,
};
}
/**
* Check 8 — report the detected package manager (informational).
* @param {DoctorContext} ctx
* @returns {DoctorCheck}
*/
export function checkPackageManager(ctx) {
const pm = detectPackageManager(ctx.cwd);
const detected = pm !== 'npx';
return {
id: 'package-manager',
label: 'Package manager',
status: 'info',
message: detected
? `Detected package manager: ${pm}.`
: 'No lockfile detected — defaulting to npm/npx.',
};
}
/**
* Ordered list of synchronous check functions. Append here to add a check.
* (checkConfig is async and is awaited separately by {@link runChecks}.)
* @type {Array<(ctx: DoctorContext) => DoctorCheck>}
*/
export const SYNC_CHECKS = [
checkNodeVersion,
checkCoreInstalled,
checkVersionAlignment,
checkThemes,
checkAgentDocs,
checkPeerDeps,
checkPackageManager,
];
/**
* Run all diagnostic checks and return a structured report.
*
* @param {object} [options]
* @param {string} [options.cwd] - Directory to diagnose (default: process.cwd()).
* @returns {Promise<DoctorReport>}
*/
export async function runChecks(options = {}) {
const cwd = options.cwd ?? process.cwd();
const coreDir = findCoreDir(cwd);
// findConfigPath throws when multiple config files coexist. That's a
// misconfiguration doctor exists to report — catch it and surface it through
// checkConfig as a FAIL rather than crashing the whole diagnostic engine.
let configPath = null;
let configError = null;
try {
configPath = findConfigPath(cwd);
} catch (err) {
configError = /** @type {Error} */ (err);
}
// Resolve a possible theme key from config (best-effort; never throws).
let configTheme = null;
try {
const project = await Project.load(cwd);
configTheme =
/** @type {{theme?: string}} */ (project.config ?? {}).theme ?? null;
} catch {
// Best-effort: a missing/invalid config leaves configTheme null.
}
/** @type {DoctorContext} */
const ctx = {
cwd,
nodeVersion: process.versions.node,
coreDir,
configPath,
configTheme,
configError,
};
/** @type {DoctorCheck[]} */
const checks = [];
// checkConfig is async; run it in its declared slot (after themes).
for (const fn of SYNC_CHECKS) {
checks.push(fn(ctx));
if (fn === checkThemes) {
checks.push(await checkConfig(ctx));
}
}
const summary = {pass: 0, warn: 0, fail: 0, info: 0};
for (const c of checks) summary[c.status] += 1;
return {checks, summary};
}
/**
* Programmatic API: run the doctor and return the same envelope shape that
* `astryx doctor --json` emits.
*
* @param {object} [options]
* @param {string} [options.cwd]
* @returns {Promise<{type: 'doctor', data: DoctorReport}>}
*/
export async function doctor(options = {}) {
const report = await runChecks(options);
return {type: 'doctor', data: report};
}