code-quality-lib 2.0.2 → 2.5.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/README.md CHANGED
@@ -15,6 +15,9 @@
15
15
  - **CLI + Library** — use from terminal or programmatically
16
16
  - **Detailed reports** — generates `.quality-report.md` with AI-friendly error info
17
17
  - **`--logs` flag** — verbose terminal output for debugging
18
+ - **`--fix` flag** — auto-fix ESLint and Prettier issues automatically
19
+ - **Environment-based configs** — different tools for dev vs CI/CD
20
+ - **Snyk token validation** — validates tokens before running security scans
18
21
  - **TypeScript definitions** — full type safety included
19
22
 
20
23
  ## Installation
@@ -29,35 +32,244 @@ yarn add -D code-quality-lib # yarn
29
32
  ## Quick Start
30
33
 
31
34
  ```bash
32
- npx code-quality # npm
33
- bunx code-quality # bun
34
- pnpm dlx code-quality # pnpm
35
- yarn dlx code-quality # yarn
35
+ # Install and run (first time will auto-start wizard)
36
+ npm install -D code-quality-lib && npx code-quality
37
+
38
+ # Or with bun
39
+ bun add -D code-quality-lib && bunx code-quality
40
+
41
+ # Or with yarn
42
+ yarn add -D code-quality-lib && yarn code-quality
36
43
  ```
37
44
 
38
45
  ## CLI Usage
39
46
 
40
47
  ```bash
41
48
  code-quality # run all quality checks
49
+ code-quality --wizard # run interactive setup wizard
50
+ code-quality --config # generate .code-quality.json config file
42
51
  code-quality --logs # show detailed error output
52
+ code-quality --fix # auto-fix ESLint and Prettier issues
53
+ code-quality --env dev # run development checks (ESLint, TS, Prettier)
54
+ code-quality --env ci # run CI/CD checks (all tools)
55
+ code-quality --env prod # run production checks (all tools)
43
56
  code-quality --help # show help
44
57
  code-quality --version # show version
45
58
  ```
46
59
 
