@bfra.me/workspace-analyzer 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.
Files changed (73) hide show
  1. package/README.md +402 -0
  2. package/lib/chunk-4LSFAAZW.js +1 -0
  3. package/lib/chunk-JDF7DQ4V.js +27 -0
  4. package/lib/chunk-WOJ4C7N7.js +7122 -0
  5. package/lib/cli.d.ts +1 -0
  6. package/lib/cli.js +318 -0
  7. package/lib/index.d.ts +3701 -0
  8. package/lib/index.js +1262 -0
  9. package/lib/types/index.d.ts +146 -0
  10. package/lib/types/index.js +28 -0
  11. package/package.json +89 -0
  12. package/src/analyzers/analyzer.ts +201 -0
  13. package/src/analyzers/architectural-analyzer.ts +304 -0
  14. package/src/analyzers/build-config-analyzer.ts +334 -0
  15. package/src/analyzers/circular-import-analyzer.ts +463 -0
  16. package/src/analyzers/config-consistency-analyzer.ts +335 -0
  17. package/src/analyzers/dead-code-analyzer.ts +565 -0
  18. package/src/analyzers/duplicate-code-analyzer.ts +626 -0
  19. package/src/analyzers/duplicate-dependency-analyzer.ts +381 -0
  20. package/src/analyzers/eslint-config-analyzer.ts +281 -0
  21. package/src/analyzers/exports-field-analyzer.ts +324 -0
  22. package/src/analyzers/index.ts +388 -0
  23. package/src/analyzers/large-dependency-analyzer.ts +535 -0
  24. package/src/analyzers/package-json-analyzer.ts +349 -0
  25. package/src/analyzers/peer-dependency-analyzer.ts +275 -0
  26. package/src/analyzers/tree-shaking-analyzer.ts +623 -0
  27. package/src/analyzers/tsconfig-analyzer.ts +382 -0
  28. package/src/analyzers/unused-dependency-analyzer.ts +356 -0
  29. package/src/analyzers/version-alignment-analyzer.ts +308 -0
  30. package/src/api/analyze-workspace.ts +245 -0
  31. package/src/api/index.ts +11 -0
  32. package/src/cache/cache-manager.ts +495 -0
  33. package/src/cache/cache-schema.ts +247 -0
  34. package/src/cache/change-detector.ts +169 -0
  35. package/src/cache/file-hasher.ts +65 -0
  36. package/src/cache/index.ts +47 -0
  37. package/src/cli/commands/analyze.ts +240 -0
  38. package/src/cli/commands/index.ts +5 -0
  39. package/src/cli/index.ts +61 -0
  40. package/src/cli/types.ts +65 -0
  41. package/src/cli/ui.ts +213 -0
  42. package/src/cli.ts +9 -0
  43. package/src/config/defaults.ts +183 -0
  44. package/src/config/index.ts +81 -0
  45. package/src/config/loader.ts +270 -0
  46. package/src/config/merger.ts +229 -0
  47. package/src/config/schema.ts +263 -0
  48. package/src/core/incremental-analyzer.ts +462 -0
  49. package/src/core/index.ts +34 -0
  50. package/src/core/orchestrator.ts +416 -0
  51. package/src/graph/dependency-graph.ts +408 -0
  52. package/src/graph/index.ts +19 -0
  53. package/src/index.ts +417 -0
  54. package/src/parser/config-parser.ts +491 -0
  55. package/src/parser/import-extractor.ts +340 -0
  56. package/src/parser/index.ts +54 -0
  57. package/src/parser/typescript-parser.ts +95 -0
  58. package/src/performance/bundle-estimator.ts +444 -0
  59. package/src/performance/index.ts +27 -0
  60. package/src/reporters/console-reporter.ts +355 -0
  61. package/src/reporters/index.ts +49 -0
  62. package/src/reporters/json-reporter.ts +273 -0
  63. package/src/reporters/markdown-reporter.ts +349 -0
  64. package/src/reporters/reporter.ts +399 -0
  65. package/src/rules/builtin-rules.ts +709 -0
  66. package/src/rules/index.ts +52 -0
  67. package/src/rules/rule-engine.ts +409 -0
  68. package/src/scanner/index.ts +18 -0
  69. package/src/scanner/workspace-scanner.ts +403 -0
  70. package/src/types/index.ts +176 -0
  71. package/src/types/result.ts +19 -0
  72. package/src/utils/index.ts +7 -0
  73. package/src/utils/pattern-matcher.ts +48 -0
