@objectstack/cli 3.0.0 → 3.0.1

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.
@@ -5,7 +5,8 @@ import chalk from 'chalk';
5
5
  import { execSync } from 'child_process';
6
6
  import fs from 'fs';
7
7
  import path from 'path';
8
- import { printHeader, printSuccess, printWarning, printError } from '../utils/format.js';
8
+ import { printHeader, printSuccess, printWarning, printError, printStep, printInfo } from '../utils/format.js';
9
+ import { loadConfig, configExists } from '../utils/config.js';
9
10
 
10
11
  interface HealthCheckResult {
11
12
  name: string;
@@ -14,9 +15,293 @@ interface HealthCheckResult {
14
15
  fix?: string;
15
16
  }
16
17
 
18
+ // ─── Config-Aware Checks ────────────────────────────────────────────
19
+
20
+ function detectCircularDependencies(objects: any[]): string[] {
21
+ const issues: string[] = [];
22
+ const graph = new Map<string, string[]>();
23
+
24
+ for (const obj of objects) {
25
+ const deps: string[] = [];
26
+ if (obj.fields && typeof obj.fields === 'object') {
27
+ for (const field of Object.values(obj.fields) as any[]) {
28
+ if (field?.type === 'lookup' && field?.reference) {
29
+ deps.push(field.reference);
30
+ }
31
+ }
32
+ }
33
+ graph.set(obj.name, deps);
34
+ }
35
+
36
+ // DFS cycle detection
37
+ const visited = new Set<string>();
38
+ const stack = new Set<string>();
39
+
40
+ function dfs(node: string, path: string[]): boolean {
41
+ if (stack.has(node)) {
42
+ const cycleStart = path.indexOf(node);
43
+ const cycle = path.slice(cycleStart).concat(node);
44
+ issues.push(`Circular dependency: ${cycle.join(' → ')}`);
45
+ return true;
46
+ }
47
+ if (visited.has(node)) return false;
48
+
49
+ visited.add(node);
50
+ stack.add(node);
51
+
52
+ for (const dep of graph.get(node) || []) {
53
+ if (graph.has(dep)) {
54
+ dfs(dep, [...path, node]);
55
+ }
56
+ }
57
+
58
+ stack.delete(node);
59
+ return false;
60
+ }
61
+
62
+ for (const name of graph.keys()) {
63
+ if (!visited.has(name)) {
64
+ dfs(name, []);
65
+ }
66
+ }
67
+
68
+ return issues;
69
+ }
70
+
71
+ function findOrphanViews(config: any): string[] {
72
+ const objectNames = new Set<string>();
73
+ if (Array.isArray(config.objects)) {
74
+ for (const obj of config.objects) {
75
+ if (obj.name) objectNames.add(obj.name);
76
+ }
77
+ }
78
+
79
+ const orphans: string[] = [];
80
+ if (Array.isArray(config.views)) {
81
+ for (const view of config.views) {
82
+ if (view.object && !objectNames.has(view.object)) {
83
+ orphans.push(`View "${view.name || '?'}" references non-existent object "${view.object}"`);
84
+ }
85
+ }
86
+ }
87
+ return orphans;
88
+ }
89
+
90
+ function findUnusedObjects(config: any): string[] {
91
+ const objectNames = new Set<string>();
92
+ if (Array.isArray(config.objects)) {
93
+ for (const obj of config.objects) {
94
+ if (obj.name) objectNames.add(obj.name);
95
+ }
96
+ }
97
+
98
+ const referencedObjects = new Set<string>();
99
+
100
+ // Views reference objects
101
+ if (Array.isArray(config.views)) {
102
+ for (const view of config.views) {
103
+ if (view.object) referencedObjects.add(view.object);
104
+ }
105
+ }
106
+
107
+ // Flows may reference objects via trigger
108
+ if (Array.isArray(config.flows)) {
109
+ for (const flow of config.flows) {
110
+ if (flow.trigger?.object) referencedObjects.add(flow.trigger.object);
111
+ if (flow.object) referencedObjects.add(flow.object);
112
+ }
113
+ }
114
+
115
+ // Apps may reference objects via navigation
116
+ if (Array.isArray(config.apps)) {
117
+ for (const app of config.apps) {
118
+ if (Array.isArray(app.navigation)) {
119
+ for (const nav of app.navigation) {
120
+ if (nav.object) referencedObjects.add(nav.object);
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ // Agents may reference objects
127
+ if (Array.isArray(config.agents)) {
128
+ for (const agent of config.agents) {
129
+ if (Array.isArray(agent.objects)) {
130
+ for (const o of agent.objects) referencedObjects.add(o);
131
+ }
132
+ }
133
+ }
134
+
135
+ // Lookup fields reference other objects
136
+ if (Array.isArray(config.objects)) {
137
+ for (const obj of config.objects) {
138
+ if (obj.fields && typeof obj.fields === 'object') {
139
+ for (const field of Object.values(obj.fields) as any[]) {
140
+ if (field?.type === 'lookup' && field?.reference) {
141
+ referencedObjects.add(field.reference);
142
+ }
143
+ }
144
+ }
145
+ }
146
+ }
147
+
148
+ const unused: string[] = [];
149
+ for (const name of objectNames) {
150
+ if (!referencedObjects.has(name)) {
151
+ unused.push(`Object "${name}" is defined but not referenced by any view, flow, app, or agent`);
152
+ }
153
+ }
154
+ return unused;
155
+ }
156
+
157
+ // ─── Filesystem Checks ──────────────────────────────────────────────
158
+
159
+ function walkDir(dir: string, ext: string): string[] {
160
+ const results: string[] = [];
161
+ if (!fs.existsSync(dir)) return results;
162
+
163
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
164
+ for (const entry of entries) {
165
+ if (entry.name === 'node_modules') continue;
166
+ const fullPath = path.join(dir, entry.name);
167
+ if (entry.isDirectory()) {
168
+ results.push(...walkDir(fullPath, ext));
169
+ } else if (entry.name.endsWith(ext)) {
170
+ results.push(fullPath);
171
+ }
172
+ }
173
+ return results;
174
+ }
175
+
176
+ function findMissingTests(cwd: string): string[] {
177
+ const specSrcDir = path.join(cwd, 'packages/spec/src');
178
+ if (!fs.existsSync(specSrcDir)) return [];
179
+
180
+ const missing: string[] = [];
181
+ const zodFiles = walkDir(specSrcDir, '.zod.ts');
182
+
183
+ for (const zodFile of zodFiles) {
184
+ const testFile = zodFile.replace('.zod.ts', '.test.ts');
185
+ if (!fs.existsSync(testFile)) {
186
+ const relZod = path.relative(specSrcDir, zodFile);
187
+ const relTest = path.relative(specSrcDir, testFile);
188
+ missing.push(`Missing test: ${relTest} (for ${relZod})`);
189
+ }
190
+ }
191
+ return missing;
192
+ }
193
+
194
+ function findDeprecatedUsages(cwd: string): string[] {
195
+ const specSrcDir = path.join(cwd, 'packages/spec/src');
196
+ if (!fs.existsSync(specSrcDir)) return [];
197
+
198
+ const deprecated: string[] = [];
199
+ const tsFiles = walkDir(specSrcDir, '.ts')
200
+ .filter((f) => !f.endsWith('.test.ts'));
201
+
202
+ for (const tsFile of tsFiles) {
203
+ try {
204
+ const content = fs.readFileSync(tsFile, 'utf-8');
205
+ const lines = content.split('\n');
206
+ const relPath = path.relative(specSrcDir, tsFile);
207
+ for (let i = 0; i < lines.length; i++) {
208
+ if (lines[i].includes('@deprecated')) {
209
+ deprecated.push(`${relPath}:${i + 1} — @deprecated tag found`);
210
+ }
211
+ }
212
+ } catch {
213
+ // Skip unreadable files
214
+ }
215
+ }
216
+ return deprecated;
217
+ }
218
+
219
+ // ─── Deprecated Pattern Detection ───────────────────────────────────
220
+
221
+ const DEPRECATED_PATTERNS: Array<{
222
+ pattern: RegExp;
223
+ description: string;
224
+ replacement: string;
225
+ }> = [
226
+ {
227
+ pattern: /\bEnhancedObjectKernel\b/,
228
+ description: 'EnhancedObjectKernel is deprecated in v3',
229
+ replacement: 'Use ObjectKernel instead',
230
+ },
231
+ {
232
+ pattern: /\bmax_length\b/,
233
+ description: 'snake_case config key: max_length',
234
+ replacement: 'Use maxLength (camelCase)',
235
+ },
236
+ {
237
+ pattern: /\bdefault_value\b/,
238
+ description: 'snake_case config key: default_value',
239
+ replacement: 'Use defaultValue (camelCase)',
240
+ },
241
+ {
242
+ pattern: /\bmin_length\b/,
243
+ description: 'snake_case config key: min_length',
244
+ replacement: 'Use minLength (camelCase)',
245
+ },
246
+ {
247
+ pattern: /\breference_filters\b/,
248
+ description: 'snake_case config key: reference_filters',
249
+ replacement: 'Use referenceFilters (camelCase)',
250
+ },
251
+ {
252
+ pattern: /\bunique_name\b/,
253
+ description: 'snake_case config key: unique_name',
254
+ replacement: 'Use uniqueName (camelCase)',
255
+ },
256
+ {
257
+ pattern: /from\s+['"]@objectstack\/core\/enhanced['"]/,
258
+ description: 'Import from deprecated @objectstack/core/enhanced path',
259
+ replacement: "Use import from '@objectstack/core'",
260
+ },
261
+ {
262
+ pattern: /from\s+['"]@objectstack\/spec\/dist\/[^'"]+['"]/,
263
+ description: 'Import from deprecated @objectstack/spec/dist/ deep path',
264
+ replacement: "Use import from '@objectstack/spec'",
265
+ },
266
+ ];
267
+
268
+ function scanDeprecatedPatterns(dir: string): Array<{ file: string; line: number; description: string; replacement: string }> {
269
+ const results: Array<{ file: string; line: number; description: string; replacement: string }> = [];
270
+ if (!fs.existsSync(dir)) return results;
271
+
272
+ const tsFiles = walkDir(dir, '.ts').filter(f => !f.endsWith('.test.ts'));
273
+
274
+ for (const tsFile of tsFiles) {
275
+ try {
276
+ const content = fs.readFileSync(tsFile, 'utf-8');
277
+ const lines = content.split('\n');
278
+ const relPath = path.relative(process.cwd(), tsFile);
279
+
280
+ for (let i = 0; i < lines.length; i++) {
281
+ for (const dp of DEPRECATED_PATTERNS) {
282
+ if (dp.pattern.test(lines[i])) {
283
+ results.push({
284
+ file: relPath,
285
+ line: i + 1,
286
+ description: dp.description,
287
+ replacement: dp.replacement,
288
+ });
289
+ }
290
+ }
291
+ }
292
+ } catch {
293
+ // Skip unreadable files
294
+ }
295
+ }
296
+ return results;
297
+ }
298
+
299
+ // ─── Command ────────────────────────────────────────────────────────
300
+
17
301
  export const doctorCommand = new Command('doctor')
18
- .description('Check development environment health')
302
+ .description('Check development environment and configuration health')
19
303
  .option('-v, --verbose', 'Show detailed information')
304
+ .option('--scan-deprecations', 'Scan for deprecated ObjectStack patterns')
20
305
  .action(async (options) => {
21
306
  printHeader('Environment Health Check');
22
307
 
@@ -138,7 +423,7 @@ export const doctorCommand = new Command('doctor')
138
423
  });
139
424
  }
140
425
 
141
- // Display results
426
+ // Display environment results
142
427
  let hasErrors = false;
143
428
  let hasWarnings = false;
144
429
 
@@ -160,6 +445,103 @@ export const doctorCommand = new Command('doctor')
160
445
  if (result.status === 'error') hasErrors = true;
161
446
  if (result.status === 'warning') hasWarnings = true;
162
447
  });
448
+
449
+ // ── Extended Checks ──────────────────────────────────────────────
450
+
451
+ // Missing test files
452
+ printStep('Checking for missing test files...');
453
+ const missingTests = findMissingTests(cwd);
454
+ if (missingTests.length > 0) {
455
+ hasWarnings = true;
456
+ for (const msg of missingTests) {
457
+ printWarning(msg);
458
+ }
459
+ } else {
460
+ printSuccess('Test coverage All *.zod.ts files have matching tests');
461
+ }
462
+
463
+ // Deprecated usage detection
464
+ printStep('Scanning for @deprecated usage...');
465
+ const deprecatedUsages = findDeprecatedUsages(cwd);
466
+ if (deprecatedUsages.length > 0) {
467
+ hasWarnings = true;
468
+ for (const msg of deprecatedUsages) {
469
+ printWarning(`Deprecated: ${msg}`);
470
+ }
471
+ } else {
472
+ printSuccess('Deprecations No @deprecated tags found');
473
+ }
474
+
475
+ // Config-aware checks (only if config exists)
476
+ if (configExists()) {
477
+ printStep('Loading configuration for analysis...');
478
+ try {
479
+ const { config } = await loadConfig();
480
+
481
+ // Circular dependency detection
482
+ if (Array.isArray(config.objects) && config.objects.length > 0) {
483
+ printStep('Checking for circular dependencies...');
484
+ const cycles = detectCircularDependencies(config.objects);
485
+ if (cycles.length > 0) {
486
+ hasWarnings = true;
487
+ for (const msg of cycles) {
488
+ printWarning(msg);
489
+ }
490
+ } else {
491
+ printSuccess('Dependencies No circular references detected');
492
+ }
493
+
494
+ // Unused objects
495
+ printStep('Checking for unused objects...');
496
+ const unused = findUnusedObjects(config);
497
+ if (unused.length > 0) {
498
+ hasWarnings = true;
499
+ for (const msg of unused) {
500
+ printWarning(msg);
501
+ }
502
+ } else {
503
+ printSuccess('Object usage All objects are referenced');
504
+ }
505
+ }
506
+
507
+ // Orphan views
508
+ if (Array.isArray(config.views) && config.views.length > 0) {
509
+ printStep('Checking for orphan views...');
510
+ const orphans = findOrphanViews(config);
511
+ if (orphans.length > 0) {
512
+ hasWarnings = true;
513
+ for (const msg of orphans) {
514
+ printWarning(msg);
515
+ }
516
+ } else {
517
+ printSuccess('View integrity All views reference valid objects');
518
+ }
519
+ }
520
+ } catch {
521
+ printWarning('Could not load config for analysis (config checks skipped)');
522
+ hasWarnings = true;
523
+ }
524
+ }
525
+
526
+ // ── Deprecation Pattern Scan ─────────────────────────────────────
527
+ if (options.scanDeprecations) {
528
+ printStep('Scanning for deprecated ObjectStack patterns...');
529
+ const scanDir = path.join(cwd, 'src');
530
+ const deprecations = scanDeprecatedPatterns(scanDir);
531
+ if (deprecations.length > 0) {
532
+ hasWarnings = true;
533
+ for (const dep of deprecations) {
534
+ printWarning(`${dep.file}:${dep.line} — ${dep.description}`);
535
+ if (options.verbose) {
536
+ console.log(chalk.dim(` → ${dep.replacement}`));
537
+ }
538
+ }
539
+ console.log('');
540
+ printInfo(`Found ${deprecations.length} deprecated pattern(s). Run \`objectstack codemod v2-to-v3\` to auto-fix.`);
541
+ } else {
542
+ printSuccess('Deprecation scan No deprecated patterns found');
543
+ }
544
+ }
163
545
 
164
546
  console.log('');
165
547