60
+ ### Interactive Wizard
61
+
62
+ Use the wizard to configure options before running:
63
+
64
+ ```bash
65
+ code-quality --wizard
66
+ ```
67
+
68
+ The wizard will guide you through:
69
+
70
+ ```
71
+ 🧙‍♂️ Code Quality Setup Wizard
72
+ ──────────────────────────────────────────────────
73
+ Let's configure your quality checks!
74
+
75
+ 📦 Detected package manager: npm
76
+ Use npm? (Y/n):
77
+
78
+ ⚙️ Use project config files (.eslintrc, .prettierrc, etc.)?
79
+ Answer "No" to use bundled configs from code-quality-lib
80
+ Use project configs? (y/N):
81
+
82
+ 🔧 Select tools to run (default = all checked):
83
+ [✓] TypeScript? (Y/n):
84
+ [✓] ESLint? (Y/n):
85
+ [✓] Prettier? (Y/n):
86
+ [✓] Knip? (Y/n):
87
+ [✓] Snyk? (Y/n):
88
+
89
+ 🌍 Load .env file before running checks?
90
+ Load .env? (Y/n):
91
+
92
+ 📋 Configuration Summary:
93
+ ──────────────────────────────────────────────────
94
+ 📦 Package Manager: npm
95
+ ⚙️ Config: Bundled configs
96
+ 🔧 Tools: TypeScript, ESLint, Prettier, Knip, Snyk
97
+ 🌍 Load .env: Yes
98
+ ──────────────────────────────────────────────────
99
+ Run checks with these settings? (Y/n):
100
+ ```
101
+
102
+ **Smart Features:**
103
+
104
+ - **Remember settings** — First run creates `.code-quality.json`, future runs skip questions
105
+ - **Yes/No questions** — Simple Y/n prompts with sensible defaults
106
+ - **Checkbox-style tools** — Each tool can be individually enabled/disabled
107
+ - **Bundled configs default** — Uses library's built-in configs by default
108
+
109
+ After confirmation, it runs the quality checks with your selected settings.
110
+
111
+ ### Auto-Wizard on First Run
112
+
113
+ If you run `code-quality` without any configuration file, it automatically starts the wizard:
114
+
115
+ ```bash
116
+ code-quality # First run: no config found → starts wizard
117
+ code-quality # Subsequent runs: uses saved settings
118
+ ```
119
+
120
+ This ensures proper setup on first use while being fast on subsequent runs.
121
+
122
+ ### Terminal Output
123
+
124
+ The CLI provides step-by-step progress like setup wizards:
125
+
126
+ ```
127
+ 🚀 Code Quality Setup
128
+ ──────────────────────────────────────────────────
129
+ 📦 Package Manager: npm
130
+ ⚙️ Config: Project configs
131
+ 🔧 Tools: 5 quality checks
132
+
133
+ 1. TypeScript... ✅ Done
134
+ 2. ESLint... ✅ Done
135
+ 3. Prettier... ✅ Done
136
+ 4. Knip... ✅ Done
137
+ 5. Snyk... ✅ Done
138
+
139
+ ──────────────────────────────────────────────────
140
+ 📊 Quality Check Summary
141
+
142
+ ✅ TypeScript Passed
143
+ ✅ ESLint Passed
144
+ ✅ Prettier Passed
145
+ ✅ Knip Passed
146
+ ✅ Snyk Passed
147
+
148
+ ──────────────────────────────────────────────────
149
+ 🎉 Success! All quality checks passed.
150
+
151
+ ✅ Your code is ready for production!
152
+ ```
153
+
154
+ ### Auto-Fix with --fix
155
+
156
+ Automatically fix ESLint and Prettier issues:
157
+
158
+ ```bash
159
+ code-quality --fix # Fix all issues
160
+ code-quality --env prod --fix # Fix in production mode
161
+ code-quality --ESLint --fix # Fix only ESLint
162
+ code-quality --Prettier --fix # Fix only Prettier
163
+ ```
164
+
165
+ The `--fix` flag will:
166
+
167
+ 1. Run quality checks normally
168
+ 2. If ESLint or Prettier fail, automatically run:
169
+ - `eslint --fix` for ESLint issues
170
+ - `prettier --write` for Prettier issues
171
+ 3. Re-run checks to verify fixes
172
+ 4. Show final results
173
+
174
+ ### Environment-Based Configuration
175
+
176
+ Different tool sets for different environments:
177
+
178
+ ```bash
179
+ code-quality --env development # ESLint, TypeScript, Prettier
180
+ code-quality --env ci # All tools (ESLint, TS, Prettier, Knip, Snyk)
181
+ code-quality --env production # All tools (ESLint, TS, Prettier, Knip, Snyk)
182
+ ```
183
+
184
+ Or configure environments in `.code-quality/config.json`:
185
+
186
+ ```json
187
+ {
188
+ "version": "1.0.0",
189
+ "environments": {
190
+ "development": {
191
+ "tools": ["ESLint", "TypeScript", "Prettier"]
192
+ },
193
+ "ci": {
194
+ "tools": ["ESLint", "TypeScript", "Prettier", "Knip", "Snyk"]
195
+ },
196
+ "production": {
197
+ "tools": ["ESLint", "TypeScript", "Prettier", "Knip", "Snyk"]
198
+ }
199
+ },
200
+ "packageManager": "npm",
201
+ "useProjectConfig": true,
202
+ "loadEnv": true
203
+ }
204
+ ```
205
+
206
+ ### Configuration Directory
207
+
208
+ Generate a configuration directory with reference configs:
209
+
210
+ ```bash
211
+ code-quality --config
212
+ ```
213
+
214
+ This creates `.code-quality/` directory with:
215
+
216
+ - **config.json** — Main configuration file
217
+ - **tsconfig.json** — TypeScript reference config
218
+ - **eslint.config.mjs** — ESLint reference config
219
+ - **.prettierrc** — Prettier reference config
220
+ - **knip.json** — Knip reference config
221
+ - **README.md** — Usage documentation
222
+
223
+ The CLI automatically loads `.code-quality/config.json` if it exists:
224
+
225
+ ```bash
226
+ code-quality # uses your custom config
227
+ ```
228
+
229
+ Or use it programmatically:
230
+
231
+ ```javascript
232
+ const config = require('./.code-quality/config.json')
233
+ const checker = new CodeQualityChecker(config)
234
+ await checker.run()
235
+ ```
236
+
237
+ ### Config Modes
238
+
239
+ **Use Project Configs (Default)**
240
+
241
+ ```json
242
+ {
243
+ "useProjectConfig": true
244
+ }
245
+ ```
246
+
247
+ Uses your project's existing config files (`.eslintrc.js`, `.prettierrc`, `tsconfig.json`, etc.)
248
+
249
+ **Use Reference Configs**
250
+
251
+ ```json
252
+ {
253
+ "useProjectConfig": false
254
+ }
255
+ ```
256
+
257
+ Uses reference configs from `.code-quality/` directory as starting point for new projects
258
+
47
259
  ## Library Usage