package/README.md ADDED
@@ -0,0 +1,402 @@
1
+ # @bfra.me/workspace-analyzer
2
+
3
+ Comprehensive monorepo static analysis for workspace configuration, dependencies, and architecture.
4
+
5
+ ## Features
6
+
7
+ - **Configuration Analysis**: Detect inconsistencies across `package.json`, `tsconfig.json`, `eslint.config.ts`, and `tsup.config.ts` files
8
+ - **Dependency Analysis**: Identify unused dependencies and circular imports with full path reporting
9
+ - **Architectural Validation**: Validate against configurable rule sets (layer violations, barrel export misuse)
10
+ - **Performance Analysis**: Identify tree-shaking blockers, large dependencies, and dead code
11
+ - **Modern CLI**: Interactive TUI using `@clack/prompts` with progress reporting
12
+ - **Incremental Analysis**: Caching support for fast re-analysis of large codebases
13
+ - **Multiple Output Formats**: Console, JSON, and Markdown report generation
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ pnpm add -D @bfra.me/workspace-analyzer
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ```bash
24
+ # Analyze current workspace
25
+ npx workspace-analyzer
26
+
27
+ # Interactive mode with package selection
28
+ npx workspace-analyzer --interactive
29
+
30
+ # CI-friendly JSON output
31
+ npx workspace-analyzer --json > analysis-report.json
32
+ ```
33
+
34
+ ## CLI Usage
35
+
36
+ The CLI provides a modern, interactive experience powered by `@clack/prompts`:
37
+
38
+ ```bash
39
+ # Basic analysis
40
+ workspace-analyzer [path]
41
+
42
+ # With configuration file
43
+ workspace-analyzer --config workspace-analyzer.config.ts
44
+
45
+ # Output formats
46
+ workspace-analyzer --json # JSON output
47
+ workspace-analyzer --markdown # Markdown report
48
+
49
+ # Filtering options
50
+ workspace-analyzer --min-severity warning
51
+ workspace-analyzer --categories dependency,architecture
52
+
53
+ # Analysis control
54
+ workspace-analyzer --no-cache # Disable caching
55
+ workspace-analyzer --verbose # Detailed output
56
+ workspace-analyzer --dry-run # Preview without analysis
57
+ ```
58
+
59
+ ### CLI Options
60
+
61
+ | Option | Description |
62
+ | ------------------------ | ----------------------------------------------------------- |
63
+ | `--config <path>` | Path to configuration file |
64
+ | `--json` | Output results as JSON |
65
+ | `--markdown` | Output results as Markdown |
66
+ | `--min-severity <level>` | Minimum severity to report (info, warning, error, critical) |
67
+ | `--categories <list>` | Comma-separated categories to analyze |
68
+ | `--include <patterns>` | Glob patterns for files to include |
69
+ | `--exclude <patterns>` | Glob patterns for files to exclude |
70
+ | `--no-cache` | Disable incremental analysis caching |
71
+ | `--cache-dir <path>` | Custom cache directory |
72
+ | `--concurrency <n>` | Maximum parallel operations |
73
+ | `--verbose` | Enable verbose logging |
74
+ | `--quiet` | Suppress non-essential output |
75
+ | `--dry-run` | Preview analysis without executing |
76
+ | `--interactive` | Interactive package selection |
77
+ | `--fix` | Apply auto-fixes where available (placeholder) |
78
+
79
+ ## Programmatic API
80
+
81
+ ### `analyzeWorkspace(path, options)`
82
+
83
+ Analyzes an entire workspace for issues.
84
+
85
+ ```typescript
86
+ import {analyzeWorkspace} from '@bfra.me/workspace-analyzer'
87
+
88
+ const result = await analyzeWorkspace('./my-monorepo', {
89
+ minSeverity: 'warning',
90
+ categories: ['dependency', 'architecture'],
91
+ onProgress: (progress) => {
92
+ console.log(`${progress.phase}: ${progress.processed}/${progress.total}`)
93
+ },
94
+ })
95
+
96
+ if (result.success) {
97
+ console.log(`Found ${result.data.summary.totalIssues} issues`)
98
+ for (const issue of result.data.issues) {
99
+ console.log(`[${issue.severity}] ${issue.title}`)
100
+ console.log(` ${issue.location.filePath}:${issue.location.line}`)
101
+ if (issue.suggestion) {
102
+ console.log(` Fix: ${issue.suggestion}`)
103
+ }
104
+ }
105
+ } else {
106
+ console.error(`Analysis failed: ${result.error.message}`)
107
+ }
108
+ ```
109
+
110
+ ### `analyzePackages(path, packageNames, options)`
111
+
112
+ Analyzes specific packages within a workspace.
113
+
114
+ ```typescript
115
+ import {analyzePackages} from '@bfra.me/workspace-analyzer'
116
+
117
+ const result = await analyzePackages('./my-monorepo', [
118
+ '@myorg/core',
119
+ '@myorg/utils',
120
+ ])
121
+
122
+ if (result.success) {
123
+ console.log(`Analyzed ${result.data.summary.packagesAnalyzed} packages`)
124
+ }
125
+ ```
126
+
127
+ ### `defineConfig(config)`
128
+
129
+ Type-safe configuration helper.
130
+
131
+ ```typescript
132
+ import {defineConfig} from '@bfra.me/workspace-analyzer'
133
+
134
+ export default defineConfig({
135
+ minSeverity: 'warning',
136
+ categories: ['dependency', 'architecture'],
137
+ })
138
+ ```
139
+
140
+ ## Configuration
141
+
142
+ Create a `workspace-analyzer.config.ts` file in your workspace root:
143
+
144
+ ```typescript
145
+ import {defineConfig} from '@bfra.me/workspace-analyzer'
146
+
147
+ export default defineConfig({
148
+ // File patterns
149
+ include: ['**/*.ts', '**/*.tsx'],
150
+ exclude: ['**/node_modules/**', '**/lib/**', '**/dist/**'],
151
+
152
+ // Analysis settings
153
+ minSeverity: 'warning',
154
+ categories: ['dependency', 'configuration', 'architecture'],
155
+
156
+ // Performance
157
+ cache: true,
158
+ cacheDir: '.workspace-analyzer-cache',
159
+ concurrency: 4,
160
+
161
+ // Package patterns (for monorepo scanning)
162
+ packagePatterns: ['packages/*', 'apps/*'],
163
+
164
+ // Per-analyzer configuration
165
+ analyzers: {
166
+ 'circular-import': {enabled: true, severity: 'error'},
167
+ 'unused-dependency': {enabled: true, severity: 'warning'},
168
+ 'config-consistency': {enabled: true},
169
+ 'tree-shaking-blocker': {enabled: true},
170
+ 'dead-code': {enabled: false}, // Disable specific analyzer
171
+ },
172
+
173
+ // Architectural rules
174
+ architecture: {
175
+ layers: [
176
+ {name: 'domain', patterns: ['**/domain/**'], allowedImports: []},
177
+ {name: 'application', patterns: ['**/application/**'], allowedImports: ['domain']},
178
+ {name: 'infrastructure', patterns: ['**/infrastructure/**'], allowedImports: ['domain', 'application']},
179
+ ],
180
+ allowBarrelExports: ['**/index.ts'],
181
+ enforcePublicApi: true,
182
+ },
183
+ })
184
+ ```
185
+
186
+ ### Configuration File Names
187
+
188
+ The analyzer searches for configuration in these files (in order):
189
+
190
+ - `workspace-analyzer.config.ts`
191
+ - `workspace-analyzer.config.js`
192
+ - `workspace-analyzer.config.mjs`
193
+ - `workspace-analyzer.config.cjs`
194
+
195
+ ## Built-in Analyzers
196
+
197
+ ### Configuration Analyzers
198
+
199
+ | Analyzer | Description |
200
+ | -------------------- | ------------------------------------------------------ |
201
+ | `package-json` | Validates package.json structure, exports, and scripts |
202
+ | `tsconfig` | Checks TypeScript configuration consistency |
203
+ | `eslint-config` | Validates ESLint configuration patterns |
204
+ | `build-config` | Analyzes tsup/build configuration |
205
+ | `config-consistency` | Cross-validates configuration across files |
206
+ | `version-alignment` | Checks dependency version consistency across packages |
207
+ | `exports-field` | Validates exports field against actual source files |
208
+
209
+ ### Dependency Analyzers
210
+
211
+ | Analyzer | Description |
212
+ | ---------------------- | ----------------------------------------------------- |
213
+ | `unused-dependency` | Detects dependencies declared but not imported |
214
+ | `circular-import` | Finds circular import chains using Tarjan's algorithm |
215
+ | `peer-dependency` | Validates peer dependency declarations |
216
+ | `duplicate-dependency` | Finds duplicate dependencies across packages |
217
+
218
+ ### Architectural Analyzers
219
+
220
+ | Analyzer | Description |
221
+ | --------------- | ------------------------------------------------- |
222
+ | `architectural` | Enforces layer boundaries and import restrictions |
223
+
224
+ ### Performance Analyzers
225
+
226
+ | Analyzer | Description |
227
+ | ---------------------- | ----------------------------------------------------- |
228
+ | `tree-shaking-blocker` | Detects patterns that prevent tree-shaking |
229
+ | `dead-code` | Finds unreachable/unused exports |
230
+ | `duplicate-code` | Identifies similar code blocks via AST fingerprinting |
231
+ | `large-dependency` | Flags large dependencies that may impact bundle size |
232
+
233
+ ## Issue Categories
234
+
235
+ | Category | Description |
236
+ | ----------------- | --------------------------------------------------------- |
237
+ | `configuration` | Package.json, tsconfig.json, and build config issues |
238
+ | `dependency` | Unused, duplicate, or misversioned dependencies |
239
+ | `architecture` | Layer violations, barrel export misuse, public API issues |
240
+ | `performance` | Tree-shaking blockers, large dependencies |
241
+ | `circular-import` | Circular dependency chains |
242
+ | `unused-export` | Exports not used within the workspace |
243
+ | `type-safety` | TypeScript configuration issues |
244
+
245
+ ## Severity Levels
246
+
247
+ | Level | Description |
248
+ | ---------- | --------------------------------------------------- |
249
+ | `info` | Informational findings, suggestions for improvement |
250
+ | `warning` | Potential issues that should be reviewed |
251
+ | `error` | Problems that should be fixed |
252
+ | `critical` | Severe issues requiring immediate attention |
253
+
254
+ ## Architectural Rules
255
+
256
+ ### Layer Violation Rule
257
+
258
+ Enforces architectural layer boundaries:
259
+
260
+ ```typescript
261
+ defineConfig({
262
+ architecture: {
263
+ layers: [
264
+ {name: 'domain', patterns: ['**/domain/**'], allowedImports: []},
265
+ {name: 'application', patterns: ['**/app/**'], allowedImports: ['domain']},
266
+ {name: 'infrastructure', patterns: ['**/infra/**'], allowedImports: ['domain', 'application']},
267
+ ],
268
+ },
269
+ })
270
+ ```
271
+
272
+ ### Barrel Export Rule
273
+
274
+ Controls `export *` usage:
275
+
276
+ ```typescript
277
+ defineConfig({
278
+ architecture: {
279
+ // Allow barrel exports only in specific files
280
+ allowBarrelExports: ['**/index.ts'],
281
+ // Or disable barrel exports entirely
282
+ // allowBarrelExports: false,
283
+ },
284
+ })
285
+ ```
286
+
287
+ ### Public API Rule
288
+
289
+ Enforces explicit public API surface:
290
+
291
+ ```typescript
292
+ defineConfig({
293
+ architecture: {
294
+ enforcePublicApi: true,
295
+ },
296
+ })
297
+ ```
298
+
299
+ ## Output Formats
300
+
301
+ ### Console (Default)
302
+
303
+ Colorized terminal output with severity highlighting and file locations.
304
+
305
+ ### JSON
306
+
307
+ Machine-readable output for CI integration:
308
+
309
+ ```bash
310
+ workspace-analyzer --json > report.json
311
+ ```
312
+
313
+ ```json
314
+ {
315
+ "issues": [],
316
+ "summary": {
317
+ "totalIssues": 5,
318
+ "bySeverity": {"warning": 3, "error": 2},
319
+ "byCategory": {"dependency": 2, "architecture": 3},
320
+ "packagesAnalyzed": 8,
321
+ "filesAnalyzed": 150,
322
+ "durationMs": 2500
323
+ }
324
+ }
325
+ ```
326
+
327
+ ### Markdown
328
+
329
+ Human-readable report for documentation or PR comments:
330
+
331
+ ```bash
332
+ workspace-analyzer --markdown > ANALYSIS.md
333
+ ```
334
+
335
+ ## Caching
336
+
337
+ The analyzer supports incremental caching for improved performance:
338
+
339
+ ```typescript
340
+ defineConfig({
341
+ cache: true,
342
+ cacheDir: '.workspace-analyzer-cache',
343
+ maxCacheAge: 7 * 24 * 60 * 60 * 1000, // 7 days
344
+ hashAlgorithm: 'sha256',
345
+ })
346
+ ```
347
+
348
+ Cache is automatically invalidated when:
349
+
350
+ - Source files change (content hash)
351
+ - Configuration files change
352
+ - Analyzer versions change
353
+
354
+ ## CI Integration
355
+
356
+ ### GitHub Actions
357
+
358
+ ```yaml
359
+ - name: Run workspace analysis
360
+ run: pnpm workspace-analyzer --json > analysis-report.json
361
+
362
+ - name: Check for errors
363
+ run: |
364
+ if jq -e '.summary.bySeverity.error > 0' analysis-report.json; then
365
+ echo "Analysis found errors"
366
+ exit 1
367
+ fi
368
+ ```
369
+
370
+ ### Pre-commit Hook
371
+
372
+ ```bash
373
+ # package.json
374
+ {
375
+ "lint-staged": {
376
+ "*.{ts,tsx}": ["workspace-analyzer --categories dependency,architecture"]
377
+ }
378
+ }
379
+ ```
380
+
381
+ ## Development
382
+
383
+ ```bash
384
+ # Build the package
385
+ pnpm --filter @bfra.me/workspace-analyzer build
386
+
387
+ # Run tests
388
+ pnpm --filter @bfra.me/workspace-analyzer test
389
+
390
+ # Type check
391
+ pnpm --filter @bfra.me/workspace-analyzer type-check
392
+ ```
393
+
394
+ ## Related Packages
395
+
396
+ - [@bfra.me/es](../es/README.md) - Core utilities (Result type, async helpers)
397
+ - [@bfra.me/doc-sync](../doc-sync/README.md) - Documentation sync (shares TypeScript parsing infrastructure)
398
+ - [@bfra.me/eslint-config](../eslint-config/readme.md) - ESLint configuration
399
+
400
+ ## License
401
+
402
+ MIT © [Marcus R. Brown](https://github.com/marcusrbrown)
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-4LSFAAZW.js.map
@@ -0,0 +1,27 @@
1
+ // src/types/result.ts
2
+ import { err, ok } from "@bfra.me/es/result";
3
+ import { isErr, isOk } from "@bfra.me/es/result";
4
+ import {
5
+ flatMap,
6
+ fromPromise,
7
+ fromThrowable,
8
+ map,
9
+ mapErr,
10
+ unwrap,
11
+ unwrapOr
12
+ } from "@bfra.me/es/result";
13
+
14
+ export {
15
+ err,
16
+ ok,
17
+ isErr,
18
+ isOk,
19
+ flatMap,
20
+ fromPromise,
21
+ fromThrowable,
22
+ map,
23
+ mapErr,
24
+ unwrap,
25
+ unwrapOr
26
+ };
27
+ //# sourceMappingURL=chunk-JDF7DQ4V.js.map