@dxgjs/doctor 0.0.0-stage → 0.1.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DXG Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
+ DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,27 @@
1
- # Temporary Holding Version
1
+ # @dxgjs/doctor
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Diagnostic and repair tool for DXG projects.
4
+
5
+ > Internal package of the [DXG](https://github.com/dxgjs/dxg) monorepo. If you want to use DXG, install the CLI instead: `npx @dxgjs/cli`.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm i @dxgjs/doctor
11
+ # or
12
+ pnpm add @dxgjs/doctor
13
+ ```
14
+
15
+ ## Requirements
16
+
17
+ - Node.js >= 22.13
18
+ - ESM only
19
+
20
+ ## Links
21
+
22
+ - Repository: https://github.com/dxgjs/dxg
23
+ - Issues: https://github.com/dxgjs/dxg/issues
24
+
25
+ ## License
26
+
27
+ MIT
@@ -0,0 +1,375 @@
1
+ import { ProjectAwareness } from '@dxgjs/workspace';
2
+ import { Stats } from 'fs';
3
+
4
+ /**
5
+ * Type definitions for dxg doctor.
6
+ * These types define the contract between checks, the runner, and the CLI.
7
+ */
8
+
9
+ /** Category of diagnostic check. */
10
+ type DoctorCategory = "env" | "project" | "database" | "scripts";
11
+ /** Severity of a diagnostic finding. */
12
+ type DoctorSeverity = "error" | "warn" | "info" | "ok";
13
+ /** Result of a single check. */
14
+ interface DoctorCheckResult {
15
+ /** Unique identifier for this check (e.g., "env:database-url"). */
16
+ id: string;
17
+ /** Human-readable name of the check. */
18
+ name: string;
19
+ /** Category this check belongs to. */
20
+ category: DoctorCategory;
21
+ /** Severity of the finding. */
22
+ severity: DoctorSeverity;
23
+ /** Short message describing the result. */
24
+ message: string;
25
+ /** Detailed explanation, shown in verbose mode or on failure. */
26
+ detail?: string;
27
+ /** Whether this check can be auto-fixed. */
28
+ fixable: boolean;
29
+ /** If fixable, the fix function to apply. */
30
+ fix?: () => Promise<void>;
31
+ }
32
+ /** FS interface for file operations - matches @dxgjs/fs exports. */
33
+ interface FSInterface {
34
+ readFile(filePath: string, options?: {
35
+ encoding?: BufferEncoding | null;
36
+ } | BufferEncoding | null): Promise<string | Buffer>;
37
+ readFileSync(filePath: string, options?: {
38
+ encoding?: BufferEncoding | null;
39
+ } | BufferEncoding | null): string | Buffer;
40
+ writeFile(filePath: string, data: string | Buffer, options?: {
41
+ encoding?: BufferEncoding | null;
42
+ flag?: string;
43
+ } | BufferEncoding | null): Promise<void>;
44
+ writeFileSync(filePath: string, data: string | Buffer, options?: {
45
+ encoding?: BufferEncoding | null;
46
+ flag?: string;
47
+ } | BufferEncoding | null): void;
48
+ readJson<T = unknown>(filePath: string): Promise<T>;
49
+ writeJson<T = unknown>(filePath: string, data: T, options?: {
50
+ spaces?: number;
51
+ }): Promise<void>;
52
+ pathExists(filePath: string): Promise<boolean>;
53
+ pathExistsSync(filePath: string): boolean;
54
+ stat(filePath: string): Promise<Stats>;
55
+ statSync(filePath: string): Stats;
56
+ mkdir(filePath: string, options?: {
57
+ recursive?: boolean;
58
+ }): Promise<string | undefined>;
59
+ mkdirSync(filePath: string, options?: {
60
+ recursive?: boolean;
61
+ }): void;
62
+ readdir(filePath: string, options?: {
63
+ encoding?: BufferEncoding | null;
64
+ }): Promise<string[]>;
65
+ readdirSync(filePath: string, options?: {
66
+ encoding?: BufferEncoding | null;
67
+ }): string[];
68
+ glob(pattern: string, options?: {
69
+ cwd?: string;
70
+ }): Promise<string[]>;
71
+ globSync(pattern: string, options?: {
72
+ cwd?: string;
73
+ }): string[];
74
+ executeCommand(command: string, args?: string[], options?: Record<string, unknown>): Promise<{
75
+ stdout: string;
76
+ stderr: string;
77
+ exitCode: number;
78
+ }>;
79
+ }
80
+ /** Template renderer interface. */
81
+ interface TemplateRenderer {
82
+ render(template: string, data: Record<string, unknown>): string;
83
+ }
84
+ /** Options passed to each check. */
85
+ interface DoctorCheckOptions {
86
+ /** Current working directory (project root). */
87
+ cwd: string;
88
+ /** Parsed package.json content. */
89
+ packageJson: Record<string, unknown>;
90
+ /** Project awareness data. */
91
+ awareness: ProjectAwareness;
92
+ /** FS abstraction for reading/writing files. */
93
+ fs: FSInterface;
94
+ /** Template renderer. */
95
+ templates: TemplateRenderer;
96
+ /** CLI options merged with global options. */
97
+ options: DoctorCliOptions;
98
+ }
99
+ /** CLI options for the doctor command. */
100
+ interface DoctorCliOptions {
101
+ /** Categories to run (default: all). */
102
+ category?: DoctorCategory[];
103
+ /** Output as JSON. */
104
+ json?: boolean;
105
+ /** Attempt to fix issues automatically. */
106
+ fix?: boolean;
107
+ /** Skip confirmation prompts for fixes. */
108
+ yes?: boolean;
109
+ /** Dry run — show what would be fixed without applying. */
110
+ dryRun?: boolean;
111
+ /** Exit with code 1 if any issues found (for CI). */
112
+ checkOnly?: boolean;
113
+ /** Non-interactive mode (from global options). */
114
+ nonInteractive?: boolean;
115
+ /** Force mode (from global options). */
116
+ force?: boolean;
117
+ }
118
+ /** Context passed to checks, built from CLI options and project state. */
119
+ interface DoctorContext {
120
+ /** Current working directory (project root). */
121
+ cwd: string;
122
+ /** Parsed package.json content (fresh read after any fixes). */
123
+ packageJson: Record<string, unknown>;
124
+ /** Project awareness data. */
125
+ awareness: ProjectAwareness;
126
+ /** FS abstraction for reading/writing files. */
127
+ fs: FSInterface;
128
+ /** Template renderer. */
129
+ templates: TemplateRenderer;
130
+ /** CLI options merged with global options. */
131
+ options: DoctorCliOptions;
132
+ }
133
+ /** Function signature for a diagnostic check. */
134
+ type DoctorCheck = (ctx: DoctorContext) => Promise<DoctorCheckResult | DoctorCheckResult[]>;
135
+ /** Registry entry for a check. */
136
+ interface DoctorCheckRegistryEntry {
137
+ /** Unique identifier for this check (e.g., "env:database-url"). */
138
+ id: string;
139
+ /** The check function. */
140
+ check: DoctorCheck;
141
+ /** Category this check belongs to. */
142
+ category: DoctorCategory;
143
+ /** Whether this check runs by default (can be overridden by --category). */
144
+ default?: boolean;
145
+ }
146
+ /** Aggregated results from all checks. */
147
+ interface DoctorReport {
148
+ /** All check results, grouped by category. */
149
+ results: Record<DoctorCategory, DoctorCheckResult[]>;
150
+ /** Summary counts. */
151
+ summary: {
152
+ ok: number;
153
+ info: number;
154
+ warn: number;
155
+ error: number;
156
+ fixable: number;
157
+ };
158
+ /** Whether any errors were found. */
159
+ hasErrors: boolean;
160
+ /** Whether any warnings were found. */
161
+ hasWarnings: boolean;
162
+ /** Whether any fixes are available. */
163
+ hasFixes: boolean;
164
+ }
165
+ /** Exit codes for the doctor command. */
166
+ declare const DoctorExitCode: {
167
+ /** All checks passed (or only info/warn). */
168
+ readonly OK: 0;
169
+ /** Issues found (errors or warnings depending on --check-only). */
170
+ readonly ISSUES_FOUND: 1;
171
+ /** Invalid usage (bad flags, missing args, etc.). */
172
+ readonly INVALID_USAGE: 2;
173
+ };
174
+ type DoctorExitCode = (typeof DoctorExitCode)[keyof typeof DoctorExitCode];
175
+
176
+ /**
177
+ * Check registry for dxg doctor.
178
+ * Central place to register and retrieve diagnostic checks by category.
179
+ */
180
+
181
+ /**
182
+ * Register a diagnostic check.
183
+ * @param id Unique identifier for the check (e.g., "env:database-url")
184
+ * @param check The check function
185
+ * @param category Category this check belongs to
186
+ * @param defaultRun Whether this check runs by default (default: true)
187
+ */
188
+ declare function registerCheck(id: string, check: DoctorCheck, category: DoctorCategory, defaultRun?: boolean): void;
189
+ /**
190
+ * Get a check by its ID.
191
+ */
192
+ declare function getCheck(id: string): DoctorCheckRegistryEntry | undefined;
193
+ /**
194
+ * Get all checks for a specific category.
195
+ */
196
+ declare function getChecksByCategory(category: DoctorCategory): DoctorCheckRegistryEntry[];
197
+ /**
198
+ * Get all registered checks.
199
+ */
200
+ declare function getAllChecks(): DoctorCheckRegistryEntry[];
201
+ /**
202
+ * Get all checks that should run by default for the given categories.
203
+ * If categories is empty, returns all default checks from all categories.
204
+ */
205
+ declare function getDefaultChecks(categories?: DoctorCategory[]): DoctorCheckRegistryEntry[];
206
+ /**
207
+ * Clear the registry (mainly for testing).
208
+ */
209
+ declare function clearRegistry(): void;
210
+ /**
211
+ * Run a specific check by ID with the given context.
212
+ */
213
+ declare function runCheck(id: string, ctx: DoctorContext): Promise<Awaited<ReturnType<DoctorCheck>>>;
214
+
215
+ /**
216
+ * Runner for dxg doctor checks.
217
+ * Executes registered checks, aggregates results, and handles fixes.
218
+ */
219
+
220
+ /**
221
+ * Run all default checks (or filtered by category) and return an aggregated report.
222
+ */
223
+ declare function runDoctor(ctx: DoctorContext): Promise<DoctorReport>;
224
+ /**
225
+ * Apply fixes for all fixable checks in the report.
226
+ * Returns the number of fixes applied.
227
+ */
228
+ declare function applyFixes(report: DoctorReport, ctx: DoctorContext): Promise<number>;
229
+ /**
230
+ * Determine exit code based on report and CLI options.
231
+ */
232
+ declare function determineExitCode(report: DoctorReport, options: {
233
+ checkOnly?: boolean;
234
+ json?: boolean;
235
+ }): DoctorExitCode;
236
+ /**
237
+ * Run checks for a specific category only.
238
+ */
239
+ declare function runCategoryChecks(category: DoctorCategory, ctx: DoctorContext): Promise<DoctorCheckResult[]>;
240
+
241
+ /**
242
+ * Renderers for dxg doctor output.
243
+ * Uses Clack for interactive UI and picocolors for colors.
244
+ * No emojis - uses 6-char colored tags and bullets.
245
+ */
246
+
247
+ /**
248
+ * Show the doctor intro banner.
249
+ */
250
+ declare function showIntro(): void;
251
+ /**
252
+ * Show the doctor outro with summary.
253
+ */
254
+ declare function showOutro(report: DoctorReport, options: {
255
+ json?: boolean;
256
+ checkOnly?: boolean;
257
+ fix?: boolean;
258
+ dryRun?: boolean;
259
+ }): void;
260
+ /**
261
+ * Render a single check result in human-readable format.
262
+ * Includes category label since it's shown in isolation.
263
+ */
264
+ declare function renderCheckResult(result: DoctorCheckResult): void;
265
+ /**
266
+ * Render all results grouped by category.
267
+ * Wraps each category in a note() block with category name as title,
268
+ * omitting the per-line category label since it's redundant.
269
+ */
270
+ declare function renderReport(report: DoctorReport): void;
271
+ /**
272
+ * Render report as JSON.
273
+ */
274
+ declare function renderJson(report: DoctorReport): void;
275
+ /**
276
+ * Show a spinner while running checks.
277
+ */
278
+ declare function withSpinner<T>(message: string, fn: () => Promise<T>): Promise<T>;
279
+ /**
280
+ * Show a note with details (for fix dry-run preview).
281
+ */
282
+ declare function showFixPreview(fixes: string[]): void;
283
+ /**
284
+ * Confirm before applying fixes (unless --yes).
285
+ */
286
+ declare function confirmFixes(ctx: DoctorContext, count: number): Promise<boolean>;
287
+ /**
288
+ * Format a check ID for display.
289
+ */
290
+ declare function formatCheckId(id: string): string;
291
+
292
+ /**
293
+ * Flag validation and parsing for dxg doctor command.
294
+ * Handles CLI flags, global options merging, and validation.
295
+ */
296
+
297
+ /**
298
+ * Validate and normalize doctor CLI options.
299
+ * Merges global options (--non-interactive, --dry-run, --force) with doctor-specific flags.
300
+ */
301
+ declare function validateDoctorOptions(rawOptions: Record<string, unknown>, globalOptions: {
302
+ nonInteractive?: boolean;
303
+ dryRun?: boolean;
304
+ force?: boolean;
305
+ }): DoctorCliOptions;
306
+ /**
307
+ * Validate that only known flags are used.
308
+ */
309
+ declare function validateKnownFlags(flags: Record<string, unknown>): void;
310
+ /**
311
+ * Map exit code to process exit.
312
+ */
313
+ declare function mapExitCode(code: DoctorExitCode): number;
314
+
315
+ /**
316
+ * Environment checks for dxg doctor.
317
+ * Validates .env files, DATABASE_URL, and environment configuration.
318
+ * Aligned with the database generator's actual behavior.
319
+ */
320
+ /**
321
+ * Register all environment checks.
322
+ */
323
+ declare function registerEnvChecks(): Promise<void>;
324
+
325
+ /**
326
+ * Project structure checks for dxg doctor.
327
+ * Validates package.json and .gitignore (for .env coverage).
328
+ * Only checks required by the database generator.
329
+ */
330
+ /**
331
+ * Register all project checks.
332
+ */
333
+ declare function registerProjectChecks(): Promise<void>;
334
+
335
+ /**
336
+ * Database checks for dxg doctor.
337
+ * Validates Prisma schema, provider, generated client, and DXG-owned artifacts.
338
+ * Aligned with the database generator's actual behavior.
339
+ */
340
+ /**
341
+ * Register all database checks.
342
+ */
343
+ declare function registerDatabaseChecks(): Promise<void>;
344
+
345
+ /**
346
+ * Scripts checks for dxg doctor.
347
+ * Validates package.json scripts added by the database generator.
348
+ * Generator adds exactly 5 Prisma scripts (provider-independent):
349
+ * - db:generate: "prisma generate"
350
+ * - db:push: "prisma db push"
351
+ * - db:studio: "prisma studio"
352
+ * - db:pull: "prisma db pull"
353
+ * - db:seed: "prisma db seed" (optional, only if selected during generator run)
354
+ *
355
+ * Migration scripts (db:migrate, db:migrate:deploy) are NOT managed by DXG.
356
+ * Users who need migrations run the Prisma CLI directly.
357
+ */
358
+ /**
359
+ * Register all scripts checks.
360
+ * Only 2 checks remain: package-json-scripts (structure) and prisma-scripts (generator's 5 scripts)
361
+ */
362
+ declare function registerScriptsChecks(): Promise<void>;
363
+
364
+ /**
365
+ * Main entry point for @dxgjs/doctor package.
366
+ * Exports all public APIs for the doctor command.
367
+ */
368
+
369
+ /**
370
+ * Register all default checks.
371
+ * Call this once at CLI startup to populate the registry.
372
+ */
373
+ declare function registerAllChecks(): Promise<void>;
374
+
375
+ export { type DoctorCategory, type DoctorCheck, type DoctorCheckOptions, type DoctorCheckRegistryEntry, type DoctorCheckResult, type DoctorCliOptions, type DoctorContext, DoctorExitCode, type DoctorReport, type DoctorSeverity, type FSInterface, type TemplateRenderer, applyFixes, clearRegistry, confirmFixes, determineExitCode, formatCheckId, getAllChecks, getCheck, getChecksByCategory, getDefaultChecks, mapExitCode, registerAllChecks, registerCheck, registerDatabaseChecks, registerEnvChecks, registerProjectChecks, registerScriptsChecks, renderCheckResult, renderJson, renderReport, runCategoryChecks, runCheck, runDoctor, showFixPreview, showIntro, showOutro, validateDoctorOptions, validateKnownFlags, withSpinner };