48
260
 
49
261
  ```javascript
50
- const { CodeQualityChecker, runQualityCheck } = require('code-quality-lib');
262
+ const { CodeQualityChecker, runQualityCheck } = require('code-quality-lib')
51
263
 
52
264
  // Quick — run all checks with defaults (uses project's config files)
53
- const result = await runQualityCheck();
54
- console.log(result.success ? 'All passed' : 'Some failed');
265
+ const result = await runQualityCheck()
266
+ console.log(result.success ? 'All passed' : 'Some failed')
55
267
 
56
268
  // Use bundled configs instead of project's configs
57
269
  const checker = new CodeQualityChecker({
58
270
  useProjectConfig: false, // use library's bundled .eslintrc, .prettierrc, etc.
59
- });
60
- await checker.run();
271
+ })
272
+ await checker.run()
61
273
 
62
274
  // Custom — select tools, override commands
63
275
  const customChecker = new CodeQualityChecker({
@@ -68,34 +280,58 @@ const customChecker = new CodeQualityChecker({
68
280
  ESLint: 'eslint src/ --ext .ts,.tsx',
69
281
  },
70
282
  loadEnv: false,
71
- });
283
+ })
72
284
 
73
- const result = await customChecker.run({ showLogs: true });
74
- console.log(result.results); // per-tool results array
285
+ const result = await customChecker.run({ showLogs: true })
286
+ console.log(result.results) // per-tool results array
75
287
  ```
76
288
 
77
289
  ## Configuration Options
78
290
 
79
- | Option | Type | Default | Description |
80
- |--------|------|---------|-------------|
81
- | `useProjectConfig` | `boolean` | `true` | Use project's config files (`.eslintrc.js`, `.prettierrc`, etc.) instead of bundled configs |
82
- | `tools` | `string[]` | All 5 tools | Which tools to run |
83
- | `packageManager` | `'npm' \| 'bun' \| 'pnpm' \| 'yarn'` | auto-detected | Force a specific package manager |
84
- | `commands` | `Record<string, string>` | bundled paths | Custom commands per tool |
85
- | `descriptions` | `Record<string, string>` | built-in | Custom descriptions per tool |
86
- | `loadEnv` | `boolean` | `true` | Load `.env` file |
291
+ | Option | Type | Default | Description |
292
+ | ------------------ | ------------------------------------ | ------------- | ------------------------------------------------------------------------------------------- |
293
+ | `useProjectConfig` | `boolean` | `true` | Use project's config files (`.eslintrc.js`, `.prettierrc`, etc.) instead of bundled configs |
294
+ | `tools` | `string[]` | All 5 tools | Which tools to run |
295
+ | `packageManager` | `'npm' \| 'bun' \| 'pnpm' \| 'yarn'` | auto-detected | Force a specific package manager |
296
+ | `commands` | `Record<string, string>` | bundled paths | Custom commands per tool |
297
+ | `descriptions` | `Record<string, string>` | built-in | Custom descriptions per tool |
298
+ | `loadEnv` | `boolean` | `true` | Load `.env` file |
299
+ | `environment` | `string` | auto-detected | Override environment (development, ci, production) |
300
+ | `environments` | `Record<string, EnvironmentConfig>` | - | Environment-specific tool configurations |
301
+
302
+ **EnvironmentConfig:**
303
+
304
+ ```typescript
305
+ interface EnvironmentConfig {
306
+ tools: string[]
307
+ }
308
+ ```
87
309
 
