@adonisjs/assembler 8.0.0-next.9 → 8.0.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 (34) hide show
  1. package/README.md +260 -0
  2. package/build/codemod_exception-CzQgXAAf.js +137 -0
  3. package/build/index.d.ts +1 -1
  4. package/build/index.js +927 -1724
  5. package/build/source-dVeugJ0e.js +166 -0
  6. package/build/src/bundler.d.ts +2 -0
  7. package/build/src/code_scanners/routes_scanner/main.d.ts +16 -2
  8. package/build/src/code_scanners/routes_scanner/main.js +168 -441
  9. package/build/src/code_transformer/main.d.ts +14 -1
  10. package/build/src/code_transformer/main.js +502 -622
  11. package/build/src/code_transformer/rc_file_transformer.d.ts +28 -2
  12. package/build/src/debug.d.ts +1 -1
  13. package/build/src/dev_server.d.ts +60 -12
  14. package/build/src/exceptions/codemod_exception.d.ts +178 -0
  15. package/build/src/file_buffer.d.ts +19 -0
  16. package/build/src/file_system.d.ts +2 -2
  17. package/build/src/helpers.js +72 -16
  18. package/build/src/index_generator/main.js +28 -6
  19. package/build/src/paths_resolver.d.ts +2 -1
  20. package/build/src/test_runner.d.ts +3 -2
  21. package/build/src/types/code_scanners.d.ts +29 -13
  22. package/build/src/types/code_transformer.d.ts +127 -0
  23. package/build/src/types/common.d.ts +98 -2
  24. package/build/src/types/hooks.d.ts +4 -1
  25. package/build/src/types/main.js +1 -0
  26. package/build/src/utils.d.ts +9 -3
  27. package/build/src/virtual_file_system.d.ts +1 -1
  28. package/build/validator_extractor-Ccio_Ndi.js +82 -0
  29. package/build/virtual_file_system-bGeoWsK-.js +285 -0
  30. package/package.json +41 -39
  31. package/build/chunk-7XU453QB.js +0 -418
  32. package/build/chunk-PORDZS62.js +0 -391
  33. package/build/chunk-TIKQQRMX.js +0 -116
  34. package/build/src/hooks.d.ts +0 -224
package/README.md CHANGED
@@ -420,6 +420,266 @@ export const policies = {
420
420
  }
