@webpieces/dev-config 0.2.61 → 0.2.63

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.
@@ -0,0 +1,528 @@
1
+ /**
2
+ * Validate Modified Files Executor
3
+ *
4
+ * Validates that modified files don't exceed a maximum line count (default 900).
5
+ * This encourages keeping files small and focused - when you touch a file,
6
+ * you must bring it under the limit.
7
+ *
8
+ * Usage:
9
+ * nx affected --target=validate-modified-files --base=origin/main
10
+ *
11
+ * Escape hatch: Add webpieces-disable max-lines-modified-files comment with date and justification
12
+ * Format: // webpieces-disable max-lines-modified-files 2025/01/15 -- [reason]
13
+ * The disable expires after 1 month from the date specified.
14
+ */
15
+
16
+ import type { ExecutorContext } from '@nx/devkit';
17
+ import { execSync } from 'child_process';
18
+ import * as fs from 'fs';
19
+ import * as path from 'path';
20
+
21
+ export interface ValidateModifiedFilesOptions {
22
+ max?: number;
23
+ forceLimit?: boolean;
24
+ }
25
+
26
+ export interface ExecutorResult {
27
+ success: boolean;
28
+ }
29
+
30
+ interface FileViolation {
31
+ file: string;
32
+ lines: number;
33
+ expiredDisable?: boolean;
34
+ expiredDate?: string;
35
+ }
36
+
37
+ const TMP_DIR = 'tmp/webpieces';
38
+ const TMP_MD_FILE = 'webpieces.filesize.md';
39
+
40
+ const FILESIZE_DOC_CONTENT = `# AI Agent Instructions: File Too Long
41
+
42
+ **READ THIS FILE to fix files that are too long**
43
+
44
+ ## Core Principle
45
+
46
+ With **stateless systems + dependency injection, refactor is trivial**.
47
+ Pick a method or a few and move to new class XXXXX, then inject XXXXX
48
+ into all users of those methods via the constructor.
49
+ Delete those methods from original class.
50
+
51
+ **99% of files can be less than the configured max lines of code.**
52
+
53
+ Files should contain a SINGLE COHESIVE UNIT.
54
+ - One class per file (Java convention)
55
+ - If class is too large, extract child responsibilities
56
+ - Use dependency injection to compose functionality
57
+
58
+ ## Command: Reduce File Size
59
+
60
+ ### Step 1: Check for Multiple Classes
61
+ If the file contains multiple classes, **SEPARATE each class into its own file**.
62
+
63
+ \`\`\`typescript
64
+ // BAD: UserController.ts (multiple classes)
65
+ export class UserController { /* ... */ }
66
+ export class UserValidator { /* ... */ }
67
+ export class UserNotifier { /* ... */ }
68
+
69
+ // GOOD: Three separate files
70
+ // UserController.ts
71
+ export class UserController { /* ... */ }
72
+
73
+ // UserValidator.ts
74
+ export class UserValidator { /* ... */ }
75
+
76
+ // UserNotifier.ts
77
+ export class UserNotifier { /* ... */ }
78
+ \`\`\`
79
+
80
+ ### Step 2: Extract Child Responsibilities (if single class is too large)
81
+
82
+ #### Pattern: Create New Service Class with Dependency Injection
83
+
84
+ \`\`\`typescript
85
+ // BAD: UserController.ts (800 lines, single class)
86
+ @provideSingleton()
87
+ @Controller()
88
+ export class UserController {
89
+ // 200 lines: CRUD operations
90
+ // 300 lines: validation logic
91
+ // 200 lines: notification logic
92
+ // 100 lines: analytics logic
93
+ }
94
+
95
+ // GOOD: Extract validation service
96
+ // 1. Create UserValidationService.ts
97
+ @provideSingleton()
98
+ export class UserValidationService {
99
+ validateUserData(data: UserData): ValidationResult {
100
+ // 300 lines of validation logic moved here
101
+ }
102
+
103
+ validateEmail(email: string): boolean { /* ... */ }
104
+ validatePassword(password: string): boolean { /* ... */ }
105
+ }
106
+
107
+ // 2. Inject into UserController.ts
108
+ @provideSingleton()
109
+ @Controller()
110
+ export class UserController {
111
+ constructor(
112
+ @inject(TYPES.UserValidationService)
113
+ private validator: UserValidationService
114
+ ) {}
115
+
116
+ async createUser(data: UserData): Promise<User> {
117
+ const validation = this.validator.validateUserData(data);
118
+ if (!validation.isValid) {
119
+ throw new ValidationError(validation.errors);
120
+ }
121
+ // ... rest of logic
122
+ }
123
+ }
124
+ \`\`\`
125
+
126
+ ## AI Agent Action Steps
127
+
128
+ 1. **ANALYZE** the file structure:
129
+ - Count classes (if >1, separate immediately)
130
+ - Identify logical responsibilities within single class
131
+
132
+ 2. **IDENTIFY** "child code" to extract:
133
+ - Validation logic -> ValidationService
134
+ - Notification logic -> NotificationService
135
+ - Data transformation -> TransformerService
136
+ - External API calls -> ApiService
137
+ - Business rules -> RulesEngine
138
+
139
+ 3. **CREATE** new service file(s):
140
+ - Start with temporary name: \`XXXX.ts\` or \`ChildService.ts\`
141
+ - Add \`@provideSingleton()\` decorator
142
+ - Move child methods to new class
143
+
144
+ 4. **UPDATE** dependency injection:
145
+ - Add to \`TYPES\` constants (if using symbol-based DI)
146
+ - Inject new service into original class constructor
147
+ - Replace direct method calls with \`this.serviceName.method()\`
148
+
149
+ 5. **RENAME** extracted file:
150
+ - Read the extracted code to understand its purpose
151
+ - Rename \`XXXX.ts\` to logical name (e.g., \`UserValidationService.ts\`)
152
+
153
+ 6. **VERIFY** file sizes:
154
+ - Original file should now be under the limit
155
+ - Each extracted file should be under the limit
156
+ - If still too large, extract more services
157
+
158
+ ## Examples of Child Responsibilities to Extract
159
+
160
+ | If File Contains | Extract To | Pattern |
161
+ |-----------------|------------|---------|
162
+ | Validation logic (200+ lines) | \`XValidator.ts\` or \`XValidationService.ts\` | Singleton service |
163
+ | Notification logic (150+ lines) | \`XNotifier.ts\` or \`XNotificationService.ts\` | Singleton service |
164
+ | Data transformation (200+ lines) | \`XTransformer.ts\` | Singleton service |
165
+ | External API calls (200+ lines) | \`XApiClient.ts\` | Singleton service |
166
+ | Complex business rules (300+ lines) | \`XRulesEngine.ts\` | Singleton service |
167
+ | Database queries (200+ lines) | \`XRepository.ts\` | Singleton service |
168
+
169
+ ## WebPieces Dependency Injection Pattern
170
+
171
+ \`\`\`typescript
172
+ // 1. Define service with @provideSingleton
173
+ import { provideSingleton } from '@webpieces/http-routing';
174
+
175
+ @provideSingleton()
176
+ export class MyService {
177
+ doSomething(): void { /* ... */ }
178
+ }
179
+
180
+ // 2. Inject into consumer
181
+ import { inject } from 'inversify';
182
+ import { TYPES } from './types';
183
+
184
+ @provideSingleton()
185
+ @Controller()
186
+ export class MyController {
187
+ constructor(
188
+ @inject(TYPES.MyService) private service: MyService
189
+ ) {}
190
+ }
191
+ \`\`\`
192
+
193
+ ## Escape Hatch
194
+
195
+ If refactoring is genuinely not feasible (generated files, complex algorithms, etc.),
196
+ add a disable comment at the TOP of the file (within first 5 lines) with a DATE:
197
+
198
+ \`\`\`typescript
199
+ // webpieces-disable max-lines-modified-files 2025/01/15 -- Complex generated file, refactoring would break generation
200
+ \`\`\`
201
+
202
+ **IMPORTANT**: The date format is yyyy/mm/dd. The disable will EXPIRE after 1 month from this date.
203
+ After expiration, you must either fix the file or update the date to get another month.
204
+ This ensures that disable comments are reviewed periodically.
205
+
206
+ Remember: Find the "child code" and pull it down into a new class. Once moved, the code's purpose becomes clear, making it easy to rename to a logical name.
207
+ `;
208
+
209
+ /**
210
+ * Write the instructions documentation to tmp directory
211
+ */
212
+ function writeTmpInstructions(workspaceRoot: string): string {
213
+ const tmpDir = path.join(workspaceRoot, TMP_DIR);
214
+ const mdPath = path.join(tmpDir, TMP_MD_FILE);
215
+
216
+ fs.mkdirSync(tmpDir, { recursive: true });
217
+ fs.writeFileSync(mdPath, FILESIZE_DOC_CONTENT);
218
+
219
+ return mdPath;
220
+ }
221
+
222
+ /**
223
+ * Get changed TypeScript files between base and working tree.
224
+ * Uses `git diff base` (no three-dots) to match what `nx affected` does -
225
+ * this includes both committed and uncommitted changes in one diff.
226
+ */
227
+ function getChangedTypeScriptFiles(workspaceRoot: string, base: string): string[] {
228
+ try {
229
+ // Use two-dot diff (base to working tree) - same as nx affected
230
+ const output = execSync(`git diff --name-only ${base} -- '*.ts' '*.tsx'`, {
231
+ cwd: workspaceRoot,
232
+ encoding: 'utf-8',
233
+ });
234
+ return output
235
+ .trim()
236
+ .split('\n')
237
+ .filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
238
+ } catch {
239
+ return [];
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Parse a date string in yyyy/mm/dd format and return a Date object.
245
+ * Returns null if the format is invalid.
246
+ */
247
+ function parseDisableDate(dateStr: string): Date | null {
248
+ // Match yyyy/mm/dd format
249
+ const match = dateStr.match(/^(\d{4})\/(\d{2})\/(\d{2})$/);
250
+ if (!match) return null;
251
+
252
+ const year = parseInt(match[1], 10);
253
+ const month = parseInt(match[2], 10) - 1; // JS months are 0-indexed
254
+ const day = parseInt(match[3], 10);
255
+
256
+ const date = new Date(year, month, day);
257
+
258
+ // Validate the date is valid (e.g., not Feb 30)
259
+ if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
260
+ return null;
261
+ }
262
+
263
+ return date;
264
+ }
265
+
266
+ /**
267
+ * Check if a date is within the last month (not expired).
268
+ */
269
+ function isDateWithinMonth(date: Date): boolean {
270
+ const now = new Date();
271
+ const oneMonthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
272
+ return date >= oneMonthAgo;
273
+ }
274
+
275
+ interface DisableStatus {
276
+ hasDisable: boolean;
277
+ isValid: boolean;
278
+ isExpired: boolean;
279
+ date?: string;
280
+ }
281
+
282
+ /**
283
+ * Check if a file has a valid, non-expired disable comment at the top (within first 5 lines).
284
+ * Returns status object with details about the disable comment.
285
+ */
286
+ // webpieces-disable max-lines-new-methods -- Date validation logic requires checking multiple conditions
287
+ function checkDisableComment(content: string): DisableStatus {
288
+ const lines = content.split('\n').slice(0, 5);
289
+
290
+ for (const line of lines) {
291
+ if (line.includes('webpieces-disable') && line.includes('max-lines-modified-files')) {
292
+ // Found disable comment, now check for date
293
+ // Format: // webpieces-disable max-lines-modified-files yyyy/mm/dd -- reason
294
+ const dateMatch = line.match(/max-lines-modified-files\s+(\d{4}\/\d{2}\/\d{2}|XXXX\/XX\/XX)/);
295
+
296
+ if (!dateMatch) {
297
+ // No date found - invalid disable comment
298
+ return { hasDisable: true, isValid: false, isExpired: false };
299
+ }
300
+
301
+ const dateStr = dateMatch[1];
302
+
303
+ // Secret permanent disable
304
+ if (dateStr === 'XXXX/XX/XX') {
305
+ return { hasDisable: true, isValid: true, isExpired: false, date: dateStr };
306
+ }
307
+
308
+ const date = parseDisableDate(dateStr);
309
+ if (!date) {
310
+ // Invalid date format
311
+ return { hasDisable: true, isValid: false, isExpired: false, date: dateStr };
312
+ }
313
+
314
+ if (!isDateWithinMonth(date)) {
315
+ // Date is expired (older than 1 month)
316
+ return { hasDisable: true, isValid: true, isExpired: true, date: dateStr };
317
+ }
318
+
319
+ // Valid and not expired
320
+ return { hasDisable: true, isValid: true, isExpired: false, date: dateStr };
321
+ }
322
+ }
323
+
324
+ return { hasDisable: false, isValid: false, isExpired: false };
325
+ }
326
+
327
+ /**
328
+ * Count lines in a file and check for violations
329
+ */
330
+ // webpieces-disable max-lines-new-methods -- File iteration with disable checking logic
331
+ function findViolations(workspaceRoot: string, changedFiles: string[], maxLines: number, forceLimit: boolean): FileViolation[] {
332
+ const violations: FileViolation[] = [];
333
+
334
+ for (const file of changedFiles) {
335
+ const fullPath = path.join(workspaceRoot, file);
336
+
337
+ if (!fs.existsSync(fullPath)) continue;
338
+
339
+ const content = fs.readFileSync(fullPath, 'utf-8');
340
+ const lineCount = content.split('\n').length;
341
+
342
+ // Skip files under the limit
343
+ if (lineCount <= maxLines) continue;
344
+
345
+ // When forceLimit is true, ignore all disable comments
346
+ if (forceLimit) {
347
+ violations.push({ file, lines: lineCount });
348
+ continue;
349
+ }
350
+
351
+ // Check for disable comment
352
+ const disableStatus = checkDisableComment(content);
353
+
354
+ if (disableStatus.hasDisable) {
355
+ if (disableStatus.isValid && !disableStatus.isExpired) {
356
+ // Valid, non-expired disable - skip this file
357
+ continue;
358
+ }
359
+
360
+ if (disableStatus.isExpired) {
361
+ // Expired disable - report as violation with expired info
362
+ violations.push({
363
+ file,
364
+ lines: lineCount,
365
+ expiredDisable: true,
366
+ expiredDate: disableStatus.date,
367
+ });
368
+ continue;
369
+ }
370
+
371
+ // Invalid disable (missing/bad date) - fall through to report as violation
372
+ }
373
+
374
+ violations.push({
375
+ file,
376
+ lines: lineCount,
377
+ });
378
+ }
379
+
380
+ return violations;
381
+ }
382
+
383
+ /**
384
+ * Auto-detect the base branch by finding the merge-base with origin/main.
385
+ */
386
+ function detectBase(workspaceRoot: string): string | null {
387
+ try {
388
+ const mergeBase = execSync('git merge-base HEAD origin/main', {
389
+ cwd: workspaceRoot,
390
+ encoding: 'utf-8',
391
+ stdio: ['pipe', 'pipe', 'pipe'],
392
+ }).trim();
393
+
394
+ if (mergeBase) {
395
+ return mergeBase;
396
+ }
397
+ } catch {
398
+ try {
399
+ const mergeBase = execSync('git merge-base HEAD main', {
400
+ cwd: workspaceRoot,
401
+ encoding: 'utf-8',
402
+ stdio: ['pipe', 'pipe', 'pipe'],
403
+ }).trim();
404
+
405
+ if (mergeBase) {
406
+ return mergeBase;
407
+ }
408
+ } catch {
409
+ // Ignore
410
+ }
411
+ }
412
+ return null;
413
+ }
414
+
415
+ /**
416
+ * Get today's date in yyyy/mm/dd format for error messages
417
+ */
418
+ function getTodayDateString(): string {
419
+ const now = new Date();
420
+ const year = now.getFullYear();
421
+ const month = String(now.getMonth() + 1).padStart(2, '0');
422
+ const day = String(now.getDate()).padStart(2, '0');
423
+ return `${year}/${month}/${day}`;
424
+ }
425
+
426
+ /**
427
+ * Report violations to console
428
+ */
429
+ // webpieces-disable max-lines-new-methods -- Error output formatting with multiple message sections
430
+ function reportViolations(violations: FileViolation[], maxLines: number, forceLimit: boolean): void {
431
+ console.error('');
432
+ console.error('āŒ YOU MUST FIX THIS AND NOT be more than ' + maxLines + ' lines of code per file');
433
+ console.error(' as it slows down IDEs AND is VERY VERY EASY to refactor.');
434
+ console.error('');
435
+ console.error('šŸ“š With stateless systems + dependency injection, refactor is trivial:');
436
+ console.error(' Pick a method or a few and move to new class XXXXX, then inject XXXXX');
437
+ console.error(' into all users of those methods via the constructor.');
438
+ console.error(' Delete those methods from original class.');
439
+ console.error(' 99% of files can be less than ' + maxLines + ' lines of code.');
440
+ console.error('');
441
+ console.error('āš ļø *** READ tmp/webpieces/webpieces.filesize.md for detailed guidance on how to fix this easily *** āš ļø');
442
+ console.error('');
443
+
444
+ for (const v of violations) {
445
+ if (v.expiredDisable) {
446
+ console.error(` āŒ ${v.file} (${v.lines} lines, max: ${maxLines})`);
447
+ console.error(` ā° EXPIRED DISABLE: Your disable comment dated ${v.expiredDate} has expired (>1 month old).`);
448
+ console.error(` You must either FIX the file or UPDATE the date to get another month.`);
449
+ } else {
450
+ console.error(` āŒ ${v.file} (${v.lines} lines, max: ${maxLines})`);
451
+ }
452
+ }
453
+ console.error('');
454
+
455
+ // Only show escape hatch instructions when forceLimit is not enabled
456
+ if (!forceLimit) {
457
+ console.error(' You can disable this error, but you will be forced to fix again in 1 month');
458
+ console.error(' since 99% of files can be less than ' + maxLines + ' lines of code.');
459
+ console.error('');
460
+ console.error(' Use escape with DATE (expires in 1 month):');
461
+ console.error(` // webpieces-disable max-lines-modified-files ${getTodayDateString()} -- [your reason]`);
462
+ console.error('');
463
+ } else {
464
+ console.error(' āš ļø forceModifiedFilesLimit is enabled - disable comments are NOT allowed.');
465
+ console.error(' You MUST refactor to reduce file size.');
466
+ console.error('');
467
+ }
468
+ }
469
+
470
+ export default async function runExecutor(
471
+ options: ValidateModifiedFilesOptions,
472
+ context: ExecutorContext
473
+ ): Promise<ExecutorResult> {
474
+ const workspaceRoot = context.root;
475
+ const maxLines = options.max ?? 900;
476
+ const forceLimit = options.forceLimit ?? false;
477
+
478
+ let base = process.env['NX_BASE'];
479
+
480
+ if (!base) {
481
+ base = detectBase(workspaceRoot) ?? undefined;
482
+
483
+ if (!base) {
484
+ console.log('\nā­ļø Skipping modified files validation (could not detect base branch)');
485
+ console.log(' To run explicitly: nx affected --target=validate-modified-files --base=origin/main');
486
+ console.log('');
487
+ return { success: true };
488
+ }
489
+
490
+ console.log('\nšŸ“ Validating Modified File Sizes (auto-detected base)\n');
491
+ } else {
492
+ console.log('\nšŸ“ Validating Modified File Sizes\n');
493
+ }
494
+
495
+ console.log(` Base: ${base}`);
496
+ console.log(' Comparing to: working tree (includes uncommitted changes)');
497
+ console.log(` Max lines for modified files: ${maxLines}`);
498
+ if (forceLimit) {
499
+ console.log(' Force limit: ENABLED (disable comments will be ignored)');
500
+ }
501
+ console.log('');
502
+
503
+ try {
504
+ const changedFiles = getChangedTypeScriptFiles(workspaceRoot, base);
505
+
506
+ if (changedFiles.length === 0) {
507
+ console.log('āœ… No TypeScript files changed');
508
+ return { success: true };
509
+ }
510
+
511
+ console.log(`šŸ“‚ Checking ${changedFiles.length} changed file(s)...`);
512
+
513
+ const violations = findViolations(workspaceRoot, changedFiles, maxLines, forceLimit);
514
+
515
+ if (violations.length === 0) {
516
+ console.log('āœ… All modified files are under ' + maxLines + ' lines');
517
+ return { success: true };
518
+ }
519
+
520
+ writeTmpInstructions(workspaceRoot);
521
+ reportViolations(violations, maxLines, forceLimit);
522
+ return { success: false };
523
+ } catch (err: unknown) {
524
+ const error = err instanceof Error ? err : new Error(String(err));
525
+ console.error('āŒ Modified files validation failed:', error.message);
526
+ return { success: false };
527
+ }
528
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "title": "Validate Modified Files Executor",
4
+ "description": "Validates that modified files don't exceed a maximum line count. Encourages keeping files small and focused.",
5
+ "type": "object",
6
+ "properties": {
7
+ "max": {
8
+ "type": "number",
9
+ "description": "Maximum number of lines allowed for modified files",
10
+ "default": 900
11
+ },
12
+ "forceLimit": {
13
+ "type": "boolean",
14
+ "description": "When true, disable comments are ignored and the limit is strictly enforced",
15
+ "default": false
16
+ }
17
+ },
18
+ "required": []
19
+ }
@@ -14,7 +14,9 @@
14
14
  * Usage:
15
15
  * nx affected --target=validate-modified-methods --base=origin/main
16
16
  *
17
- * Escape hatch: Add webpieces-disable max-lines-new-and-modified comment with justification
17
+ * Escape hatch: Add webpieces-disable max-lines-modified comment with date and justification
18
+ * Format: // webpieces-disable max-lines-modified 2025/01/15 -- [reason]
19
+ * The disable expires after 1 month from the date specified.
18
20
  */
19
21
  import type { ExecutorContext } from '@nx/devkit';
20
22
  export interface ValidateModifiedMethodsOptions {