88
- ## Bundled Tools
310
+ ## Tool Resolution
89
311
 
90
- All tools are included as dependencies zero extra setup:
312
+ The library intelligently resolves tools in this order:
91
313
 
92
- | Tool | Description |
93
- |------|-------------|
94
- | **TypeScript** | Type checking (`tsc --noEmit`) |
95
- | **ESLint** | Linting with plugins (react, sonarjs, unicorn, import, prettier) |
96
- | **Prettier** | Code formatting validation |
97
- | **Knip** | Dead code and unused export detection |
98
- | **Snyk** | Security vulnerability scanning |
314
+ 1. **Project's `node_modules/.bin`** Uses your project's installed versions first
315
+ 2. **Library's bundled tools** — Falls back to bundled versions if not found in project
316
+ 3. **Custom commands** If you specify custom commands in config, uses them as-is
317
+
318
+ This means:
319
+
320
+ - Uses your project's tool versions and configurations by default
321
+ - ✅ Works out-of-the-box with bundled tools as fallback
322
+ - ✅ Custom commands use tools from your project's PATH
323
+
324
+ ### Bundled Tools
325
+
326
+ All tools are included as dependencies for fallback:
327
+
328
+ | Tool | Description |
329
+ | -------------- | ---------------------------------------------------------------- |
330
+ | **TypeScript** | Type checking (`tsc --noEmit`) |
331
+ | **ESLint** | Linting with plugins (react, sonarjs, unicorn, import, prettier) |
332
+ | **Prettier** | Code formatting validation |
333
+ | **Knip** | Dead code and unused export detection |
334
+ | **Snyk** | Security vulnerability scanning |
99
335
 
100
336
  ## Package Manager Detection
101
337
 
@@ -110,12 +346,46 @@ Automatically detected by lock file presence:
110
346
  ## Error Reporting
111
347
 
112
348
  Every run generates `.quality-report.md` with:
349
+
113
350
  - Status of each check (pass/fail)
114
351
  - Full error output for failed checks
115
352
  - AI-friendly structured information for automated fixes
116
353
 
117
354
  Add `.quality-report.md` to your `.gitignore`.
118
355
 
356
+ ## Snyk Token Validation
357
+
358
+ The library validates Snyk tokens before running security scans:
359
+
360
+ ```bash
361
+ # Set your Snyk token
362
+ export SNYK_TOKEN=your_token_here
363
+
364
+ # Or add to .env file
365
+ echo "SNYK_TOKEN=your_token_here" >> .env
366
+
367
+ # Run with validation
368
+ code-quality --env production
369
+ ```
370
+
371
+ **Token Validation Features:**
372
+
373
+ - **Pre-scan validation** - Checks token before running full scan
374
+ - **Clear cache** - Forces token validation by clearing Snyk cache
375
+ - **Detailed errors** - Shows helpful fix instructions for invalid tokens
376
+ - **Fallback handling** - Graceful degradation for token issues
377
+
378
+ **Error Messages:**
379
+
380
+ ```
381
+ ❌ Snyk token validation failed. Token may be expired or invalid.
382
+
383
+ To fix:
384
+ 1. Get a new token at: https://snyk.io/login
385
+ 2. Set SNYK_TOKEN in your .env file
386
+ 3. Or run: npx snyk auth
387
+ ```
388
+
119
389
  ## AI Skills
