@dxgjs/doctor 0.0.0 → 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 +21 -0
- package/README.md +16 -34
- package/dist/index.d.ts +375 -0
- package/dist/index.js +2054 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -7
package/dist/index.js
ADDED
|
@@ -0,0 +1,2054 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __glob = (map) => (path) => {
|
|
4
|
+
var fn = map[path];
|
|
5
|
+
if (fn) return fn();
|
|
6
|
+
throw new Error("Module not found in bundle: " + path);
|
|
7
|
+
};
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/registry.ts
|
|
17
|
+
var registry_exports = {};
|
|
18
|
+
__export(registry_exports, {
|
|
19
|
+
clearRegistry: () => clearRegistry,
|
|
20
|
+
getAllChecks: () => getAllChecks,
|
|
21
|
+
getCheck: () => getCheck,
|
|
22
|
+
getChecksByCategory: () => getChecksByCategory,
|
|
23
|
+
getDefaultChecks: () => getDefaultChecks,
|
|
24
|
+
registerCheck: () => registerCheck,
|
|
25
|
+
runCheck: () => runCheck
|
|
26
|
+
});
|
|
27
|
+
function registerCheck(id, check, category, defaultRun = true) {
|
|
28
|
+
if (checkRegistry.has(id)) {
|
|
29
|
+
throw new Error(`Doctor check "${id}" is already registered`);
|
|
30
|
+
}
|
|
31
|
+
checkRegistry.set(id, { id, check, category, default: defaultRun });
|
|
32
|
+
}
|
|
33
|
+
function getCheck(id) {
|
|
34
|
+
return checkRegistry.get(id);
|
|
35
|
+
}
|
|
36
|
+
function getChecksByCategory(category) {
|
|
37
|
+
const results = [];
|
|
38
|
+
for (const [, entry] of checkRegistry) {
|
|
39
|
+
if (entry.category === category) {
|
|
40
|
+
results.push(entry);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return results;
|
|
44
|
+
}
|
|
45
|
+
function getAllChecks() {
|
|
46
|
+
return Array.from(checkRegistry.values());
|
|
47
|
+
}
|
|
48
|
+
function getDefaultChecks(categories) {
|
|
49
|
+
const results = [];
|
|
50
|
+
for (const [, entry] of checkRegistry) {
|
|
51
|
+
if (entry.default) {
|
|
52
|
+
if (!categories || categories.length === 0 || categories.includes(entry.category)) {
|
|
53
|
+
results.push(entry);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return results;
|
|
58
|
+
}
|
|
59
|
+
function clearRegistry() {
|
|
60
|
+
checkRegistry.clear();
|
|
61
|
+
}
|
|
62
|
+
async function runCheck(id, ctx) {
|
|
63
|
+
const entry = checkRegistry.get(id);
|
|
64
|
+
if (!entry) {
|
|
65
|
+
throw new Error(`Doctor check "${id}" not found`);
|
|
66
|
+
}
|
|
67
|
+
return entry.check(ctx);
|
|
68
|
+
}
|
|
69
|
+
var checkRegistry;
|
|
70
|
+
var init_registry = __esm({
|
|
71
|
+
"src/registry.ts"() {
|
|
72
|
+
"use strict";
|
|
73
|
+
checkRegistry = /* @__PURE__ */ new Map();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// src/utils/paths.ts
|
|
78
|
+
import { isAbsolute, relative, win32 } from "path";
|
|
79
|
+
function isWindowsPath(p) {
|
|
80
|
+
return /^[a-zA-Z]:[\\/]/.test(p) || p.startsWith("\\\\");
|
|
81
|
+
}
|
|
82
|
+
function toRelativePath(absolutePath, cwd) {
|
|
83
|
+
try {
|
|
84
|
+
const useWin32 = isWindowsPath(absolutePath) || isWindowsPath(cwd);
|
|
85
|
+
const rel = useWin32 ? win32.relative(cwd, absolutePath) : relative(cwd, absolutePath);
|
|
86
|
+
if (rel === "") return ".";
|
|
87
|
+
if (rel.startsWith("..")) return absolutePath;
|
|
88
|
+
if (useWin32 ? win32.isAbsolute(rel) : isAbsolute(rel)) return absolutePath;
|
|
89
|
+
return normalizeSeparators(rel);
|
|
90
|
+
} catch {
|
|
91
|
+
return absolutePath;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function normalizeSeparators(path) {
|
|
95
|
+
return path.replace(/\\/g, "/");
|
|
96
|
+
}
|
|
97
|
+
function toDisplayPath(absolutePath, cwd) {
|
|
98
|
+
return normalizeSeparators(toRelativePath(absolutePath, cwd));
|
|
99
|
+
}
|
|
100
|
+
var init_paths = __esm({
|
|
101
|
+
"src/utils/paths.ts"() {
|
|
102
|
+
"use strict";
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// src/checks/database.ts
|
|
107
|
+
var database_exports = {};
|
|
108
|
+
__export(database_exports, {
|
|
109
|
+
registerDatabaseChecks: () => registerDatabaseChecks
|
|
110
|
+
});
|
|
111
|
+
import { readFileSync, existsSync } from "fs";
|
|
112
|
+
import { join, dirname } from "path";
|
|
113
|
+
import {
|
|
114
|
+
SCHEMA_PATH,
|
|
115
|
+
PRISMA_CONFIG_PATH,
|
|
116
|
+
DEFAULT_HELPER_PATH,
|
|
117
|
+
DEFAULT_GENERATOR_OUTPUT,
|
|
118
|
+
SUPPORTED_PROVIDERS,
|
|
119
|
+
getProviderData,
|
|
120
|
+
isSupportedProvider,
|
|
121
|
+
extractProviderFromSchema,
|
|
122
|
+
extractProviderFromConfig,
|
|
123
|
+
normalizePrismaProvider,
|
|
124
|
+
parseGeneratorOutput,
|
|
125
|
+
resolveGeneratorOutput,
|
|
126
|
+
isValidPrismaClientHelper,
|
|
127
|
+
SEED_SCRIPT_NAME,
|
|
128
|
+
SEED_CONFIG_COMMAND
|
|
129
|
+
} from "@dxgjs/database-meta";
|
|
130
|
+
function findPrismaSchema(cwd) {
|
|
131
|
+
const possiblePaths = [
|
|
132
|
+
join(cwd, SCHEMA_PATH),
|
|
133
|
+
join(cwd, "packages", "database", SCHEMA_PATH),
|
|
134
|
+
join(cwd, "apps", "api", SCHEMA_PATH)
|
|
135
|
+
];
|
|
136
|
+
for (const path of possiblePaths) {
|
|
137
|
+
if (existsSync(path)) return path;
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
function findPrismaConfig(cwd) {
|
|
142
|
+
const possiblePaths = [
|
|
143
|
+
join(cwd, PRISMA_CONFIG_PATH),
|
|
144
|
+
join(cwd, "prisma", PRISMA_CONFIG_PATH)
|
|
145
|
+
];
|
|
146
|
+
for (const path of possiblePaths) {
|
|
147
|
+
if (existsSync(path)) return path;
|
|
148
|
+
}
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
function extractProvider(cwd) {
|
|
152
|
+
const schemaPath = findPrismaSchema(cwd);
|
|
153
|
+
if (schemaPath) {
|
|
154
|
+
try {
|
|
155
|
+
const content = readFileSync(schemaPath, "utf-8");
|
|
156
|
+
const provider = extractProviderFromSchema(content);
|
|
157
|
+
if (provider) return normalizePrismaProvider(provider);
|
|
158
|
+
} catch {
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const configPath = findPrismaConfig(cwd);
|
|
162
|
+
if (configPath) {
|
|
163
|
+
try {
|
|
164
|
+
const content = readFileSync(configPath, "utf-8");
|
|
165
|
+
const provider = extractProviderFromConfig(content);
|
|
166
|
+
if (provider) return normalizePrismaProvider(provider);
|
|
167
|
+
} catch {
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
function findHelperFile(cwd) {
|
|
173
|
+
const possiblePaths = [
|
|
174
|
+
join(cwd, DEFAULT_HELPER_PATH),
|
|
175
|
+
join(cwd, "src", DEFAULT_HELPER_PATH)
|
|
176
|
+
];
|
|
177
|
+
for (const path of possiblePaths) {
|
|
178
|
+
if (existsSync(path)) return path;
|
|
179
|
+
}
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
async function checkPrismaSchema(ctx) {
|
|
183
|
+
const schemaPath = findPrismaSchema(ctx.cwd);
|
|
184
|
+
const configPath = findPrismaConfig(ctx.cwd);
|
|
185
|
+
if (!schemaPath && !configPath) {
|
|
186
|
+
return {
|
|
187
|
+
id: "database:prisma-schema",
|
|
188
|
+
name: DATABASE_CHECKS["database:prisma-schema"].name,
|
|
189
|
+
category: "database",
|
|
190
|
+
severity: "info",
|
|
191
|
+
message: "Skipped \u2014 no Prisma schema or config (database not configured)",
|
|
192
|
+
fixable: false
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (!schemaPath || !configPath) {
|
|
196
|
+
return {
|
|
197
|
+
id: "database:prisma-schema",
|
|
198
|
+
name: DATABASE_CHECKS["database:prisma-schema"].name,
|
|
199
|
+
category: "database",
|
|
200
|
+
severity: "error",
|
|
201
|
+
message: "Incomplete Prisma setup",
|
|
202
|
+
detail: !schemaPath ? "prisma/schema.prisma missing but prisma7.config.ts exists" : "prisma7.config.ts missing but prisma/schema.prisma exists",
|
|
203
|
+
fixable: false
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
id: "database:prisma-schema",
|
|
208
|
+
name: DATABASE_CHECKS["database:prisma-schema"].name,
|
|
209
|
+
category: "database",
|
|
210
|
+
severity: "ok",
|
|
211
|
+
message: `Prisma schema found at ${toDisplayPath(schemaPath, ctx.cwd)}`,
|
|
212
|
+
fixable: false
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
async function checkPrismaConfig(ctx) {
|
|
216
|
+
const schemaPath = findPrismaSchema(ctx.cwd);
|
|
217
|
+
const configPath = findPrismaConfig(ctx.cwd);
|
|
218
|
+
if (!schemaPath && !configPath) {
|
|
219
|
+
return {
|
|
220
|
+
id: "database:prisma-config",
|
|
221
|
+
name: DATABASE_CHECKS["database:prisma-config"].name,
|
|
222
|
+
category: "database",
|
|
223
|
+
severity: "info",
|
|
224
|
+
message: "Skipped \u2014 no Prisma schema or config (database not configured)",
|
|
225
|
+
fixable: false
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (!configPath && schemaPath) {
|
|
229
|
+
return {
|
|
230
|
+
id: "database:prisma-config",
|
|
231
|
+
name: DATABASE_CHECKS["database:prisma-config"].name,
|
|
232
|
+
category: "database",
|
|
233
|
+
severity: "warn",
|
|
234
|
+
message: "Prisma 7 config (prisma7.config.ts) not found",
|
|
235
|
+
detail: "prisma/schema.prisma exists but prisma7.config.ts is missing",
|
|
236
|
+
fixable: false
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
id: "database:prisma-config",
|
|
241
|
+
name: DATABASE_CHECKS["database:prisma-config"].name,
|
|
242
|
+
category: "database",
|
|
243
|
+
severity: "ok",
|
|
244
|
+
message: `Prisma config found at ${toDisplayPath(configPath, ctx.cwd)}`,
|
|
245
|
+
fixable: false
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
async function checkProvider(ctx) {
|
|
249
|
+
const schemaPath = findPrismaSchema(ctx.cwd);
|
|
250
|
+
const configPath = findPrismaConfig(ctx.cwd);
|
|
251
|
+
if (!schemaPath && !configPath) {
|
|
252
|
+
return {
|
|
253
|
+
id: "database:provider",
|
|
254
|
+
name: DATABASE_CHECKS["database:provider"].name,
|
|
255
|
+
category: "database",
|
|
256
|
+
severity: "info",
|
|
257
|
+
message: "Skipped \u2014 no Prisma schema or config (database not configured)",
|
|
258
|
+
fixable: false
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
const provider = extractProvider(ctx.cwd);
|
|
262
|
+
if (!provider) {
|
|
263
|
+
const canFix = !!schemaPath && !configPath;
|
|
264
|
+
return {
|
|
265
|
+
id: "database:provider",
|
|
266
|
+
name: DATABASE_CHECKS["database:provider"].name,
|
|
267
|
+
category: "database",
|
|
268
|
+
severity: "error",
|
|
269
|
+
message: "No provider specified in Prisma schema or config",
|
|
270
|
+
detail: "Run 'dxg add database' to configure a provider",
|
|
271
|
+
fixable: canFix,
|
|
272
|
+
fix: canFix ? async () => {
|
|
273
|
+
await fixProviderInSchema(ctx.cwd, schemaPath);
|
|
274
|
+
} : void 0
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
if (!isSupportedProvider(provider)) {
|
|
278
|
+
return {
|
|
279
|
+
id: "database:provider",
|
|
280
|
+
name: DATABASE_CHECKS["database:provider"].name,
|
|
281
|
+
category: "database",
|
|
282
|
+
severity: "warn",
|
|
283
|
+
message: `Unknown provider: ${provider}`,
|
|
284
|
+
detail: `Valid providers: ${SUPPORTED_PROVIDERS.join(", ")}`,
|
|
285
|
+
fixable: false
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
const providerData = getProviderData(provider);
|
|
289
|
+
return {
|
|
290
|
+
id: "database:provider",
|
|
291
|
+
name: DATABASE_CHECKS["database:provider"].name,
|
|
292
|
+
category: "database",
|
|
293
|
+
severity: "ok",
|
|
294
|
+
message: `Provider configured: ${providerData?.displayName ?? provider}`,
|
|
295
|
+
fixable: false
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
async function fixProviderInSchema(_cwd, schemaPath) {
|
|
299
|
+
const { readFile, writeFile } = await import("@dxgjs/fs");
|
|
300
|
+
const contentResult = await readFile(schemaPath, "utf-8");
|
|
301
|
+
const content = typeof contentResult === "string" ? contentResult : new TextDecoder().decode(contentResult);
|
|
302
|
+
const hasProvider = /datasource\s+\w+\s*\{[\s\S]*?provider\s*=/.test(content);
|
|
303
|
+
if (!hasProvider) {
|
|
304
|
+
const provider = "postgresql";
|
|
305
|
+
const updated = content.replace(
|
|
306
|
+
/(datasource\s+\w+\s*\{)([\s\S]*?)(\})/,
|
|
307
|
+
(_match, before, middle, after) => {
|
|
308
|
+
const hasUrl = /url\s*=/.test(middle);
|
|
309
|
+
const indent = middle.match(/^\s*/)?.[0] || " ";
|
|
310
|
+
const providerLine = `${indent}provider = "${provider}"`;
|
|
311
|
+
if (hasUrl) {
|
|
312
|
+
return before + "\n" + providerLine + "\n" + middle + after;
|
|
313
|
+
} else {
|
|
314
|
+
return before + "\n" + providerLine + "\n" + middle + after;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
await writeFile(schemaPath, updated, "utf-8");
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
async function checkPrismaClient(ctx) {
|
|
322
|
+
const schemaPath = findPrismaSchema(ctx.cwd);
|
|
323
|
+
const configPath = findPrismaConfig(ctx.cwd);
|
|
324
|
+
if (!schemaPath && !configPath) {
|
|
325
|
+
return {
|
|
326
|
+
id: "database:prisma-client",
|
|
327
|
+
name: DATABASE_CHECKS["database:prisma-client"].name,
|
|
328
|
+
category: "database",
|
|
329
|
+
severity: "info",
|
|
330
|
+
message: "Skipped \u2014 no Prisma schema or config",
|
|
331
|
+
fixable: false
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
let generatorOutput = DEFAULT_GENERATOR_OUTPUT;
|
|
335
|
+
let generatorBlockMissing = false;
|
|
336
|
+
let generatorOutputMissing = false;
|
|
337
|
+
if (schemaPath) {
|
|
338
|
+
try {
|
|
339
|
+
const content = readFileSync(schemaPath, "utf-8");
|
|
340
|
+
const { present, output } = parseGeneratorOutput(content);
|
|
341
|
+
if (!present) {
|
|
342
|
+
generatorBlockMissing = true;
|
|
343
|
+
} else if (!output) {
|
|
344
|
+
generatorOutputMissing = true;
|
|
345
|
+
} else {
|
|
346
|
+
const resolved = resolveGeneratorOutput(output);
|
|
347
|
+
if (resolved) {
|
|
348
|
+
generatorOutput = resolved;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
} catch {
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const helperPath = findHelperFile(ctx.cwd);
|
|
355
|
+
if (helperPath) {
|
|
356
|
+
generatorOutput = join(dirname(helperPath), "generated", "prisma");
|
|
357
|
+
}
|
|
358
|
+
const clientPaths = [
|
|
359
|
+
join(ctx.cwd, generatorOutput, "client.ts"),
|
|
360
|
+
join(ctx.cwd, "node_modules", ".prisma", "client"),
|
|
361
|
+
join(ctx.cwd, "node_modules", "@prisma", "client")
|
|
362
|
+
];
|
|
363
|
+
if (schemaPath) {
|
|
364
|
+
clientPaths.unshift(join(dirname(schemaPath), "..", generatorOutput, "client.ts"));
|
|
365
|
+
}
|
|
366
|
+
let found = false;
|
|
367
|
+
for (const path of clientPaths) {
|
|
368
|
+
if (existsSync(path)) {
|
|
369
|
+
found = true;
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
const pkgPath = join(ctx.cwd, "package.json");
|
|
374
|
+
let hasDependency = false;
|
|
375
|
+
if (existsSync(pkgPath)) {
|
|
376
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
377
|
+
hasDependency = !!(pkg.dependencies?.["@prisma/client"] || pkg.devDependencies?.["@prisma/client"]);
|
|
378
|
+
}
|
|
379
|
+
if (!found && !hasDependency && schemaPath && generatorBlockMissing) {
|
|
380
|
+
return {
|
|
381
|
+
id: "database:prisma-client",
|
|
382
|
+
name: DATABASE_CHECKS["database:prisma-client"].name,
|
|
383
|
+
category: "database",
|
|
384
|
+
severity: "warn",
|
|
385
|
+
message: "Prisma Client not generated and generator block missing in schema.prisma",
|
|
386
|
+
detail: "Run 'dxg add database' to add generator block with output path, then run 'pnpm prisma generate'",
|
|
387
|
+
fixable: true,
|
|
388
|
+
fix: async () => {
|
|
389
|
+
await fixGeneratorBlockInSchema(ctx.cwd, schemaPath);
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
if (!found && !hasDependency && schemaPath && generatorOutputMissing) {
|
|
394
|
+
return {
|
|
395
|
+
id: "database:prisma-client",
|
|
396
|
+
name: DATABASE_CHECKS["database:prisma-client"].name,
|
|
397
|
+
category: "database",
|
|
398
|
+
severity: "warn",
|
|
399
|
+
message: "Prisma Client not generated and generator output missing in schema.prisma",
|
|
400
|
+
detail: "Add output path to generator block, then run 'pnpm prisma generate'",
|
|
401
|
+
fixable: true,
|
|
402
|
+
fix: async () => {
|
|
403
|
+
await fixGeneratorOutputInSchema(ctx.cwd, schemaPath);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
if (!found && !hasDependency) {
|
|
408
|
+
return {
|
|
409
|
+
id: "database:prisma-client",
|
|
410
|
+
name: DATABASE_CHECKS["database:prisma-client"].name,
|
|
411
|
+
category: "database",
|
|
412
|
+
severity: "warn",
|
|
413
|
+
message: "Prisma Client not generated",
|
|
414
|
+
detail: "Run 'pnpm prisma generate' or 'dxg add database' to generate client",
|
|
415
|
+
fixable: true,
|
|
416
|
+
fix: async () => {
|
|
417
|
+
const { executeCommand } = await import("@dxgjs/fs");
|
|
418
|
+
await executeCommand("pnpm", ["prisma", "generate"], { cwd: ctx.cwd });
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
if (!found && hasDependency) {
|
|
423
|
+
return {
|
|
424
|
+
id: "database:prisma-client",
|
|
425
|
+
name: DATABASE_CHECKS["database:prisma-client"].name,
|
|
426
|
+
category: "database",
|
|
427
|
+
severity: "warn",
|
|
428
|
+
message: "@prisma/client in package.json but client not generated",
|
|
429
|
+
detail: "Run 'pnpm prisma generate' to generate the client",
|
|
430
|
+
fixable: true,
|
|
431
|
+
fix: async () => {
|
|
432
|
+
const { executeCommand } = await import("@dxgjs/fs");
|
|
433
|
+
await executeCommand("pnpm", ["prisma", "generate"], { cwd: ctx.cwd });
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
return {
|
|
438
|
+
id: "database:prisma-client",
|
|
439
|
+
name: DATABASE_CHECKS["database:prisma-client"].name,
|
|
440
|
+
category: "database",
|
|
441
|
+
severity: "ok",
|
|
442
|
+
message: "Prisma Client is generated",
|
|
443
|
+
fixable: false
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
async function fixGeneratorBlockInSchema(_cwd, schemaPath) {
|
|
447
|
+
const { readFile, writeFile } = await import("@dxgjs/fs");
|
|
448
|
+
const contentResult = await readFile(schemaPath, "utf-8");
|
|
449
|
+
const content = typeof contentResult === "string" ? contentResult : new TextDecoder().decode(contentResult);
|
|
450
|
+
const hasGeneratorBlock = /generator\s+client\s*\{/.test(content);
|
|
451
|
+
if (hasGeneratorBlock) return;
|
|
452
|
+
const updated = content.replace(
|
|
453
|
+
/(datasource\s+\w+\s*\{[\s\S]*?\})/,
|
|
454
|
+
(match) => match + '\n\ngenerator client {\n provider = "prisma-client-js"\n output = "' + DEFAULT_GENERATOR_OUTPUT + '"\n}'
|
|
455
|
+
);
|
|
456
|
+
await writeFile(schemaPath, updated, "utf-8");
|
|
457
|
+
}
|
|
458
|
+
async function fixGeneratorOutputInSchema(_cwd, schemaPath) {
|
|
459
|
+
const { readFile, writeFile } = await import("@dxgjs/fs");
|
|
460
|
+
const contentResult = await readFile(schemaPath, "utf-8");
|
|
461
|
+
const content = typeof contentResult === "string" ? contentResult : new TextDecoder().decode(contentResult);
|
|
462
|
+
const generatorMatch = /generator\s+client\s*\{([\s\S]*?)\}/.exec(content);
|
|
463
|
+
if (!generatorMatch) return;
|
|
464
|
+
const block = generatorMatch[1];
|
|
465
|
+
const hasOutput = /output\s*=/.test(block);
|
|
466
|
+
if (hasOutput) return;
|
|
467
|
+
const updated = content.replace(
|
|
468
|
+
/(generator\s+client\s*\{)([\s\S]*?)(\})/,
|
|
469
|
+
(_match, before, middle, after) => {
|
|
470
|
+
const indent = middle.match(/^\s*/)?.[0] || " ";
|
|
471
|
+
const outputLine = `${indent}output = "${DEFAULT_GENERATOR_OUTPUT}"`;
|
|
472
|
+
return before + "\n" + middle + "\n" + outputLine + "\n" + after;
|
|
473
|
+
}
|
|
474
|
+
);
|
|
475
|
+
await writeFile(schemaPath, updated, "utf-8");
|
|
476
|
+
}
|
|
477
|
+
async function checkHelperFile(ctx) {
|
|
478
|
+
const schemaPath = findPrismaSchema(ctx.cwd);
|
|
479
|
+
const configPath = findPrismaConfig(ctx.cwd);
|
|
480
|
+
if (!schemaPath && !configPath) {
|
|
481
|
+
return {
|
|
482
|
+
id: "database:helper-file",
|
|
483
|
+
name: DATABASE_CHECKS["database:helper-file"].name,
|
|
484
|
+
category: "database",
|
|
485
|
+
severity: "info",
|
|
486
|
+
message: "Skipped \u2014 no Prisma schema or config",
|
|
487
|
+
fixable: false
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
const helperPath = findHelperFile(ctx.cwd);
|
|
491
|
+
if (!helperPath) {
|
|
492
|
+
const provider = extractProvider(ctx.cwd) || "postgresql";
|
|
493
|
+
const providerData = getProviderData(provider);
|
|
494
|
+
return {
|
|
495
|
+
id: "database:helper-file",
|
|
496
|
+
name: DATABASE_CHECKS["database:helper-file"].name,
|
|
497
|
+
category: "database",
|
|
498
|
+
severity: "warn",
|
|
499
|
+
message: "Prisma helper file not found",
|
|
500
|
+
detail: "Run 'dxg add database' to create the Prisma client helper (lib/prisma.ts)",
|
|
501
|
+
fixable: true,
|
|
502
|
+
fix: async () => {
|
|
503
|
+
await fixHelperFile(ctx.cwd, provider, providerData?.templateOutputPath || DEFAULT_HELPER_PATH);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
const content = readFileSync(helperPath, "utf-8");
|
|
509
|
+
if (!isValidPrismaClientHelper(content)) {
|
|
510
|
+
return {
|
|
511
|
+
id: "database:helper-file",
|
|
512
|
+
name: DATABASE_CHECKS["database:helper-file"].name,
|
|
513
|
+
category: "database",
|
|
514
|
+
severity: "warn",
|
|
515
|
+
message: "Prisma helper exists but does not match expected pattern",
|
|
516
|
+
detail: `Check ${toDisplayPath(helperPath, ctx.cwd)} for valid PrismaClient helper structure`,
|
|
517
|
+
fixable: true,
|
|
518
|
+
fix: async () => {
|
|
519
|
+
const provider = extractProvider(ctx.cwd) || "postgresql";
|
|
520
|
+
const providerData = getProviderData(provider);
|
|
521
|
+
await fixHelperFile(ctx.cwd, provider, providerData?.templateOutputPath || DEFAULT_HELPER_PATH);
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
} catch {
|
|
526
|
+
}
|
|
527
|
+
return {
|
|
528
|
+
id: "database:helper-file",
|
|
529
|
+
name: DATABASE_CHECKS["database:helper-file"].name,
|
|
530
|
+
category: "database",
|
|
531
|
+
severity: "ok",
|
|
532
|
+
message: `Prisma helper found at ${toDisplayPath(helperPath, ctx.cwd)}`,
|
|
533
|
+
fixable: false
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
async function fixHelperFile(cwd, provider, helperPath) {
|
|
537
|
+
const { writeFile, mkdir } = await import("@dxgjs/fs");
|
|
538
|
+
const isSqlite = provider === "sqlite";
|
|
539
|
+
const isMySQL = provider === "mysql";
|
|
540
|
+
const isSqlServer = provider === "sqlserver";
|
|
541
|
+
const isPlanetScale = provider === "planetscale";
|
|
542
|
+
let template;
|
|
543
|
+
if (isSqlite) {
|
|
544
|
+
template = generateSqliteHelper();
|
|
545
|
+
} else if (isMySQL) {
|
|
546
|
+
template = generateMySQLHelper();
|
|
547
|
+
} else if (isSqlServer) {
|
|
548
|
+
template = generateSQLServerHelper();
|
|
549
|
+
} else if (isPlanetScale) {
|
|
550
|
+
template = generatePlanetScaleHelper();
|
|
551
|
+
} else {
|
|
552
|
+
template = generateDefaultHelper();
|
|
553
|
+
}
|
|
554
|
+
const fullHelperPath = join(cwd, helperPath);
|
|
555
|
+
const helperDirPath = dirname(fullHelperPath);
|
|
556
|
+
await mkdir(helperDirPath, { recursive: true });
|
|
557
|
+
await writeFile(fullHelperPath, template, "utf-8");
|
|
558
|
+
}
|
|
559
|
+
function generateDefaultHelper() {
|
|
560
|
+
return `import { PrismaClient } from "@prisma/client";
|
|
561
|
+
import { PrismaPg } from "@prisma/adapter-pg";
|
|
562
|
+
import { Pool } from "pg";
|
|
563
|
+
|
|
564
|
+
const globalForPrisma = globalThis as unknown as {
|
|
565
|
+
prisma: PrismaClient | undefined;
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
569
|
+
const adapter = new PrismaPg(pool);
|
|
570
|
+
|
|
571
|
+
export const prisma =
|
|
572
|
+
globalForPrisma.prisma ??
|
|
573
|
+
new PrismaClient({
|
|
574
|
+
adapter,
|
|
575
|
+
log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
|
579
|
+
`;
|
|
580
|
+
}
|
|
581
|
+
function generateSqliteHelper() {
|
|
582
|
+
return `import { PrismaClient } from "@prisma/client";
|
|
583
|
+
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
|
|
584
|
+
import Database from "better-sqlite3";
|
|
585
|
+
|
|
586
|
+
const globalForPrisma = globalThis as unknown as {
|
|
587
|
+
prisma: PrismaClient | undefined;
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
const sqlite = new Database(process.env.DATABASE_URL?.replace("file:", "") ?? "./dev.db");
|
|
591
|
+
const adapter = new PrismaBetterSqlite3(sqlite);
|
|
592
|
+
|
|
593
|
+
export const prisma =
|
|
594
|
+
globalForPrisma.prisma ??
|
|
595
|
+
new PrismaClient({
|
|
596
|
+
adapter,
|
|
597
|
+
log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
|
601
|
+
`;
|
|
602
|
+
}
|
|
603
|
+
function generateMySQLHelper() {
|
|
604
|
+
return `import { PrismaClient } from "@prisma/client";
|
|
605
|
+
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
|
|
606
|
+
import { createPool } from "mariadb";
|
|
607
|
+
|
|
608
|
+
const globalForPrisma = globalThis as unknown as {
|
|
609
|
+
prisma: PrismaClient | undefined;
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
const pool = createPool({
|
|
613
|
+
host: process.env.DATABASE_HOST,
|
|
614
|
+
port: Number(process.env.DATABASE_PORT ?? "3306"),
|
|
615
|
+
user: process.env.DATABASE_USER,
|
|
616
|
+
password: process.env.DATABASE_PASSWORD,
|
|
617
|
+
database: process.env.DATABASE_NAME,
|
|
618
|
+
ssl: process.env.DATABASE_SSL === "true" ? { rejectUnauthorized: true } : undefined,
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
const adapter = new PrismaMariaDb(pool);
|
|
622
|
+
|
|
623
|
+
export const prisma =
|
|
624
|
+
globalForPrisma.prisma ??
|
|
625
|
+
new PrismaClient({
|
|
626
|
+
adapter,
|
|
627
|
+
log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
|
631
|
+
`;
|
|
632
|
+
}
|
|
633
|
+
function generateSQLServerHelper() {
|
|
634
|
+
return `import { PrismaClient } from "@prisma/client";
|
|
635
|
+
import { PrismaMssql } from "@prisma/adapter-mssql";
|
|
636
|
+
import sql from "mssql";
|
|
637
|
+
|
|
638
|
+
const globalForPrisma = globalThis as unknown as {
|
|
639
|
+
prisma: PrismaClient | undefined;
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
const pool = new sql.ConnectionPool({
|
|
643
|
+
server: process.env.DATABASE_HOST,
|
|
644
|
+
port: Number(process.env.DATABASE_PORT ?? "1433"),
|
|
645
|
+
user: process.env.DATABASE_USER,
|
|
646
|
+
password: process.env.DATABASE_PASSWORD,
|
|
647
|
+
database: process.env.DATABASE_NAME,
|
|
648
|
+
options: {
|
|
649
|
+
encrypt: process.env.DATABASE_SSL === "true",
|
|
650
|
+
trustServerCertificate: true,
|
|
651
|
+
},
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
const adapter = new PrismaMssql(pool);
|
|
655
|
+
|
|
656
|
+
export const prisma =
|
|
657
|
+
globalForPrisma.prisma ??
|
|
658
|
+
new PrismaClient({
|
|
659
|
+
adapter,
|
|
660
|
+
log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
|
664
|
+
`;
|
|
665
|
+
}
|
|
666
|
+
function generatePlanetScaleHelper() {
|
|
667
|
+
return `import { PrismaClient } from "@prisma/client";
|
|
668
|
+
import { PrismaPlanetScale } from "@prisma/adapter-planetscale";
|
|
669
|
+
import { createPool } from "@planetscale/database";
|
|
670
|
+
|
|
671
|
+
const globalForPrisma = globalThis as unknown as {
|
|
672
|
+
prisma: PrismaClient | undefined;
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
const pool = createPool({
|
|
676
|
+
host: process.env.DATABASE_HOST,
|
|
677
|
+
username: process.env.DATABASE_USER,
|
|
678
|
+
password: process.env.DATABASE_PASSWORD,
|
|
679
|
+
// PlanetScale uses fetch under the hood
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
const adapter = new PrismaPlanetScale(pool);
|
|
683
|
+
|
|
684
|
+
export const prisma =
|
|
685
|
+
globalForPrisma.prisma ??
|
|
686
|
+
new PrismaClient({
|
|
687
|
+
adapter,
|
|
688
|
+
log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
|
692
|
+
`;
|
|
693
|
+
}
|
|
694
|
+
async function checkSqliteDbFile(ctx) {
|
|
695
|
+
const provider = extractProvider(ctx.cwd);
|
|
696
|
+
const schemaPath = findPrismaSchema(ctx.cwd);
|
|
697
|
+
const configPath = findPrismaConfig(ctx.cwd);
|
|
698
|
+
if (!schemaPath && !configPath) {
|
|
699
|
+
return [{
|
|
700
|
+
id: "database:sqlite-db-file",
|
|
701
|
+
name: DATABASE_CHECKS["database:sqlite-db-file"].name,
|
|
702
|
+
category: "database",
|
|
703
|
+
severity: "info",
|
|
704
|
+
message: "Skipped \u2014 no Prisma schema or config",
|
|
705
|
+
fixable: false
|
|
706
|
+
}];
|
|
707
|
+
}
|
|
708
|
+
const sqliteProviderData = getProviderData("sqlite");
|
|
709
|
+
if (!sqliteProviderData || provider !== sqliteProviderData.key) {
|
|
710
|
+
return [];
|
|
711
|
+
}
|
|
712
|
+
const baseDir = schemaPath ? dirname(schemaPath) : dirname(configPath);
|
|
713
|
+
const dbPath = join(baseDir, "dev.db");
|
|
714
|
+
if (!existsSync(dbPath)) {
|
|
715
|
+
return [{
|
|
716
|
+
id: "database:sqlite-db-file",
|
|
717
|
+
name: DATABASE_CHECKS["database:sqlite-db-file"].name,
|
|
718
|
+
category: "database",
|
|
719
|
+
severity: "warn",
|
|
720
|
+
message: "SQLite database file not found",
|
|
721
|
+
detail: "Run 'pnpm prisma db push' to create the database (script added by generator)",
|
|
722
|
+
fixable: false
|
|
723
|
+
// User runs this
|
|
724
|
+
}];
|
|
725
|
+
}
|
|
726
|
+
return [{
|
|
727
|
+
id: "database:sqlite-db-file",
|
|
728
|
+
name: DATABASE_CHECKS["database:sqlite-db-file"].name,
|
|
729
|
+
category: "database",
|
|
730
|
+
severity: "ok",
|
|
731
|
+
message: "SQLite database file exists (prisma/dev.db)",
|
|
732
|
+
fixable: false
|
|
733
|
+
}];
|
|
734
|
+
}
|
|
735
|
+
async function checkSeedConfig(ctx) {
|
|
736
|
+
const configPath = findPrismaConfig(ctx.cwd);
|
|
737
|
+
const schemaPath = findPrismaSchema(ctx.cwd);
|
|
738
|
+
if (!configPath && !schemaPath) {
|
|
739
|
+
return {
|
|
740
|
+
id: "database:seed-config",
|
|
741
|
+
name: DATABASE_CHECKS["database:seed-config"].name,
|
|
742
|
+
category: "database",
|
|
743
|
+
severity: "info",
|
|
744
|
+
message: "Skipped \u2014 no Prisma schema or config",
|
|
745
|
+
fixable: false
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
const pkgPath = join(ctx.cwd, "package.json");
|
|
749
|
+
let hasSeedScript = false;
|
|
750
|
+
if (existsSync(pkgPath)) {
|
|
751
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
752
|
+
hasSeedScript = !!pkg.scripts?.[SEED_SCRIPT_NAME];
|
|
753
|
+
}
|
|
754
|
+
if (!hasSeedScript) {
|
|
755
|
+
return {
|
|
756
|
+
id: "database:seed-config",
|
|
757
|
+
name: DATABASE_CHECKS["database:seed-config"].name,
|
|
758
|
+
category: "database",
|
|
759
|
+
severity: "info",
|
|
760
|
+
message: "Skipped \u2014 db:seed script not configured",
|
|
761
|
+
detail: "Generator adds seed config when db:seed script is selected",
|
|
762
|
+
fixable: false
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
if (!configPath) {
|
|
766
|
+
return {
|
|
767
|
+
id: "database:seed-config",
|
|
768
|
+
name: DATABASE_CHECKS["database:seed-config"].name,
|
|
769
|
+
category: "database",
|
|
770
|
+
severity: "warn",
|
|
771
|
+
message: "db:seed script present but prisma7.config.ts not found",
|
|
772
|
+
detail: "Run 'dxg add database' to create Prisma config with seed",
|
|
773
|
+
fixable: false
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
try {
|
|
777
|
+
const content = readFileSync(configPath, "utf-8");
|
|
778
|
+
const hasSeed = new RegExp(`seed\\s*:\\s*["']${SEED_CONFIG_COMMAND.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}["']`).test(content);
|
|
779
|
+
if (!hasSeed) {
|
|
780
|
+
return {
|
|
781
|
+
id: "database:seed-config",
|
|
782
|
+
name: DATABASE_CHECKS["database:seed-config"].name,
|
|
783
|
+
category: "database",
|
|
784
|
+
severity: "warn",
|
|
785
|
+
message: "db:seed script configured but migrations.seed missing in prisma7.config.ts",
|
|
786
|
+
detail: "Run 'dxg add database' to add seed configuration",
|
|
787
|
+
fixable: false
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
return {
|
|
791
|
+
id: "database:seed-config",
|
|
792
|
+
name: DATABASE_CHECKS["database:seed-config"].name,
|
|
793
|
+
category: "database",
|
|
794
|
+
severity: "ok",
|
|
795
|
+
message: "Seed configuration present in prisma7.config.ts",
|
|
796
|
+
fixable: false
|
|
797
|
+
};
|
|
798
|
+
} catch {
|
|
799
|
+
return {
|
|
800
|
+
id: "database:seed-config",
|
|
801
|
+
name: DATABASE_CHECKS["database:seed-config"].name,
|
|
802
|
+
category: "database",
|
|
803
|
+
severity: "error",
|
|
804
|
+
message: "Failed to read prisma7.config.ts",
|
|
805
|
+
fixable: false
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
async function checkConnectivity(ctx) {
|
|
810
|
+
const schemaPath = findPrismaSchema(ctx.cwd);
|
|
811
|
+
const configPath = findPrismaConfig(ctx.cwd);
|
|
812
|
+
if (!schemaPath && !configPath) {
|
|
813
|
+
return {
|
|
814
|
+
id: "database:connectivity",
|
|
815
|
+
name: DATABASE_CHECKS["database:connectivity"].name,
|
|
816
|
+
category: "database",
|
|
817
|
+
severity: "info",
|
|
818
|
+
message: "Skipped \u2014 no Prisma schema or config",
|
|
819
|
+
fixable: false
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
const envPath = join(ctx.cwd, ".env");
|
|
823
|
+
let databaseUrl = "";
|
|
824
|
+
let isFileBased = false;
|
|
825
|
+
if (existsSync(envPath)) {
|
|
826
|
+
const content = readFileSync(envPath, "utf-8");
|
|
827
|
+
const lines = content.split("\n");
|
|
828
|
+
const databaseUrlLine = lines.find(
|
|
829
|
+
(line) => line.trim().startsWith("DATABASE_URL")
|
|
830
|
+
);
|
|
831
|
+
if (databaseUrlLine) {
|
|
832
|
+
databaseUrl = databaseUrlLine.split("=")[1]?.trim().replace(/^["']|["']$/g, "") || "";
|
|
833
|
+
isFileBased = databaseUrl.startsWith("file:") || databaseUrl.startsWith("sqlite:");
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
if (!databaseUrl) {
|
|
837
|
+
return {
|
|
838
|
+
id: "database:connectivity",
|
|
839
|
+
name: DATABASE_CHECKS["database:connectivity"].name,
|
|
840
|
+
category: "database",
|
|
841
|
+
severity: "info",
|
|
842
|
+
message: "Skipped \u2014 DATABASE_URL not set in .env",
|
|
843
|
+
detail: "Configure DATABASE_URL to enable connectivity check",
|
|
844
|
+
fixable: false
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
if (isFileBased) {
|
|
848
|
+
return {
|
|
849
|
+
id: "database:connectivity",
|
|
850
|
+
name: DATABASE_CHECKS["database:connectivity"].name,
|
|
851
|
+
category: "database",
|
|
852
|
+
severity: "info",
|
|
853
|
+
message: "Skipped \u2014 file-based database (SQLite)",
|
|
854
|
+
detail: "Connectivity check only for server databases",
|
|
855
|
+
fixable: false
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
const TIMEOUT_MS = 4e3;
|
|
859
|
+
try {
|
|
860
|
+
const { executeCommand } = await import("@dxgjs/fs");
|
|
861
|
+
const result = await executeCommand("pnpm", ["prisma", "db", "execute", "--stdin"], {
|
|
862
|
+
cwd: ctx.cwd,
|
|
863
|
+
timeout: TIMEOUT_MS,
|
|
864
|
+
// Strict stdio piping prevents pnpm lockfile validation logs
|
|
865
|
+
// and other side-channel output from corrupting the CLI layout
|
|
866
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
867
|
+
input: "SELECT 1;",
|
|
868
|
+
killDescendants: true
|
|
869
|
+
// Ensure full process tree cleanup on timeout
|
|
870
|
+
});
|
|
871
|
+
if (result.exitCode === 0) {
|
|
872
|
+
return {
|
|
873
|
+
id: "database:connectivity",
|
|
874
|
+
name: DATABASE_CHECKS["database:connectivity"].name,
|
|
875
|
+
category: "database",
|
|
876
|
+
severity: "ok",
|
|
877
|
+
message: "Database connection established successfully",
|
|
878
|
+
fixable: false
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
return {
|
|
882
|
+
id: "database:connectivity",
|
|
883
|
+
name: DATABASE_CHECKS["database:connectivity"].name,
|
|
884
|
+
category: "database",
|
|
885
|
+
severity: "warn",
|
|
886
|
+
message: "Database connection failed",
|
|
887
|
+
detail: typeof result.stderr === "string" ? result.stderr.trim() : "An unknown database connection error occurred.",
|
|
888
|
+
fixable: false
|
|
889
|
+
};
|
|
890
|
+
} catch (error) {
|
|
891
|
+
const execaError = error;
|
|
892
|
+
if (execaError.code === "ETIMEDOUT" || execaError.timedOut) {
|
|
893
|
+
return {
|
|
894
|
+
id: "database:connectivity",
|
|
895
|
+
name: DATABASE_CHECKS["database:connectivity"].name,
|
|
896
|
+
category: "database",
|
|
897
|
+
severity: "warn",
|
|
898
|
+
message: "Connection failed: timeout",
|
|
899
|
+
detail: `The database did not respond within ${TIMEOUT_MS}ms. Ensure your instance is running and reachable.`,
|
|
900
|
+
fixable: false
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
return {
|
|
904
|
+
id: "database:connectivity",
|
|
905
|
+
name: DATABASE_CHECKS["database:connectivity"].name,
|
|
906
|
+
category: "database",
|
|
907
|
+
severity: "warn",
|
|
908
|
+
message: "Database connection failed",
|
|
909
|
+
detail: execaError.message || "An unknown database connection error occurred.",
|
|
910
|
+
fixable: false
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
async function registerDatabaseChecks() {
|
|
915
|
+
const { registerCheck: registerCheck2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
916
|
+
registerCheck2("database:prisma-schema", checkPrismaSchema, "database");
|
|
917
|
+
registerCheck2("database:prisma-config", checkPrismaConfig, "database");
|
|
918
|
+
registerCheck2("database:provider", checkProvider, "database");
|
|
919
|
+
registerCheck2("database:prisma-client", checkPrismaClient, "database");
|
|
920
|
+
registerCheck2("database:helper-file", checkHelperFile, "database");
|
|
921
|
+
registerCheck2("database:sqlite-db-file", checkSqliteDbFile, "database");
|
|
922
|
+
registerCheck2("database:seed-config", checkSeedConfig, "database");
|
|
923
|
+
registerCheck2("database:connectivity", checkConnectivity, "database");
|
|
924
|
+
}
|
|
925
|
+
var DATABASE_CHECKS;
|
|
926
|
+
var init_database = __esm({
|
|
927
|
+
"src/checks/database.ts"() {
|
|
928
|
+
"use strict";
|
|
929
|
+
init_paths();
|
|
930
|
+
DATABASE_CHECKS = {
|
|
931
|
+
"database:prisma-schema": {
|
|
932
|
+
name: "Prisma schema exists",
|
|
933
|
+
category: "database"
|
|
934
|
+
},
|
|
935
|
+
"database:prisma-config": {
|
|
936
|
+
name: "Prisma config exists",
|
|
937
|
+
category: "database"
|
|
938
|
+
},
|
|
939
|
+
"database:provider": {
|
|
940
|
+
name: "Database provider configured",
|
|
941
|
+
category: "database"
|
|
942
|
+
},
|
|
943
|
+
"database:prisma-client": {
|
|
944
|
+
name: "Prisma Client generated",
|
|
945
|
+
category: "database"
|
|
946
|
+
},
|
|
947
|
+
"database:helper-file": {
|
|
948
|
+
name: "Prisma helper file exists",
|
|
949
|
+
category: "database"
|
|
950
|
+
},
|
|
951
|
+
"database:sqlite-db-file": {
|
|
952
|
+
name: "SQLite database file exists",
|
|
953
|
+
category: "database"
|
|
954
|
+
},
|
|
955
|
+
"database:seed-config": {
|
|
956
|
+
name: "Seed configuration present",
|
|
957
|
+
category: "database"
|
|
958
|
+
},
|
|
959
|
+
"database:connectivity": {
|
|
960
|
+
name: "Database connectivity",
|
|
961
|
+
category: "database"
|
|
962
|
+
}
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
// src/checks/env.ts
|
|
968
|
+
var env_exports = {};
|
|
969
|
+
__export(env_exports, {
|
|
970
|
+
registerEnvChecks: () => registerEnvChecks
|
|
971
|
+
});
|
|
972
|
+
import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
|
|
973
|
+
import { join as join2 } from "path";
|
|
974
|
+
import { ensureGitignoreEntry } from "@dxgjs/ast";
|
|
975
|
+
import {
|
|
976
|
+
providerPlaceholders,
|
|
977
|
+
getProviderData as getProviderData2,
|
|
978
|
+
isSupportedProvider as isSupportedProvider2,
|
|
979
|
+
extractProviderFromSchema as extractProviderFromSchema2,
|
|
980
|
+
extractProviderFromConfig as extractProviderFromConfig2,
|
|
981
|
+
normalizePrismaProvider as normalizePrismaProvider2
|
|
982
|
+
} from "@dxgjs/database-meta";
|
|
983
|
+
import { SCHEMA_PATH as SCHEMA_PATH2, PRISMA_CONFIG_PATH as PRISMA_CONFIG_PATH2 } from "@dxgjs/database-meta";
|
|
984
|
+
function findPrismaSchema2(cwd) {
|
|
985
|
+
const possiblePaths = [
|
|
986
|
+
join2(cwd, SCHEMA_PATH2),
|
|
987
|
+
join2(cwd, "packages", "database", SCHEMA_PATH2),
|
|
988
|
+
join2(cwd, "apps", "api", SCHEMA_PATH2)
|
|
989
|
+
];
|
|
990
|
+
for (const path of possiblePaths) {
|
|
991
|
+
if (existsSync2(path)) return path;
|
|
992
|
+
}
|
|
993
|
+
return null;
|
|
994
|
+
}
|
|
995
|
+
function findPrismaConfig2(cwd) {
|
|
996
|
+
const possiblePaths = [
|
|
997
|
+
join2(cwd, PRISMA_CONFIG_PATH2),
|
|
998
|
+
join2(cwd, "prisma", PRISMA_CONFIG_PATH2)
|
|
999
|
+
];
|
|
1000
|
+
for (const path of possiblePaths) {
|
|
1001
|
+
if (existsSync2(path)) return path;
|
|
1002
|
+
}
|
|
1003
|
+
return null;
|
|
1004
|
+
}
|
|
1005
|
+
function extractProvider2(cwd) {
|
|
1006
|
+
const schemaPath = findPrismaSchema2(cwd);
|
|
1007
|
+
if (schemaPath) {
|
|
1008
|
+
try {
|
|
1009
|
+
const content = readFileSync2(schemaPath, "utf-8");
|
|
1010
|
+
const provider = extractProviderFromSchema2(content);
|
|
1011
|
+
if (provider) return normalizePrismaProvider2(provider);
|
|
1012
|
+
} catch {
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
const configPath = findPrismaConfig2(cwd);
|
|
1016
|
+
if (configPath) {
|
|
1017
|
+
try {
|
|
1018
|
+
const content = readFileSync2(configPath, "utf-8");
|
|
1019
|
+
const provider = extractProviderFromConfig2(content);
|
|
1020
|
+
if (provider) return normalizePrismaProvider2(provider);
|
|
1021
|
+
} catch {
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
return null;
|
|
1025
|
+
}
|
|
1026
|
+
async function checkDotenvExists(ctx) {
|
|
1027
|
+
const envPath = join2(ctx.cwd, ".env");
|
|
1028
|
+
const { existsSync: existsSyncFs } = await import("fs");
|
|
1029
|
+
const { join: joinPath } = await import("path");
|
|
1030
|
+
const possibleSchemaPaths = [
|
|
1031
|
+
joinPath(ctx.cwd, "prisma", "schema.prisma"),
|
|
1032
|
+
joinPath(ctx.cwd, "packages", "database", "prisma", "schema.prisma"),
|
|
1033
|
+
joinPath(ctx.cwd, "apps", "api", "prisma", "schema.prisma")
|
|
1034
|
+
];
|
|
1035
|
+
const hasPrismaSchema = possibleSchemaPaths.some((p) => existsSyncFs(p));
|
|
1036
|
+
const examplePath = join2(ctx.cwd, ".env.example");
|
|
1037
|
+
const hasExample = existsSyncFs(examplePath);
|
|
1038
|
+
if (!existsSync2(envPath)) {
|
|
1039
|
+
if (hasPrismaSchema || hasExample) {
|
|
1040
|
+
return {
|
|
1041
|
+
id: "env:dotenv-exists",
|
|
1042
|
+
name: ENV_CHECKS["env:dotenv-exists"].name,
|
|
1043
|
+
category: "env",
|
|
1044
|
+
severity: "warn",
|
|
1045
|
+
message: "No .env file found",
|
|
1046
|
+
detail: hasExample ? "Create .env from .env.example to configure environment variables" : "Database generator will create .env when needed",
|
|
1047
|
+
fixable: true,
|
|
1048
|
+
fix: async () => {
|
|
1049
|
+
if (existsSync2(examplePath)) {
|
|
1050
|
+
const content = readFileSync2(examplePath, "utf-8");
|
|
1051
|
+
await ctx.fs.writeFile(envPath, content);
|
|
1052
|
+
} else {
|
|
1053
|
+
await ctx.fs.writeFile(envPath, '# Environment variables\nDATABASE_URL=""\n');
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
return {
|
|
1059
|
+
id: "env:dotenv-exists",
|
|
1060
|
+
name: ENV_CHECKS["env:dotenv-exists"].name,
|
|
1061
|
+
category: "env",
|
|
1062
|
+
severity: "info",
|
|
1063
|
+
message: ".env file not found (optional)",
|
|
1064
|
+
detail: "Create .env if you need environment variables",
|
|
1065
|
+
fixable: false
|
|
1066
|
+
};
|
|
1067
|
+
}
|
|
1068
|
+
return {
|
|
1069
|
+
id: "env:dotenv-exists",
|
|
1070
|
+
name: ENV_CHECKS["env:dotenv-exists"].name,
|
|
1071
|
+
category: "env",
|
|
1072
|
+
severity: "ok",
|
|
1073
|
+
message: ".env file exists",
|
|
1074
|
+
fixable: false
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
async function checkDatabaseUrl(ctx) {
|
|
1078
|
+
const envPath = join2(ctx.cwd, ".env");
|
|
1079
|
+
if (!existsSync2(envPath)) {
|
|
1080
|
+
return {
|
|
1081
|
+
id: "env:database-url",
|
|
1082
|
+
name: ENV_CHECKS["env:database-url"].name,
|
|
1083
|
+
category: "env",
|
|
1084
|
+
severity: "info",
|
|
1085
|
+
message: "Cannot check DATABASE_URL \u2014 no .env file",
|
|
1086
|
+
detail: "Run dxg add database to create .env with appropriate DATABASE_URL",
|
|
1087
|
+
fixable: false
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
const content = readFileSync2(envPath, "utf-8");
|
|
1091
|
+
const lines = content.split("\n");
|
|
1092
|
+
const databaseUrlLine = lines.find(
|
|
1093
|
+
(line) => line.trim().startsWith("DATABASE_URL")
|
|
1094
|
+
);
|
|
1095
|
+
if (!databaseUrlLine) {
|
|
1096
|
+
return {
|
|
1097
|
+
id: "env:database-url",
|
|
1098
|
+
name: ENV_CHECKS["env:database-url"].name,
|
|
1099
|
+
category: "env",
|
|
1100
|
+
severity: "warn",
|
|
1101
|
+
message: "DATABASE_URL not set in .env",
|
|
1102
|
+
detail: "Run dxg add database to configure DATABASE_URL for your provider",
|
|
1103
|
+
fixable: false
|
|
1104
|
+
// Generator handles this, not doctor
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
const value = databaseUrlLine.split("=")[1]?.trim().replace(/^["']|["']$/g, "");
|
|
1108
|
+
if (!value || value === '""' || value === "''") {
|
|
1109
|
+
return {
|
|
1110
|
+
id: "env:database-url",
|
|
1111
|
+
name: ENV_CHECKS["env:database-url"].name,
|
|
1112
|
+
category: "env",
|
|
1113
|
+
severity: "warn",
|
|
1114
|
+
message: "DATABASE_URL is empty in .env",
|
|
1115
|
+
detail: "For SQLite: use file:./dev.db. For server providers: add your connection string",
|
|
1116
|
+
fixable: false
|
|
1117
|
+
// User must provide real connection string
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
return {
|
|
1121
|
+
id: "env:database-url",
|
|
1122
|
+
name: ENV_CHECKS["env:database-url"].name,
|
|
1123
|
+
category: "env",
|
|
1124
|
+
severity: "ok",
|
|
1125
|
+
message: "DATABASE_URL is configured",
|
|
1126
|
+
fixable: false
|
|
1127
|
+
};
|
|
1128
|
+
}
|
|
1129
|
+
async function checkDatabaseUrlFormat(ctx) {
|
|
1130
|
+
const envPath = join2(ctx.cwd, ".env");
|
|
1131
|
+
if (!existsSync2(envPath)) {
|
|
1132
|
+
return {
|
|
1133
|
+
id: "env:database-url-format",
|
|
1134
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1135
|
+
category: "env",
|
|
1136
|
+
severity: "info",
|
|
1137
|
+
message: "Skipped \u2014 no .env file",
|
|
1138
|
+
fixable: false
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
const content = readFileSync2(envPath, "utf-8");
|
|
1142
|
+
const lines = content.split("\n");
|
|
1143
|
+
const databaseUrlLine = lines.find(
|
|
1144
|
+
(line) => line.trim().startsWith("DATABASE_URL")
|
|
1145
|
+
);
|
|
1146
|
+
if (!databaseUrlLine) {
|
|
1147
|
+
return {
|
|
1148
|
+
id: "env:database-url-format",
|
|
1149
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1150
|
+
category: "env",
|
|
1151
|
+
severity: "info",
|
|
1152
|
+
message: "Skipped \u2014 DATABASE_URL not set",
|
|
1153
|
+
fixable: false
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
const value = databaseUrlLine.split("=")[1]?.trim().replace(/^["']|["']$/g, "");
|
|
1157
|
+
if (!value) {
|
|
1158
|
+
return {
|
|
1159
|
+
id: "env:database-url-format",
|
|
1160
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1161
|
+
category: "env",
|
|
1162
|
+
severity: "info",
|
|
1163
|
+
message: "Skipped \u2014 DATABASE_URL is empty",
|
|
1164
|
+
fixable: false
|
|
1165
|
+
};
|
|
1166
|
+
}
|
|
1167
|
+
const provider = extractProvider2(ctx.cwd);
|
|
1168
|
+
const providerData = provider && isSupportedProvider2(provider) ? getProviderData2(provider) : null;
|
|
1169
|
+
try {
|
|
1170
|
+
const url = new URL(value);
|
|
1171
|
+
if (providerData) {
|
|
1172
|
+
const expectedProtocol = providerData.instantiationPattern === "connectionString" ? providerData.prismaProvider === "postgresql" ? "postgresql:" : providerData.prismaProvider === "mysql" ? "mysql:" : providerData.prismaProvider === "sqlite" ? "file:" : "postgresql:" : null;
|
|
1173
|
+
if (providerData.key === "sqlite") {
|
|
1174
|
+
if (url.protocol !== "file:" && url.protocol !== "sqlite:") {
|
|
1175
|
+
return {
|
|
1176
|
+
id: "env:database-url-format",
|
|
1177
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1178
|
+
category: "env",
|
|
1179
|
+
severity: "warn",
|
|
1180
|
+
message: `DATABASE_URL protocol "${url.protocol}" does not match SQLite provider`,
|
|
1181
|
+
detail: `Expected file: or sqlite: for SQLite provider. Placeholder: ${providerPlaceholders.sqlite}`,
|
|
1182
|
+
fixable: false
|
|
1183
|
+
};
|
|
1184
|
+
}
|
|
1185
|
+
} else if (expectedProtocol && url.protocol !== expectedProtocol) {
|
|
1186
|
+
const isPostgres = providerData.prismaProvider === "postgresql";
|
|
1187
|
+
const protocolMatches = isPostgres ? url.protocol === "postgresql:" || url.protocol === "postgres:" : url.protocol === expectedProtocol;
|
|
1188
|
+
if (!protocolMatches) {
|
|
1189
|
+
return {
|
|
1190
|
+
id: "env:database-url-format",
|
|
1191
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1192
|
+
category: "env",
|
|
1193
|
+
severity: "warn",
|
|
1194
|
+
message: `DATABASE_URL protocol "${url.protocol}" may not match ${providerData.displayName} provider`,
|
|
1195
|
+
detail: `Expected ${expectedProtocol} for ${providerData.displayName}. Placeholder: ${providerPlaceholders[providerData.key]}`,
|
|
1196
|
+
fixable: false
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
} else {
|
|
1201
|
+
const validProtocols = ["postgresql:", "postgres:", "mysql:", "sqlite:", "file:"];
|
|
1202
|
+
if (!validProtocols.includes(url.protocol)) {
|
|
1203
|
+
return {
|
|
1204
|
+
id: "env:database-url-format",
|
|
1205
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1206
|
+
category: "env",
|
|
1207
|
+
severity: "warn",
|
|
1208
|
+
message: `DATABASE_URL protocol "${url.protocol}" may not be supported`,
|
|
1209
|
+
detail: "Supported: postgresql://, postgres://, mysql://, sqlite://, file://",
|
|
1210
|
+
fixable: false
|
|
1211
|
+
};
|
|
1212
|
+
}
|
|
1213
|
+
if (url.protocol === "sqlite:" || url.protocol === "file:") {
|
|
1214
|
+
const dbPath = url.pathname;
|
|
1215
|
+
if (!dbPath || dbPath === "/") {
|
|
1216
|
+
return {
|
|
1217
|
+
id: "env:database-url-format",
|
|
1218
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1219
|
+
category: "env",
|
|
1220
|
+
severity: "error",
|
|
1221
|
+
message: "SQLite DATABASE_URL missing file path",
|
|
1222
|
+
detail: "Use file:./dev.db or sqlite:./dev.db",
|
|
1223
|
+
fixable: false
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
return {
|
|
1229
|
+
id: "env:database-url-format",
|
|
1230
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1231
|
+
category: "env",
|
|
1232
|
+
severity: "ok",
|
|
1233
|
+
message: "DATABASE_URL format is valid",
|
|
1234
|
+
fixable: false
|
|
1235
|
+
};
|
|
1236
|
+
} catch {
|
|
1237
|
+
return {
|
|
1238
|
+
id: "env:database-url-format",
|
|
1239
|
+
name: ENV_CHECKS["env:database-url-format"].name,
|
|
1240
|
+
category: "env",
|
|
1241
|
+
severity: "error",
|
|
1242
|
+
message: "DATABASE_URL is not a valid URL",
|
|
1243
|
+
detail: "Expected format: postgresql://user:pass@host:port/db",
|
|
1244
|
+
fixable: false
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
async function checkNoSecretsInGit(ctx) {
|
|
1249
|
+
const envPath = join2(ctx.cwd, ".env");
|
|
1250
|
+
const _gitignorePath = join2(ctx.cwd, ".gitignore");
|
|
1251
|
+
void _gitignorePath;
|
|
1252
|
+
try {
|
|
1253
|
+
const { executeCommand } = await import("@dxgjs/fs");
|
|
1254
|
+
const result = await executeCommand("git", ["ls-files", ".env"], { cwd: ctx.cwd });
|
|
1255
|
+
const stdout = String(result.stdout ?? "");
|
|
1256
|
+
if (stdout.trim() === ".env") {
|
|
1257
|
+
if (existsSync2(envPath)) {
|
|
1258
|
+
const content = readFileSync2(envPath, "utf-8");
|
|
1259
|
+
const hasRealSecrets = /DATABASE_URL\s*=\s*["']?postgresql:\/\/[^"'\s]+["']?/i.test(content) || /DATABASE_URL\s*=\s*["']?mysql:\/\/[^"'\s]+["']?/i.test(content) || /SECRET|TOKEN|KEY|PASSWORD/i.test(content);
|
|
1260
|
+
if (hasRealSecrets) {
|
|
1261
|
+
return {
|
|
1262
|
+
id: "env:no-secrets-in-git",
|
|
1263
|
+
name: ENV_CHECKS["env:no-secrets-in-git"].name,
|
|
1264
|
+
category: "env",
|
|
1265
|
+
severity: "error",
|
|
1266
|
+
message: ".env file is tracked by git and contains secrets",
|
|
1267
|
+
detail: "Add .env to .gitignore and rotate any exposed credentials",
|
|
1268
|
+
fixable: true,
|
|
1269
|
+
fix: async () => {
|
|
1270
|
+
await ensureGitignoreEntry(ctx.fs, ".env");
|
|
1271
|
+
}
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
return {
|
|
1276
|
+
id: "env:no-secrets-in-git",
|
|
1277
|
+
name: ENV_CHECKS["env:no-secrets-in-git"].name,
|
|
1278
|
+
category: "env",
|
|
1279
|
+
severity: "warn",
|
|
1280
|
+
message: ".env file is tracked by git",
|
|
1281
|
+
detail: "Add .env to .gitignore to prevent committing secrets",
|
|
1282
|
+
fixable: true,
|
|
1283
|
+
fix: async () => {
|
|
1284
|
+
await ensureGitignoreEntry(ctx.fs, ".env");
|
|
1285
|
+
}
|
|
1286
|
+
};
|
|
1287
|
+
}
|
|
1288
|
+
} catch {
|
|
1289
|
+
}
|
|
1290
|
+
return {
|
|
1291
|
+
id: "env:no-secrets-in-git",
|
|
1292
|
+
name: ENV_CHECKS["env:no-secrets-in-git"].name,
|
|
1293
|
+
category: "env",
|
|
1294
|
+
severity: "ok",
|
|
1295
|
+
message: "No secrets found in git-tracked files",
|
|
1296
|
+
fixable: false
|
|
1297
|
+
};
|
|
1298
|
+
}
|
|
1299
|
+
async function registerEnvChecks() {
|
|
1300
|
+
const { registerCheck: registerCheck2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
1301
|
+
registerCheck2("env:dotenv-exists", checkDotenvExists, "env");
|
|
1302
|
+
registerCheck2("env:database-url", checkDatabaseUrl, "env");
|
|
1303
|
+
registerCheck2("env:database-url-format", checkDatabaseUrlFormat, "env");
|
|
1304
|
+
registerCheck2("env:no-secrets-in-git", checkNoSecretsInGit, "env");
|
|
1305
|
+
}
|
|
1306
|
+
var ENV_CHECKS;
|
|
1307
|
+
var init_env = __esm({
|
|
1308
|
+
"src/checks/env.ts"() {
|
|
1309
|
+
"use strict";
|
|
1310
|
+
ENV_CHECKS = {
|
|
1311
|
+
"env:dotenv-exists": {
|
|
1312
|
+
name: "Dotenv file exists",
|
|
1313
|
+
category: "env"
|
|
1314
|
+
},
|
|
1315
|
+
"env:database-url": {
|
|
1316
|
+
name: "DATABASE_URL configured",
|
|
1317
|
+
category: "env"
|
|
1318
|
+
},
|
|
1319
|
+
"env:database-url-format": {
|
|
1320
|
+
name: "DATABASE_URL format valid",
|
|
1321
|
+
category: "env"
|
|
1322
|
+
},
|
|
1323
|
+
"env:no-secrets-in-git": {
|
|
1324
|
+
name: "No secrets committed to git",
|
|
1325
|
+
category: "env"
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
});
|
|
1330
|
+
|
|
1331
|
+
// src/checks/project.ts
|
|
1332
|
+
var project_exports = {};
|
|
1333
|
+
__export(project_exports, {
|
|
1334
|
+
registerProjectChecks: () => registerProjectChecks
|
|
1335
|
+
});
|
|
1336
|
+
import { readFileSync as readFileSync3, existsSync as existsSync3 } from "fs";
|
|
1337
|
+
import { join as join3 } from "path";
|
|
1338
|
+
async function checkPackageJson(ctx) {
|
|
1339
|
+
const pkgPath = join3(ctx.cwd, "package.json");
|
|
1340
|
+
if (!existsSync3(pkgPath)) {
|
|
1341
|
+
return {
|
|
1342
|
+
id: "project:package-json",
|
|
1343
|
+
name: PROJECT_CHECKS["project:package-json"].name,
|
|
1344
|
+
category: "project",
|
|
1345
|
+
severity: "error",
|
|
1346
|
+
message: "package.json not found",
|
|
1347
|
+
detail: "Initialize your project (e.g., pnpm init) before running dxg add database",
|
|
1348
|
+
fixable: false
|
|
1349
|
+
};
|
|
1350
|
+
}
|
|
1351
|
+
try {
|
|
1352
|
+
const content = readFileSync3(pkgPath, "utf-8");
|
|
1353
|
+
JSON.parse(content);
|
|
1354
|
+
return {
|
|
1355
|
+
id: "project:package-json",
|
|
1356
|
+
name: PROJECT_CHECKS["project:package-json"].name,
|
|
1357
|
+
category: "project",
|
|
1358
|
+
severity: "ok",
|
|
1359
|
+
message: "package.json is valid",
|
|
1360
|
+
fixable: false
|
|
1361
|
+
};
|
|
1362
|
+
} catch {
|
|
1363
|
+
return {
|
|
1364
|
+
id: "project:package-json",
|
|
1365
|
+
name: PROJECT_CHECKS["project:package-json"].name,
|
|
1366
|
+
category: "project",
|
|
1367
|
+
severity: "error",
|
|
1368
|
+
message: "package.json is invalid JSON",
|
|
1369
|
+
detail: "Fix syntax errors in package.json",
|
|
1370
|
+
fixable: false
|
|
1371
|
+
};
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
async function checkGitignore(ctx) {
|
|
1375
|
+
const gitignorePath = join3(ctx.cwd, ".gitignore");
|
|
1376
|
+
if (!existsSync3(gitignorePath)) {
|
|
1377
|
+
return {
|
|
1378
|
+
id: "project:gitignore",
|
|
1379
|
+
name: PROJECT_CHECKS["project:gitignore"].name,
|
|
1380
|
+
category: "project",
|
|
1381
|
+
severity: "warn",
|
|
1382
|
+
message: ".gitignore not found",
|
|
1383
|
+
detail: "Create .gitignore to exclude .env (database generator adds this)",
|
|
1384
|
+
fixable: true,
|
|
1385
|
+
fix: async () => {
|
|
1386
|
+
const content2 = `.env
|
|
1387
|
+
`;
|
|
1388
|
+
await ctx.fs.writeFile(gitignorePath, content2);
|
|
1389
|
+
}
|
|
1390
|
+
};
|
|
1391
|
+
}
|
|
1392
|
+
const content = readFileSync3(gitignorePath, "utf-8");
|
|
1393
|
+
const lines = content.split(/\r?\n/);
|
|
1394
|
+
const coversEnv = lines.some(
|
|
1395
|
+
(line) => [".env", ".env*", "*.env"].includes(line.trimEnd())
|
|
1396
|
+
);
|
|
1397
|
+
const negatesEnv = lines.some((line) => line.trimEnd() === "!.env");
|
|
1398
|
+
if (negatesEnv) {
|
|
1399
|
+
return {
|
|
1400
|
+
id: "project:gitignore",
|
|
1401
|
+
name: PROJECT_CHECKS["project:gitignore"].name,
|
|
1402
|
+
category: "project",
|
|
1403
|
+
severity: "warn",
|
|
1404
|
+
message: ".gitignore has explicit !.env negation",
|
|
1405
|
+
detail: "User chose to track .env; generator will not modify",
|
|
1406
|
+
fixable: false
|
|
1407
|
+
};
|
|
1408
|
+
}
|
|
1409
|
+
if (!coversEnv) {
|
|
1410
|
+
return {
|
|
1411
|
+
id: "project:gitignore",
|
|
1412
|
+
name: PROJECT_CHECKS["project:gitignore"].name,
|
|
1413
|
+
category: "project",
|
|
1414
|
+
severity: "warn",
|
|
1415
|
+
message: ".gitignore missing .env pattern",
|
|
1416
|
+
detail: "Add .env to .gitignore (generator does this additively)",
|
|
1417
|
+
fixable: true,
|
|
1418
|
+
fix: async () => {
|
|
1419
|
+
const needsLeadingNewline = content.length > 0 && !content.endsWith("\n");
|
|
1420
|
+
await ctx.fs.writeFile(gitignorePath, `${content}${needsLeadingNewline ? "\n" : ""}.env
|
|
1421
|
+
`);
|
|
1422
|
+
}
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
return {
|
|
1426
|
+
id: "project:gitignore",
|
|
1427
|
+
name: PROJECT_CHECKS["project:gitignore"].name,
|
|
1428
|
+
category: "project",
|
|
1429
|
+
severity: "ok",
|
|
1430
|
+
message: ".gitignore covers .env",
|
|
1431
|
+
fixable: false
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
async function registerProjectChecks() {
|
|
1435
|
+
const { registerCheck: registerCheck2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
1436
|
+
registerCheck2("project:package-json", checkPackageJson, "project");
|
|
1437
|
+
registerCheck2("project:gitignore", checkGitignore, "project");
|
|
1438
|
+
}
|
|
1439
|
+
var PROJECT_CHECKS;
|
|
1440
|
+
var init_project = __esm({
|
|
1441
|
+
"src/checks/project.ts"() {
|
|
1442
|
+
"use strict";
|
|
1443
|
+
PROJECT_CHECKS = {
|
|
1444
|
+
"project:package-json": {
|
|
1445
|
+
name: "package.json exists",
|
|
1446
|
+
category: "project"
|
|
1447
|
+
},
|
|
1448
|
+
"project:gitignore": {
|
|
1449
|
+
name: ".gitignore configured",
|
|
1450
|
+
category: "project"
|
|
1451
|
+
}
|
|
1452
|
+
};
|
|
1453
|
+
}
|
|
1454
|
+
});
|
|
1455
|
+
|
|
1456
|
+
// src/checks/scripts.ts
|
|
1457
|
+
var scripts_exports = {};
|
|
1458
|
+
__export(scripts_exports, {
|
|
1459
|
+
registerScriptsChecks: () => registerScriptsChecks
|
|
1460
|
+
});
|
|
1461
|
+
import { readFileSync as readFileSync4, existsSync as existsSync4 } from "fs";
|
|
1462
|
+
import { join as join4 } from "path";
|
|
1463
|
+
import { addNpmScripts } from "@dxgjs/ast";
|
|
1464
|
+
import {
|
|
1465
|
+
databaseScriptCatalog,
|
|
1466
|
+
SEED_SCRIPT_NAME as SEED_SCRIPT_NAME2
|
|
1467
|
+
} from "@dxgjs/database-meta";
|
|
1468
|
+
async function checkPackageJsonScripts(ctx) {
|
|
1469
|
+
const pkgPath = join4(ctx.cwd, "package.json");
|
|
1470
|
+
if (!existsSync4(pkgPath)) {
|
|
1471
|
+
return {
|
|
1472
|
+
id: "scripts:package-json-scripts",
|
|
1473
|
+
name: SCRIPTS_CHECKS["scripts:package-json-scripts"].name,
|
|
1474
|
+
category: "scripts",
|
|
1475
|
+
severity: "error",
|
|
1476
|
+
message: "package.json not found",
|
|
1477
|
+
fixable: false
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
const pkg = JSON.parse(readFileSync4(pkgPath, "utf-8"));
|
|
1481
|
+
const scripts = pkg.scripts || {};
|
|
1482
|
+
const issues = [];
|
|
1483
|
+
for (const [name, script] of Object.entries(scripts)) {
|
|
1484
|
+
if (typeof script !== "string") {
|
|
1485
|
+
issues.push(`Script "${name}" is not a string`);
|
|
1486
|
+
} else if (script.trim() === "") {
|
|
1487
|
+
issues.push(`Script "${name}" is empty`);
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
if (issues.length > 0) {
|
|
1491
|
+
return {
|
|
1492
|
+
id: "scripts:package-json-scripts",
|
|
1493
|
+
name: SCRIPTS_CHECKS["scripts:package-json-scripts"].name,
|
|
1494
|
+
category: "scripts",
|
|
1495
|
+
severity: "warn",
|
|
1496
|
+
message: `Script issues: ${issues.join("; ")}`,
|
|
1497
|
+
detail: "Fix invalid script definitions in package.json",
|
|
1498
|
+
fixable: false
|
|
1499
|
+
};
|
|
1500
|
+
}
|
|
1501
|
+
return {
|
|
1502
|
+
id: "scripts:package-json-scripts",
|
|
1503
|
+
name: SCRIPTS_CHECKS["scripts:package-json-scripts"].name,
|
|
1504
|
+
category: "scripts",
|
|
1505
|
+
severity: "ok",
|
|
1506
|
+
message: `package.json scripts valid (${Object.keys(scripts).length} scripts)`,
|
|
1507
|
+
fixable: false
|
|
1508
|
+
};
|
|
1509
|
+
}
|
|
1510
|
+
async function checkPrismaScripts(ctx) {
|
|
1511
|
+
const pkgPath = join4(ctx.cwd, "package.json");
|
|
1512
|
+
if (!existsSync4(pkgPath)) {
|
|
1513
|
+
return {
|
|
1514
|
+
id: "scripts:prisma-scripts",
|
|
1515
|
+
name: SCRIPTS_CHECKS["scripts:prisma-scripts"].name,
|
|
1516
|
+
category: "scripts",
|
|
1517
|
+
severity: "info",
|
|
1518
|
+
message: "Skipped \u2014 no package.json",
|
|
1519
|
+
fixable: false
|
|
1520
|
+
};
|
|
1521
|
+
}
|
|
1522
|
+
const pkg = JSON.parse(readFileSync4(pkgPath, "utf-8"));
|
|
1523
|
+
const scripts = pkg.scripts || {};
|
|
1524
|
+
const allCatalogScripts = Object.keys(databaseScriptCatalog);
|
|
1525
|
+
const missing = [];
|
|
1526
|
+
const configured = [];
|
|
1527
|
+
const mismatched = [];
|
|
1528
|
+
for (const name of allCatalogScripts) {
|
|
1529
|
+
const expectedCmd = databaseScriptCatalog[name].command;
|
|
1530
|
+
if (scripts[name]) {
|
|
1531
|
+
configured.push(name);
|
|
1532
|
+
const actualCmd = scripts[name];
|
|
1533
|
+
if (!actualCmd.includes(expectedCmd.replace("prisma ", "").replace("prisma db seed", "seed"))) {
|
|
1534
|
+
mismatched.push(name);
|
|
1535
|
+
}
|
|
1536
|
+
} else {
|
|
1537
|
+
missing.push(name);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
const isSeedMissing = missing.includes(SEED_SCRIPT_NAME2);
|
|
1541
|
+
const otherMissing = missing.filter((m) => m !== SEED_SCRIPT_NAME2);
|
|
1542
|
+
const hasAnyPrismaScripts = configured.length > 0;
|
|
1543
|
+
if (otherMissing.length === allCatalogScripts.length) {
|
|
1544
|
+
const requiredScripts = {};
|
|
1545
|
+
for (const name of allCatalogScripts) {
|
|
1546
|
+
requiredScripts[name] = databaseScriptCatalog[name].command;
|
|
1547
|
+
}
|
|
1548
|
+
return {
|
|
1549
|
+
id: "scripts:prisma-scripts",
|
|
1550
|
+
name: SCRIPTS_CHECKS["scripts:prisma-scripts"].name,
|
|
1551
|
+
category: "scripts",
|
|
1552
|
+
severity: "warn",
|
|
1553
|
+
message: "No Prisma scripts configured",
|
|
1554
|
+
detail: "Run 'dxg add database' to add all Prisma scripts",
|
|
1555
|
+
fixable: true,
|
|
1556
|
+
fix: async () => {
|
|
1557
|
+
await addNpmScripts(ctx.fs, requiredScripts);
|
|
1558
|
+
}
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
if (otherMissing.length > 0) {
|
|
1562
|
+
const toAdd = {};
|
|
1563
|
+
for (const name of otherMissing) {
|
|
1564
|
+
toAdd[name] = databaseScriptCatalog[name].command;
|
|
1565
|
+
}
|
|
1566
|
+
return {
|
|
1567
|
+
id: "scripts:prisma-scripts",
|
|
1568
|
+
name: SCRIPTS_CHECKS["scripts:prisma-scripts"].name,
|
|
1569
|
+
category: "scripts",
|
|
1570
|
+
severity: "warn",
|
|
1571
|
+
message: `Missing Prisma scripts: ${otherMissing.join(", ")}`,
|
|
1572
|
+
detail: `Generator adds ${allCatalogScripts.length} scripts. Run 'dxg add database' to add missing ones${isSeedMissing ? " (db:seed is optional)" : ""}`,
|
|
1573
|
+
fixable: true,
|
|
1574
|
+
fix: async () => {
|
|
1575
|
+
await addNpmScripts(ctx.fs, toAdd);
|
|
1576
|
+
}
|
|
1577
|
+
};
|
|
1578
|
+
}
|
|
1579
|
+
if (mismatched.length > 0) {
|
|
1580
|
+
return {
|
|
1581
|
+
id: "scripts:prisma-scripts",
|
|
1582
|
+
name: SCRIPTS_CHECKS["scripts:prisma-scripts"].name,
|
|
1583
|
+
category: "scripts",
|
|
1584
|
+
severity: "warn",
|
|
1585
|
+
message: `Prisma scripts may have incorrect commands: ${mismatched.join(", ")}`,
|
|
1586
|
+
detail: `Expected commands from generator: ${Object.entries(databaseScriptCatalog).map(([k, v]) => `${k}: ${v.command}`).join("; ")}`,
|
|
1587
|
+
fixable: false
|
|
1588
|
+
};
|
|
1589
|
+
}
|
|
1590
|
+
if (isSeedMissing && hasAnyPrismaScripts) {
|
|
1591
|
+
return {
|
|
1592
|
+
id: "scripts:prisma-scripts",
|
|
1593
|
+
name: SCRIPTS_CHECKS["scripts:prisma-scripts"].name,
|
|
1594
|
+
category: "scripts",
|
|
1595
|
+
severity: "info",
|
|
1596
|
+
message: "All required Prisma scripts configured (db:seed optional, not configured)",
|
|
1597
|
+
fixable: false
|
|
1598
|
+
};
|
|
1599
|
+
}
|
|
1600
|
+
return {
|
|
1601
|
+
id: "scripts:prisma-scripts",
|
|
1602
|
+
name: SCRIPTS_CHECKS["scripts:prisma-scripts"].name,
|
|
1603
|
+
category: "scripts",
|
|
1604
|
+
severity: "ok",
|
|
1605
|
+
message: `All ${allCatalogScripts.length} Prisma scripts configured${isSeedMissing ? " (db:seed optional)" : ""}`,
|
|
1606
|
+
fixable: false
|
|
1607
|
+
};
|
|
1608
|
+
}
|
|
1609
|
+
async function registerScriptsChecks() {
|
|
1610
|
+
const { registerCheck: registerCheck2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
1611
|
+
registerCheck2("scripts:package-json-scripts", checkPackageJsonScripts, "scripts");
|
|
1612
|
+
registerCheck2("scripts:prisma-scripts", checkPrismaScripts, "scripts");
|
|
1613
|
+
}
|
|
1614
|
+
var SCRIPTS_CHECKS;
|
|
1615
|
+
var init_scripts = __esm({
|
|
1616
|
+
"src/checks/scripts.ts"() {
|
|
1617
|
+
"use strict";
|
|
1618
|
+
SCRIPTS_CHECKS = {
|
|
1619
|
+
"scripts:package-json-scripts": {
|
|
1620
|
+
name: "package.json scripts valid",
|
|
1621
|
+
category: "scripts"
|
|
1622
|
+
},
|
|
1623
|
+
"scripts:prisma-scripts": {
|
|
1624
|
+
name: "Prisma scripts configured",
|
|
1625
|
+
category: "scripts"
|
|
1626
|
+
}
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
});
|
|
1630
|
+
|
|
1631
|
+
// src/types.ts
|
|
1632
|
+
var DoctorExitCode = {
|
|
1633
|
+
/** All checks passed (or only info/warn). */
|
|
1634
|
+
OK: 0,
|
|
1635
|
+
/** Issues found (errors or warnings depending on --check-only). */
|
|
1636
|
+
ISSUES_FOUND: 1,
|
|
1637
|
+
/** Invalid usage (bad flags, missing args, etc.). */
|
|
1638
|
+
INVALID_USAGE: 2
|
|
1639
|
+
};
|
|
1640
|
+
|
|
1641
|
+
// src/index.ts
|
|
1642
|
+
init_registry();
|
|
1643
|
+
|
|
1644
|
+
// src/runner.ts
|
|
1645
|
+
init_registry();
|
|
1646
|
+
|
|
1647
|
+
// import("./checks/**/*") in src/runner.ts
|
|
1648
|
+
var globImport_checks = __glob({
|
|
1649
|
+
"./checks/database.ts": () => Promise.resolve().then(() => (init_database(), database_exports)),
|
|
1650
|
+
"./checks/env.ts": () => Promise.resolve().then(() => (init_env(), env_exports)),
|
|
1651
|
+
"./checks/project.ts": () => Promise.resolve().then(() => (init_project(), project_exports)),
|
|
1652
|
+
"./checks/scripts.ts": () => Promise.resolve().then(() => (init_scripts(), scripts_exports))
|
|
1653
|
+
});
|
|
1654
|
+
|
|
1655
|
+
// src/runner.ts
|
|
1656
|
+
async function runDoctor(ctx) {
|
|
1657
|
+
const categories = ctx.options.category;
|
|
1658
|
+
const checks = getDefaultChecks(categories);
|
|
1659
|
+
const allResults = [];
|
|
1660
|
+
const checkPromises = checks.map(async (entry) => {
|
|
1661
|
+
try {
|
|
1662
|
+
const result = await runCheck(entry.id, ctx);
|
|
1663
|
+
const results = Array.isArray(result) ? result : [result];
|
|
1664
|
+
allResults.push(...results);
|
|
1665
|
+
} catch (error) {
|
|
1666
|
+
allResults.push({
|
|
1667
|
+
id: entry.id,
|
|
1668
|
+
name: `Check failed: ${entry.category}`,
|
|
1669
|
+
category: entry.category,
|
|
1670
|
+
severity: "error",
|
|
1671
|
+
message: `Check execution failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1672
|
+
fixable: false
|
|
1673
|
+
});
|
|
1674
|
+
}
|
|
1675
|
+
});
|
|
1676
|
+
await Promise.all(checkPromises);
|
|
1677
|
+
const resultsByCategory = {
|
|
1678
|
+
env: [],
|
|
1679
|
+
project: [],
|
|
1680
|
+
database: [],
|
|
1681
|
+
scripts: []
|
|
1682
|
+
};
|
|
1683
|
+
for (const result of allResults) {
|
|
1684
|
+
resultsByCategory[result.category].push(result);
|
|
1685
|
+
}
|
|
1686
|
+
const summary = {
|
|
1687
|
+
ok: 0,
|
|
1688
|
+
info: 0,
|
|
1689
|
+
warn: 0,
|
|
1690
|
+
error: 0,
|
|
1691
|
+
fixable: 0
|
|
1692
|
+
};
|
|
1693
|
+
for (const result of allResults) {
|
|
1694
|
+
summary[result.severity]++;
|
|
1695
|
+
if (result.fixable) summary.fixable++;
|
|
1696
|
+
}
|
|
1697
|
+
const report = {
|
|
1698
|
+
results: resultsByCategory,
|
|
1699
|
+
summary,
|
|
1700
|
+
hasErrors: summary.error > 0,
|
|
1701
|
+
hasWarnings: summary.warn > 0,
|
|
1702
|
+
hasFixes: summary.fixable > 0
|
|
1703
|
+
};
|
|
1704
|
+
return report;
|
|
1705
|
+
}
|
|
1706
|
+
async function applyFixes(report, ctx) {
|
|
1707
|
+
let applied = 0;
|
|
1708
|
+
for (const [, results] of Object.entries(report.results)) {
|
|
1709
|
+
for (const result of results) {
|
|
1710
|
+
if (result.fixable && result.fix) {
|
|
1711
|
+
try {
|
|
1712
|
+
if (!ctx.options.dryRun) {
|
|
1713
|
+
await result.fix();
|
|
1714
|
+
}
|
|
1715
|
+
applied++;
|
|
1716
|
+
} catch (error) {
|
|
1717
|
+
console.error(
|
|
1718
|
+
`Failed to apply fix for ${result.id}: ${error instanceof Error ? error.message : String(error)}`
|
|
1719
|
+
);
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
return applied;
|
|
1725
|
+
}
|
|
1726
|
+
function determineExitCode(report, options) {
|
|
1727
|
+
if (options.checkOnly) {
|
|
1728
|
+
if (report.hasErrors || report.hasWarnings) {
|
|
1729
|
+
return 1;
|
|
1730
|
+
}
|
|
1731
|
+
return 0;
|
|
1732
|
+
}
|
|
1733
|
+
if (report.hasErrors) {
|
|
1734
|
+
return 1;
|
|
1735
|
+
}
|
|
1736
|
+
return 0;
|
|
1737
|
+
}
|
|
1738
|
+
async function runCategoryChecks(category, ctx) {
|
|
1739
|
+
await globImport_checks(`./checks/${category}`);
|
|
1740
|
+
const checks = getDefaultChecks([category]);
|
|
1741
|
+
const results = [];
|
|
1742
|
+
for (const entry of checks) {
|
|
1743
|
+
try {
|
|
1744
|
+
const result = await runCheck(entry.id, ctx);
|
|
1745
|
+
const resultArray = Array.isArray(result) ? result : [result];
|
|
1746
|
+
results.push(...resultArray);
|
|
1747
|
+
} catch (error) {
|
|
1748
|
+
results.push({
|
|
1749
|
+
id: entry.id,
|
|
1750
|
+
name: `Check failed: ${category}`,
|
|
1751
|
+
category,
|
|
1752
|
+
severity: "error",
|
|
1753
|
+
message: `Check execution failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1754
|
+
fixable: false
|
|
1755
|
+
});
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
return results;
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
// src/renderers.ts
|
|
1762
|
+
import pc from "picocolors";
|
|
1763
|
+
import { intro, outro, note, spinner } from "@dxgjs/prompts";
|
|
1764
|
+
var TAGS = {
|
|
1765
|
+
ok: pc.green("[OK] "),
|
|
1766
|
+
info: pc.blue("[INFO] "),
|
|
1767
|
+
warn: pc.yellow("[WARN] "),
|
|
1768
|
+
error: pc.red("[ERR] ")
|
|
1769
|
+
};
|
|
1770
|
+
var CATEGORY_LABELS = {
|
|
1771
|
+
env: "ENV",
|
|
1772
|
+
project: "PROJ",
|
|
1773
|
+
database: "DB",
|
|
1774
|
+
scripts: "SCRPT"
|
|
1775
|
+
};
|
|
1776
|
+
function stripAnsi(str) {
|
|
1777
|
+
return str.replace(/\u001b\[[0-9;]*m/g, "");
|
|
1778
|
+
}
|
|
1779
|
+
function formatCheckLines(result, showCategory) {
|
|
1780
|
+
const tag = TAGS[result.severity];
|
|
1781
|
+
const categoryLabel = showCategory ? pc.gray(`[${CATEGORY_LABELS[result.category]}] `) : "";
|
|
1782
|
+
const name = pc.bold(result.name);
|
|
1783
|
+
const message = result.message;
|
|
1784
|
+
const prefix = `${tag}${categoryLabel}${name} \u2014 `;
|
|
1785
|
+
const tagVisualLength = stripAnsi(tag).length;
|
|
1786
|
+
const detailIndent = tagVisualLength;
|
|
1787
|
+
const lines = [`${prefix}${message}`];
|
|
1788
|
+
if (result.detail) {
|
|
1789
|
+
const indent = " ".repeat(detailIndent);
|
|
1790
|
+
lines.push(`${indent}${pc.gray(result.detail)}`);
|
|
1791
|
+
}
|
|
1792
|
+
if (result.fixable) {
|
|
1793
|
+
const indent = " ".repeat(detailIndent);
|
|
1794
|
+
lines.push(`${indent}${pc.cyan("\u2192 Fixable with --fix")}`);
|
|
1795
|
+
}
|
|
1796
|
+
return lines;
|
|
1797
|
+
}
|
|
1798
|
+
function showIntro() {
|
|
1799
|
+
intro(pc.bgCyan(pc.black(" DXG Doctor ")));
|
|
1800
|
+
}
|
|
1801
|
+
function showOutro(report, options) {
|
|
1802
|
+
if (options.json) return;
|
|
1803
|
+
const { summary } = report;
|
|
1804
|
+
const parts = [];
|
|
1805
|
+
if (summary.ok > 0) parts.push(`${pc.green(summary.ok)} ${pc.green("ok")}`);
|
|
1806
|
+
if (summary.info > 0)
|
|
1807
|
+
parts.push(`${pc.blue(summary.info)} ${pc.blue("info")}`);
|
|
1808
|
+
if (summary.warn > 0)
|
|
1809
|
+
parts.push(`${pc.yellow(summary.warn)} ${pc.yellow("warn")}`);
|
|
1810
|
+
if (summary.error > 0)
|
|
1811
|
+
parts.push(`${pc.red(summary.error)} ${pc.red("err")}`);
|
|
1812
|
+
const summaryLine = parts.join(" \u2022 ");
|
|
1813
|
+
if (options.dryRun && summary.fixable > 0) {
|
|
1814
|
+
outro(pc.cyan(`Dry run \u2014 ${summaryLine} \u2022 ${summary.fixable} fixable`));
|
|
1815
|
+
} else if (options.fix && summary.fixable > 0) {
|
|
1816
|
+
outro(pc.green(`Fixed ${summary.fixable} issue(s) \u2014 ${summaryLine}`));
|
|
1817
|
+
} else if (report.hasErrors && summary.fixable > 0) {
|
|
1818
|
+
outro(`${pc.red(`${summaryLine} \u2022 Run with --fix to attempt repairs`)}`);
|
|
1819
|
+
} else if (report.hasWarnings && summary.fixable > 0) {
|
|
1820
|
+
outro(`${pc.yellow(`${summaryLine} \u2022 Run with --fix to attempt repairs`)}`);
|
|
1821
|
+
} else if (report.hasErrors) {
|
|
1822
|
+
outro(pc.red(`${summaryLine}`));
|
|
1823
|
+
} else if (report.hasWarnings) {
|
|
1824
|
+
outro(pc.yellow(`${summaryLine}`));
|
|
1825
|
+
} else {
|
|
1826
|
+
outro(pc.green(`${summaryLine} \u2022 All checks passed`));
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
function renderCheckResult(result) {
|
|
1830
|
+
const lines = formatCheckLines(result, true);
|
|
1831
|
+
for (const line of lines) {
|
|
1832
|
+
console.log(line);
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
function renderReport(report) {
|
|
1836
|
+
const categoryOrder = [
|
|
1837
|
+
"env",
|
|
1838
|
+
"project",
|
|
1839
|
+
"database",
|
|
1840
|
+
"scripts"
|
|
1841
|
+
];
|
|
1842
|
+
for (const category of categoryOrder) {
|
|
1843
|
+
const results = report.results[category];
|
|
1844
|
+
if (results.length === 0) continue;
|
|
1845
|
+
const severityOrder = ["error", "warn", "info", "ok"];
|
|
1846
|
+
const sorted = [...results].sort(
|
|
1847
|
+
(a, b) => severityOrder.indexOf(a.severity) - severityOrder.indexOf(b.severity)
|
|
1848
|
+
);
|
|
1849
|
+
const lines = [];
|
|
1850
|
+
for (const result of sorted) {
|
|
1851
|
+
lines.push(...formatCheckLines(result, false));
|
|
1852
|
+
}
|
|
1853
|
+
note(lines.join("\n"), pc.bold(CATEGORY_LABELS[category]));
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
function renderJson(report) {
|
|
1857
|
+
console.log(JSON.stringify(report, null, 2));
|
|
1858
|
+
}
|
|
1859
|
+
async function withSpinner(message, fn) {
|
|
1860
|
+
const s = spinner();
|
|
1861
|
+
s.start(message);
|
|
1862
|
+
try {
|
|
1863
|
+
const result = await fn();
|
|
1864
|
+
s.stop(pc.green("Done"));
|
|
1865
|
+
return result;
|
|
1866
|
+
} catch (error) {
|
|
1867
|
+
s.stop(pc.red("Failed"));
|
|
1868
|
+
throw error;
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
function showFixPreview(fixes) {
|
|
1872
|
+
if (fixes.length === 0) return;
|
|
1873
|
+
note(
|
|
1874
|
+
fixes.map((f) => pc.gray(" \u2022 ") + f).join("\n"),
|
|
1875
|
+
pc.cyan("Proposed fixes (dry run)")
|
|
1876
|
+
);
|
|
1877
|
+
}
|
|
1878
|
+
async function confirmFixes(ctx, count) {
|
|
1879
|
+
if (ctx.options.yes || ctx.options.nonInteractive) {
|
|
1880
|
+
return true;
|
|
1881
|
+
}
|
|
1882
|
+
const { confirm, isCancel } = await import("@dxgjs/prompts");
|
|
1883
|
+
const result = await confirm({
|
|
1884
|
+
message: `Apply ${count} fix${count === 1 ? "" : "es"}?`,
|
|
1885
|
+
initialValue: true
|
|
1886
|
+
});
|
|
1887
|
+
if (isCancel(result)) {
|
|
1888
|
+
return false;
|
|
1889
|
+
}
|
|
1890
|
+
return result;
|
|
1891
|
+
}
|
|
1892
|
+
function formatCheckId(id) {
|
|
1893
|
+
return pc.gray(`(${id})`);
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
// src/flags.ts
|
|
1897
|
+
import { ValidationError } from "@dxgjs/errors";
|
|
1898
|
+
var VALID_CATEGORIES = [
|
|
1899
|
+
"env",
|
|
1900
|
+
"project",
|
|
1901
|
+
"database",
|
|
1902
|
+
"scripts"
|
|
1903
|
+
];
|
|
1904
|
+
var DOCTOR_FLAGS = [
|
|
1905
|
+
"category",
|
|
1906
|
+
"json",
|
|
1907
|
+
"fix",
|
|
1908
|
+
"yes",
|
|
1909
|
+
"dryRun",
|
|
1910
|
+
"checkOnly"
|
|
1911
|
+
];
|
|
1912
|
+
function validateDoctorOptions(rawOptions, globalOptions) {
|
|
1913
|
+
const options = {
|
|
1914
|
+
...globalOptions
|
|
1915
|
+
};
|
|
1916
|
+
if (rawOptions.category !== void 0) {
|
|
1917
|
+
const categories = parseCategories(rawOptions.category);
|
|
1918
|
+
options.category = categories;
|
|
1919
|
+
}
|
|
1920
|
+
if (rawOptions.json !== void 0) {
|
|
1921
|
+
options.json = parseBoolean(rawOptions.json, "--json");
|
|
1922
|
+
}
|
|
1923
|
+
if (rawOptions.fix !== void 0) {
|
|
1924
|
+
options.fix = parseBoolean(rawOptions.fix, "--fix");
|
|
1925
|
+
}
|
|
1926
|
+
if (rawOptions.yes !== void 0) {
|
|
1927
|
+
options.yes = parseBoolean(rawOptions.yes, "--yes");
|
|
1928
|
+
}
|
|
1929
|
+
if (rawOptions.dryRun !== void 0) {
|
|
1930
|
+
options.dryRun = parseBoolean(rawOptions.dryRun, "--dry-run");
|
|
1931
|
+
}
|
|
1932
|
+
if (rawOptions.checkOnly !== void 0) {
|
|
1933
|
+
options.checkOnly = parseBoolean(rawOptions.checkOnly, "--check-only");
|
|
1934
|
+
}
|
|
1935
|
+
validateFlagCombinations(options);
|
|
1936
|
+
return options;
|
|
1937
|
+
}
|
|
1938
|
+
function parseCategories(value) {
|
|
1939
|
+
if (typeof value === "string") {
|
|
1940
|
+
return value.split(",").map((c) => c.trim()).filter((c) => c.length > 0).map(validateCategory);
|
|
1941
|
+
}
|
|
1942
|
+
if (Array.isArray(value)) {
|
|
1943
|
+
return value.map((v) => validateCategory(String(v)));
|
|
1944
|
+
}
|
|
1945
|
+
throw new ValidationError(
|
|
1946
|
+
`--category must be a string or array, got ${typeof value}`
|
|
1947
|
+
);
|
|
1948
|
+
}
|
|
1949
|
+
function validateCategory(category) {
|
|
1950
|
+
const normalized = category.toLowerCase();
|
|
1951
|
+
if (!VALID_CATEGORIES.includes(normalized)) {
|
|
1952
|
+
throw new ValidationError(
|
|
1953
|
+
`Invalid category: "${category}". Valid categories: ${VALID_CATEGORIES.join(", ")}`
|
|
1954
|
+
);
|
|
1955
|
+
}
|
|
1956
|
+
return normalized;
|
|
1957
|
+
}
|
|
1958
|
+
function parseBoolean(value, flagName) {
|
|
1959
|
+
if (typeof value === "boolean") return value;
|
|
1960
|
+
if (typeof value === "string") {
|
|
1961
|
+
const lower = value.toLowerCase();
|
|
1962
|
+
if (lower === "true" || lower === "1" || lower === "yes" || lower === "") {
|
|
1963
|
+
return true;
|
|
1964
|
+
}
|
|
1965
|
+
if (lower === "false" || lower === "0" || lower === "no") {
|
|
1966
|
+
return false;
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
throw new ValidationError(
|
|
1970
|
+
`${flagName} must be a boolean, got ${JSON.stringify(value)}`
|
|
1971
|
+
);
|
|
1972
|
+
}
|
|
1973
|
+
function validateFlagCombinations(options) {
|
|
1974
|
+
if (options.fix && options.category && options.category.length === 0) {
|
|
1975
|
+
throw new ValidationError(
|
|
1976
|
+
"--fix requires at least one category to check"
|
|
1977
|
+
);
|
|
1978
|
+
}
|
|
1979
|
+
if (options.checkOnly && options.fix) {
|
|
1980
|
+
throw new ValidationError(
|
|
1981
|
+
"--check-only and --fix cannot be used together"
|
|
1982
|
+
);
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
function validateKnownFlags(flags) {
|
|
1986
|
+
const knownFlags = [
|
|
1987
|
+
...DOCTOR_FLAGS,
|
|
1988
|
+
"nonInteractive",
|
|
1989
|
+
"dryRun",
|
|
1990
|
+
"force",
|
|
1991
|
+
"help",
|
|
1992
|
+
"version"
|
|
1993
|
+
];
|
|
1994
|
+
for (const flag of Object.keys(flags)) {
|
|
1995
|
+
if (!knownFlags.includes(flag)) {
|
|
1996
|
+
throw new ValidationError(
|
|
1997
|
+
`Unknown flag: --${flag}. Run "dxg doctor --help" for usage.`
|
|
1998
|
+
);
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
function mapExitCode(code) {
|
|
2003
|
+
return code;
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
// src/index.ts
|
|
2007
|
+
init_env();
|
|
2008
|
+
init_project();
|
|
2009
|
+
init_database();
|
|
2010
|
+
init_scripts();
|
|
2011
|
+
async function registerAllChecks() {
|
|
2012
|
+
const { clearRegistry: clearRegistry2 } = await Promise.resolve().then(() => (init_registry(), registry_exports));
|
|
2013
|
+
clearRegistry2();
|
|
2014
|
+
const { registerEnvChecks: registerEnvChecks2 } = await Promise.resolve().then(() => (init_env(), env_exports));
|
|
2015
|
+
const { registerProjectChecks: registerProjectChecks2 } = await Promise.resolve().then(() => (init_project(), project_exports));
|
|
2016
|
+
const { registerDatabaseChecks: registerDatabaseChecks2 } = await Promise.resolve().then(() => (init_database(), database_exports));
|
|
2017
|
+
const { registerScriptsChecks: registerScriptsChecks2 } = await Promise.resolve().then(() => (init_scripts(), scripts_exports));
|
|
2018
|
+
await registerEnvChecks2();
|
|
2019
|
+
await registerProjectChecks2();
|
|
2020
|
+
await registerDatabaseChecks2();
|
|
2021
|
+
await registerScriptsChecks2();
|
|
2022
|
+
}
|
|
2023
|
+
export {
|
|
2024
|
+
DoctorExitCode,
|
|
2025
|
+
applyFixes,
|
|
2026
|
+
clearRegistry,
|
|
2027
|
+
confirmFixes,
|
|
2028
|
+
determineExitCode,
|
|
2029
|
+
formatCheckId,
|
|
2030
|
+
getAllChecks,
|
|
2031
|
+
getCheck,
|
|
2032
|
+
getChecksByCategory,
|
|
2033
|
+
getDefaultChecks,
|
|
2034
|
+
mapExitCode,
|
|
2035
|
+
registerAllChecks,
|
|
2036
|
+
registerCheck,
|
|
2037
|
+
registerDatabaseChecks,
|
|
2038
|
+
registerEnvChecks,
|
|
2039
|
+
registerProjectChecks,
|
|
2040
|
+
registerScriptsChecks,
|
|
2041
|
+
renderCheckResult,
|
|
2042
|
+
renderJson,
|
|
2043
|
+
renderReport,
|
|
2044
|
+
runCategoryChecks,
|
|
2045
|
+
runCheck,
|
|
2046
|
+
runDoctor,
|
|
2047
|
+
showFixPreview,
|
|
2048
|
+
showIntro,
|
|
2049
|
+
showOutro,
|
|
2050
|
+
validateDoctorOptions,
|
|
2051
|
+
validateKnownFlags,
|
|
2052
|
+
withSpinner
|
|
2053
|
+
};
|
|
2054
|
+
//# sourceMappingURL=index.js.map
|