Codemods
Codemods class for programmatically modifying AdonisJS source files. This class provides APIs to modify configuration files, register middleware, generate stubs, and install packages. The codemod APIs rely on the@adonisjs/assembler package, which must be installed as a dependency in the user application.
Import
Constructor
The AdonisJS application instance
CLI logger instance for displaying operation results
Example
Properties
Overwrite existing files when generating files from stubs. Default:
falseDisplay verbose logs for package installation. Default:
falseMethods
defineEnvVariables()
Define one or more environment variables in the.env file.
Key-value pairs of environment variables to add
Configuration options:
omitFromExample?: Array<keyof T>- Keys to exclude from.env.examplefile
Promise<void>
getTsMorphProject()
Returns the TsMorph project instance for advanced AST manipulations. See ts-morph documentation for more information. Returns:Promise<CodeTransformer['project'] | undefined>
defineEnvValidations()
Define validations for environment variables in thestart/env.ts file. This method updates the environment validation schema using the assembler.
Validation schema node for environment variables
Promise<void>
registerMiddleware()
Register middleware in thestart/kernel.ts file. This method adds middleware to the specified stack (server, router, or named).
The middleware stack to register to
Array of middleware nodes to register
Promise<void>
MiddlewareNode Type
registerPolicies()
Register bouncer policies to the list of policies collection exported from theapp/policies/main.ts file. This method adds new policy definitions to the policies export.
Array of policy nodes to register
Promise<void>
BouncerPolicyNode Type
updateRcFile()
Update theadonisrc.ts file with new configuration settings. This method allows modification of the AdonisJS runtime configuration.
Callback function that receives the RC file object for modification
Promise<void>
registerVitePlugin()
Register a new Vite plugin in thevite.config.ts file. This method adds plugin configuration to the Vite build configuration.
Configuration for the Vite plugin to add
Promise<void>
registerJapaPlugin()
Register a new Japa plugin in thetests/bootstrap.ts file. This method adds plugin configuration to the test runner setup.
Configuration for the Japa plugin to add
Promise<void>
addValidator()
Add a new validator file to the validators directory. This method creates a new validator file with the provided definition.The filename for the validator (without extension)
The name of the exported validator
The validator file contents
Promise<void>
addLimiter()
Add a new rate limiter file to the limiters directory. This method creates a new limiter file with the provided definition.The filename for the limiter (without extension)
The name of the exported limiter
The limiter file contents
Promise<void>
addModelMixins()
Add mixins to a model class. This method adds mixin calls to the specified model file.The model filename (e.g., ‘user’)
Array of mixin configurations to add
Promise<void>
ModelMixinNode Type
addControllerMethod()
Add a new method to an existing controller class. This method injects a new method into the specified controller file.The controller filename (e.g., ‘users_controller’)
The controller class name (e.g., ‘UsersController’)
The method name to add
The method implementation code
Array of imports required by the new method
Promise<void>
makeUsingStub()
Generate a file using a stub template.Root directory containing stub files
Path to the specific stub file
Template variables for stub generation
Optional configuration:
contentsFromFile?: string- Override stub contents with file contents
Promise<GeneratedStub>
GeneratedStub Type
installPackages()
Install packages using the detected or specified package manager. Automatically detects npm, yarn, or pnpm and installs dependencies accordingly. You can specify version of each package by setting it in the name like@adonisjs/lucid@^20.0.0.
Array of packages with their dependency type
Optional package manager to use (auto-detected if not provided). Options:
'npm', 'yarn', 'yarn@berry', 'pnpm', 'pnpm@6', 'deno'Promise<boolean>
listPackagesToInstall()
List the packages that should be installed manually. This method displays installation commands for different package managers when automatic installation is not available or desired.Array of packages with their dependency type
Promise<void>
Events
TheCodemods class extends EventEmitter and emits the following events:
error
Emitted when a codemod operation fails.Complete Example
See Also
- Kernel API - Manage and execute commands
- Command API - Create custom Ace commands