120
390
 
121
391
  This library includes `.ai/skills/` — markdown files that teach AI coding assistants (Cursor, Copilot, Windsurf, etc.) to follow the project's coding standards. See [`.ai/skills/README.md`](.ai/skills/README.md).
@@ -127,6 +397,7 @@ This library includes `.ai/skills/` — markdown files that teach AI coding assi
127
397
  ## Testing & CI/CD
128
398
 
129
399
  Tested on every push across 4 runtimes:
400
+
130
401
  - **Node.js 25.x** (npm)
131
402
  - **Bun 1.3.x**
132
403
  - **pnpm 10.x**
package/index.d.ts CHANGED
@@ -1,49 +1,60 @@
1
+ export interface EnvironmentConfig {
2
+ tools: string[]
3
+ }
4
+
1
5
  export interface CodeQualityOptions {
2
6
  /** Load environment variables from .env file (default: true) */
3
- loadEnv?: boolean;
7
+ loadEnv?: boolean
4
8
  /** Use project's own config files instead of bundled configs (default: true) */
5
- useProjectConfig?: boolean;
9
+ useProjectConfig?: boolean
6
10
  /** Array of tool names to run (default: all tools) */
7
- tools?: string[];
11
+ tools?: string[]
8
12
  /** Custom commands for each tool, keyed by tool name */
9
- commands?: Record<string, string>;
13
+ commands?: Record<string, string>
10
14
  /** Custom descriptions for each tool, keyed by tool name */
11
- descriptions?: Record<string, string>;
15
+ descriptions?: Record<string, string>
12
16
  /** Force a specific package manager (auto-detected if not specified) */
13
- packageManager?: 'bun' | 'pnpm' | 'yarn' | 'npm';
17
+ packageManager?: 'bun' | 'pnpm' | 'yarn' | 'npm'
14
18
  /** Show detailed error logs in terminal */
15
- showLogs?: boolean;
19
+ showLogs?: boolean
20
+ /** Set the active environment (default: development) */
21
+ environment?: string
22
+ /** Environment-specific tool configurations */
23
+ environments?: Record<string, EnvironmentConfig>
16
24
  }
17
25
 
18
26
  export interface CommandResult {
19
- success: boolean;
20
- output: string;
27
+ success: boolean
28
+ output: string
21
29
  }
22
30
 
23
31
  export interface CheckResult {
24
- name: string;
25
- description: string;
26
- success: boolean;
27
- output: string;
32
+ name: string
33
+ description: string
34
+ success: boolean
35
+ output: string
36
+ errors: number
37
+ warnings: number
38
+ errorLines: string[]
28
39
  }
29
40
 
30
41
  export interface QualityCheckResult {
31
- success: boolean;
32
- message: string;
33
- results: CheckResult[];
42
+ success: boolean
43
+ message: string
44
+ results: CheckResult[]
34
45
  }
35
46
 
36
47
  export class CodeQualityChecker {
37
- options: Required<Omit<CodeQualityOptions, 'showLogs'>>;
48
+ options: Required<Omit<CodeQualityOptions, 'showLogs'>>
38
49
 
39
- constructor(options?: CodeQualityOptions);
50
+ constructor(options?: CodeQualityOptions)
40
51
 
41
52
  /** Execute a single shell command and return the result */
42
- runCommand(command: string, description: string): CommandResult;
53
+ runCommand(command: string, description: string): CommandResult
43
54
 
44
55
  /** Run all configured quality checks */
45
- run(options?: { showLogs?: boolean }): Promise<QualityCheckResult>;
56
+ run(options?: { showLogs?: boolean }): Promise<QualityCheckResult>
46
57
  }
47
58
 
48
59
  /** Convenience function to run quality checks without instantiating the class */
49
- export function runQualityCheck(options?: CodeQualityOptions): Promise<QualityCheckResult>;
60
+ export function runQualityCheck(options?: CodeQualityOptions): Promise<QualityCheckResult>