421
421
  ```
422
422
 
423
+ ### addValidator
424
+ Create a new validator file or add a validator to an existing file. If the file does not exist, it will be created with the provided contents. If it exists and the export name is not already defined, the validator will be appended to the file.
425
+
426
+ > [!IMPORTANT]
427
+ > This codemod respects the `validators` directory configured in `adonisrc.ts` and defaults to `app/validators`.
428
+
429
+ ```ts
430
+ const transformer = new CodeTransformer(appRoot)
431
+
432
+ try {
433
+ await transformer.addValidator({
434
+ validatorFileName: 'user.ts',
435
+ exportName: 'loginValidator',
436
+ contents: `export const loginValidator = vine.compile(
437
+ vine.object({
438
+ email: vine.string().email(),
439
+ password: vine.string().minLength(8)
440
+ })
441
+ )`
442
+ })
443
+ } catch (error) {
444
+ console.error('Unable to add validator')
445
+ console.error(error)
446
+ }
447
+ ```
448
+
449
+ Output (app/validators/user.ts)
450
+
451
+ ```ts
452
+ export const loginValidator = vine.compile(
453
+ vine.object({
454
+ email: vine.string().email(),
455
+ password: vine.string().minLength(8)
456
+ })
457
+ )
458
+ ```
459
+
460
+ ### addLimiter
461
+ Create a new rate limiter file or add a limiter to an existing file. If the file does not exist, it will be created with the provided contents. If it exists and the export name is not already defined, the limiter will be appended to the file.
462
+
463
+ > [!IMPORTANT]
464
+ > Limiters are created in the `start` directory configured in `adonisrc.ts` and defaults to `start`.
465
+
466
+ ```ts
467
+ const transformer = new CodeTransformer(appRoot)
468
+
469
+ try {
470
+ await transformer.addLimiter({
471
+ limiterFileName: 'limiters.ts',
472
+ exportName: 'apiThrottle',
473
+ contents: `export const apiThrottle = limiter.define('api', () => {
474
+ return limiter.allowRequests(10).every('1 minute')
475
+ })`
476
+ })
477
+ } catch (error) {
478
+ console.error('Unable to add limiter')
479
+ console.error(error)
480
+ }
481
+ ```
482
+
483
+ Output (start/limiters.ts)
484
+
485
+ ```ts
486
+ export const apiThrottle = limiter.define('api', () => {
487
+ return limiter.allowRequests(10).every('1 minute')
488
+ })
489
+ ```
490
+
491
+ ### addModelMixins
492
+ Apply one or more mixins to a model class. This wraps the model's extends clause with the `compose` helper and applies the specified mixins.
493
+
494
+ > [!IMPORTANT]
495
+ > This codemod expects the model file to exist with a default exported class that extends a base class.
496
+
497
+ ```ts
498
+ const transformer = new CodeTransformer(appRoot)
499
+
500
+ try {
501
+ await transformer.addModelMixins('user.ts', [
502
+ {
503
+ name: 'SoftDeletes',
504
+ importPath: '@adonisjs/lucid/orm/mixins/soft_deletes',
505
+ importType: 'named'
506
+ },
507
+ {
508
+ name: 'Sluggable',
509
+ importPath: '#mixins/sluggable',
510
+ importType: 'default',
511
+ args: ['title', '{ strategy: "dbIncrement" }']
512
+ }
513
+ ])
514
+ } catch (error) {
515
+ console.error('Unable to add mixins to model')
516
+ console.error(error)
517
+ }
518
+ ```
519
+
520
+ Input (app/models/user.ts)
521
+
522
+ ```ts
523
+ import { BaseModel } from '@adonisjs/lucid/orm'
524
+
525
+ export default class User extends BaseModel {
526
+ // ...
527
+ }
528
+ ```
529
+
530
+ Output (app/models/user.ts)
531
+
532
+ ```ts
533
+ import { BaseModel } from '@adonisjs/lucid/orm'
534
+ import { compose } from '@adonisjs/core/helpers'
535
+ import { SoftDeletes } from '@adonisjs/lucid/orm/mixins/soft_deletes'
536
+ import Sluggable from '#mixins/sluggable'
537
+
538
+ export default class User extends compose(BaseModel, SoftDeletes(), Sluggable(title, { strategy: "dbIncrement" })) {
539
+ // ...
540
+ }
541
+ ```
542
+
543
+ ### addControllerMethod
544
+ Create a new controller file or add a method to an existing controller class. If the controller file does not exist, it will be created with the class and method. If it exists and the method is not already defined, the method will be added to the class.
545
+
546
+ > [!IMPORTANT]
547
+ > This codemod respects the `controllers` directory configured in `adonisrc.ts` and defaults to `app/controllers`.
548
+
549
+ ```ts
550
+ const transformer = new CodeTransformer(appRoot)
551
+
552
+ try {
553
+ await transformer.addControllerMethod({
554
+ controllerFileName: 'users_controller.ts',
555
+ className: 'UsersController',
556
+ name: 'destroy',
557
+ contents: `async destroy({ params, response }: HttpContext) {
558
+ const user = await User.findOrFail(params.id)
559
+ await user.delete()
560
+ return response.noContent()
561
+ }`,
562
+ imports: [
563
+ { isType: false, isNamed: true, name: 'HttpContext', path: '@adonisjs/core/http' },
564
+ { isType: false, isNamed: false, name: 'User', path: '#models/user' }
565
+ ]
566
+ })
567
+ } catch (error) {
568
+ console.error('Unable to add controller method')
569
+ console.error(error)
570
+ }
571
+ ```
572
+
573
+ Output (app/controllers/users_controller.ts)
574
+
575
+ ```ts
576
+ import type { HttpContext } from '@adonisjs/core/http'
577
+ import User from '#models/user'
578
+
579
+ export default class UsersController {
580
+ async destroy({ params, response }: HttpContext) {
581
+ const user = await User.findOrFail(params.id)
582
+ await user.delete()
583
+ return response.noContent()
584
+ }
585
+ }
586
+ ```
587
+
588
+ ### RcFileTransformer additional methods
589
+
590
+ The `RcFileTransformer` class (accessible via `updateRcFile` callback) now supports additional methods for managing imports and hooks.
591
+
592
+ #### addNamedImport
593
+ Add a named import to the `adonisrc.ts` file.
594
+
595
+ ```ts
596
+ const transformer = new CodeTransformer(appRoot)
597
+
598
+ try {
599
+ await transformer.updateRcFile((rcFile) => {
600
+ rcFile.addNamedImport('@adonisjs/core/types', ['Middleware', 'Provider'])
601
+ })
602
+ } catch (error) {
603
+ console.error('Unable to add named import')
604
+ console.error(error)
605
+ }
606
+ ```
607
+
608
+ Output
609
+
610
+ ```ts
611
+ import { defineConfig } from '@adonisjs/core/app'
612
+ import { Middleware, Provider } from '@adonisjs/core/types'
613
+
614
+ export default defineConfig({
615
+ // ...
616
+ })
617
+ ```
618
+
619
+ #### addDefaultImport
620
+ Add a default import to the `adonisrc.ts` file.
621
+
622
+ ```ts
623
+ const transformer = new CodeTransformer(appRoot)
624
+
625
+ try {
626
+ await transformer.updateRcFile((rcFile) => {
627
+ rcFile.addDefaultImport('#config/database', 'databaseConfig')
628
+ })
629
+ } catch (error) {
630
+ console.error('Unable to add default import')
631
+ console.error(error)
632
+ }
633
+ ```
634
+
635
+ Output
636
+
637
+ ```ts
638
+ import { defineConfig } from '@adonisjs/core/app'
639
+ import databaseConfig from '#config/database'
640
+
641
+ export default defineConfig({
642
+ // ...
643
+ })
644
+ ```
645
+
646
+ #### addAssemblerHook
647
+ Add assembler hooks to the `adonisrc.ts` file. Hooks can be added as thunk imports (lazy loaded) or as raw values for direct import references.
648
+
649
+ ```ts
650
+ const transformer = new CodeTransformer(appRoot)
651
+
652
+ try {
653
+ await transformer.updateRcFile((rcFile) => {
654
+ // Add a thunk-style hook (lazy import)
655
+ rcFile.addAssemblerHook('onBuildStarting', './commands/build_hook.js')
656
+
657
+ // Add a raw hook (direct import reference)
658
+ rcFile.addAssemblerHook('onBuildCompleted', 'buildCompletedHook', true)
659
+ })
660
+ } catch (error) {
661
+ console.error('Unable to add assembler hook')
662
+ console.error(error)
663
+ }
664
+ ```
665
+
666
+ Output
667
+
668
+ ```ts
669
+ import { defineConfig } from '@adonisjs/core/app'
670
+
671
+ export default defineConfig({
672
+ hooks: {
673
+ onBuildStarting: [
674
+ () => import('./commands/build_hook.js')
675
+ ],
676
+ onBuildCompleted: [
677
+ buildCompletedHook
678
+ ]
679
+ }
680
+ })
681
+ ```
682
+
423
683
  ## Index generator
424
684
 
425
685
  The `IndexGenerator` is a core concept in Assembler that is used to watch the filesystem and create barrel files or types from a source directory.
@@ -0,0 +1,137 @@
1
+ var CodemodException = class CodemodException extends Error {
2
+ instructions;
3
+ filePath;
4
+ constructor(message, options) {
5
+ super(message);
6
+ this.name = "CodemodException";
7
+ this.instructions = options?.instructions;
8
+ this.filePath = options?.filePath;
9
+ }
10
+ static #formatEnvValidations(definition) {
11
+ const lines = [];
12
+ if (definition.leadingComment) {
13
+ lines.push(`/*`);
14
+ lines.push(`|----------------------------------------------------------`);
15
+ lines.push(`| ${definition.leadingComment}`);
16
+ lines.push(`|----------------------------------------------------------`);
17
+ lines.push(`*/`);
18
+ }
19
+ for (const [variable, validation] of Object.entries(definition.variables)) lines.push(`${variable}: ${validation},`);
20
+ return lines.join("\n");
21
+ }
22
+ static #formatMiddleware(stack, middleware) {
23
+ if (stack === "named") return `export const middleware = router.named({\n ${middleware.filter((m) => m.name).map((m) => `${m.name}: () => import('${m.path}')`).join(",\n ")}\n})`;
24
+ return `${stack}.use([\n ${middleware.map((m) => `() => import('${m.path}')`).join(",\n ")}\n])`;
25
+ }
26
+ static #formatPolicies(policies) {
27
+ return `export const policies = {\n ${policies.map((p) => `${p.name}: () => import('${p.path}')`).join(",\n ")}\n}`;
28
+ }
29
+ static #formatVitePlugin(pluginCall, importDeclarations) {
30
+ return `${importDeclarations.map((decl) => decl.isNamed ? `import { ${decl.identifier} } from '${decl.module}'` : `import ${decl.identifier} from '${decl.module}'`).join("\n")}\n\nexport default defineConfig({\n plugins: [${pluginCall}]\n})`;
31
+ }
32
+ static #formatJapaPlugin(pluginCall, importDeclarations) {
33
+ return `${importDeclarations.map((decl) => decl.isNamed ? `import { ${decl.identifier} } from '${decl.module}'` : `import ${decl.identifier} from '${decl.module}'`).join("\n")}\n\nexport const plugins: Config['plugins'] = [\n ${pluginCall}\n]`;
34
+ }
35
+ static missingEnvFile(filePath, definition) {
36
+ const code = this.#formatEnvValidations(definition);
37
+ return new CodemodException(`Could not find source file at path: "${filePath}"`, {
38
+ filePath,
39
+ instructions: `Add the following code to "${filePath}":\n\n${code}`
40
+ });
41
+ }
42
+ static missingEnvCreate(filePath, definition) {
43
+ return new CodemodException(`Cannot find Env.create statement in the file.`, {
44
+ filePath,
45
+ instructions: `Add the following code inside Env.create() in "${filePath}":\n\n${this.#formatEnvValidations(definition)}`
46
+ });
47
+ }
48
+ static invalidEnvCreate(filePath, definition) {
49
+ return new CodemodException(`The second argument of Env.create is not an object literal.`, {
50
+ filePath,
51
+ instructions: `Add the following code inside Env.create() in "${filePath}":\n\n${this.#formatEnvValidations(definition)}`
52
+ });
53
+ }
54
+ static missingKernelFile(filePath, stack, middleware) {
55
+ const code = this.#formatMiddleware(stack, middleware);
56
+ return new CodemodException(`Could not find source file at path: "${filePath}"`, {
57
+ filePath,
58
+ instructions: `Add the following code to "${filePath}":\n\n${code}`
59
+ });
60
+ }
61
+ static missingMiddlewareStack(filePath, stack, middleware) {
62
+ const code = this.#formatMiddleware(stack, middleware);
63
+ return new CodemodException(`Cannot find ${stack === "named" ? "middleware variable" : `${stack}.use`} statement in the file.`, {
64
+ filePath,
65
+ instructions: `Add the following code to "${filePath}":\n\n${code}`
66
+ });
67
+ }
68
+ static invalidMiddlewareStack(filePath, stack, middleware, reason) {
69
+ return new CodemodException(reason, {
70
+ filePath,
71
+ instructions: `Add the following code to "${filePath}":\n\n${this.#formatMiddleware(stack, middleware)}`
72
+ });
73
+ }
74
+ static missingPoliciesFile(filePath, policies) {
75
+ const code = this.#formatPolicies(policies);
76
+ return new CodemodException(`Could not find source file at path: "${filePath}"`, {
77
+ filePath,
78
+ instructions: `Add the following code to "${filePath}":\n\n${code}`
79
+ });
80
+ }
81
+ static invalidPoliciesFile(filePath, policies, reason) {
82
+ return new CodemodException(reason, {
83
+ filePath,
84
+ instructions: `Add the following code to "${filePath}":\n\n${this.#formatPolicies(policies)}`
85
+ });
86
+ }
87
+ static missingViteConfig(filePath, pluginCall, importDeclarations) {
88
+ return new CodemodException(`Cannot find vite.config.ts file. Make sure to rename vite.config.js to vite.config.ts`, {
89
+ filePath,
90
+ instructions: `Add the following code to "${filePath}":\n\n${this.#formatVitePlugin(pluginCall, importDeclarations)}`
91
+ });
92
+ }
93
+ static invalidViteConfig(filePath, pluginCall, importDeclarations, reason) {
94
+ return new CodemodException(reason, {
95
+ filePath,
96
+ instructions: `Add the following code to "${filePath}":\n\n${this.#formatVitePlugin(pluginCall, importDeclarations)}`
97
+ });
98
+ }
99
+ static missingJapaBootstrap(filePath, pluginCall, importDeclarations) {
100
+ const code = this.#formatJapaPlugin(pluginCall, importDeclarations);
101
+ return new CodemodException(`Could not find source file at path: "${filePath}"`, {
102
+ filePath,
103
+ instructions: `Add the following code to "${filePath}":\n\n${code}`
104
+ });
105
+ }
106
+ static invalidJapaBootstrap(filePath, pluginCall, importDeclarations, reason) {
107
+ return new CodemodException(reason, {
108
+ filePath,
109
+ instructions: `Add the following code to "${filePath}":\n\n${this.#formatJapaPlugin(pluginCall, importDeclarations)}`
110
+ });
111
+ }
112
+ static missingRcFile(filePath, codeToAdd) {
113
+ return new CodemodException(`Could not find source file at path: "${filePath}"`, {
114
+ filePath,
115
+ instructions: `Add the following code to "${filePath}":\n\n${codeToAdd}`
116
+ });
117
+ }
118
+ static invalidRcFile(filePath, codeToAdd, reason) {
119
+ return new CodemodException(reason, {
120
+ filePath,
121
+ instructions: `Add the following code to "${filePath}":\n\n${codeToAdd}`
122
+ });
123
+ }
124
+ static E_CODEMOD_FILE_NOT_FOUND(filePath, codeToAdd) {
125
+ return new CodemodException(`Could not find source file at path: "${filePath}"`, {
126
+ filePath,
127
+ instructions: `Add the following code to "${filePath}":\n\n${codeToAdd}`
128
+ });
129
+ }
130
+ static E_CODEMOD_INVALID_STRUCTURE(message, filePath, codeToAdd) {
131
+ return new CodemodException(message, {
132
+ filePath,
133
+ instructions: `Add the following code to "${filePath}":\n\n${codeToAdd}`
134
+ });
135
+ }
136
+ };
137
+ export { CodemodException as t };
package/build/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export { hooks } from './src/hooks.ts';
2
1
  export { Bundler } from './src/bundler.ts';
3
2
  export { DevServer } from './src/dev_server.ts';
4
3
  export { TestRunner } from './src/test_runner.ts';
5
4
  export { FileBuffer } from './src/file_buffer.ts';
6
5
  export { VirtualFileSystem } from './src/virtual_file_system.ts';
6
+ export { CodemodException } from './src/exceptions/codemod_exception.ts';
7
7
  export { SUPPORTED_PACKAGE_MANAGERS } from './src/bundler.ts';