import { EventEmitter } from 'node:events'; import type { UIPrimitives } from '@adonisjs/ace/types'; import type { CodeTransformer } from '@adonisjs/assembler/code_transformer'; import type { MiddlewareNode, EnvValidationNode, BouncerPolicyNode, SupportedPackageManager } from '@adonisjs/assembler/types'; import type { Application } from '../app.ts'; import { type GeneratedStub } from '../../types/app.ts'; /** * 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. * * @example * ```ts * const codemods = new Codemods(app, logger) * * // Generate a controller from a stub * await codemods.makeUsingStub(stubsRoot, 'controller.stub', { * filename: 'UserController', * entity: { name: 'User' } * }) * * // Install packages * await codemods.installPackages([ * { name: '@adonisjs/lucid', isDevDependency: false } * ]) * ``` */ export declare class Codemods extends EventEmitter { #private; /** * Overwrite existing files when generating files * from stubs */ overwriteExisting: boolean; /** * Display verbose logs for package installation */ verboseInstallOutput: boolean; constructor(app: Application, cliLogger: UIPrimitives['logger']); /** * Replace the logger used for all subsequent codemod * operations. Useful to suppress output by passing a * dummy logger when running inside a tasks manager. */ useLogger(logger: UIPrimitives['logger']): this; /** * Define one or more environment variables in the .env file * * @param environmentVariables - Key-value pairs of environment variables * @param options - Configuration options * @param options.omitFromExample - Keys to exclude from .env.example file * * @example * ```ts * await codemods.defineEnvVariables({ * DB_CONNECTION: 'mysql', * DB_HOST: 'localhost', * SECRET_KEY: 'abc123' * }, { * omitFromExample: ['SECRET_KEY'] * }) * ``` */ defineEnvVariables>(environmentVariables: T, options?: { omitFromExample?: Array; }): Promise; /** * Returns the TsMorph project instance for advanced AST manipulations. * See https://ts-morph.com/ for documentation. * * @example * ```ts * const project = await codemods.getTsMorphProject() * if (project) { * const sourceFile = project.getSourceFile('app/controllers/user_controller.ts') * // Perform advanced AST operations * } * ``` */ getTsMorphProject(): Promise; /** * Define validations for the environment variables in the start/env.ts file. * This method updates the environment validation schema using the assembler. * * @param validations - Validation schema node for environment variables * * @example * ```ts * await codemods.defineEnvValidations({ * NODE_ENV: 'Env.schema.enum(["development", "production", "test"] as const)', * PORT: 'Env.schema.number()', * HOST: 'Env.schema.string({ format: "host" })' * }) * ``` */ defineEnvValidations(validations: EnvValidationNode): Promise; /** * Register middleware in the start/kernel.ts file. * This method adds middleware to the specified stack (server, router, or named). * * @param stack - The middleware stack to register to ('server' | 'router' | 'named') * @param middleware - Array of middleware nodes to register * * @example * ```ts * await codemods.registerMiddleware('server', [ * { * name: 'cors', * path: '@adonisjs/cors/cors_middleware' * } * ]) * ``` */ registerMiddleware(stack: 'server' | 'router' | 'named', middleware: MiddlewareNode[]): Promise; /** * Register bouncer policies to the list of policies collection exported from * the "app/policies/main.ts" file. This method adds new policy definitions * to the policies export. * * @param policies - Array of policy nodes to register * * @example * ```ts * await codemods.registerPolicies([ * { * name: 'UserPolicy', * path: '#policies/user_policy' * } * ]) * ``` */ registerPolicies(policies: BouncerPolicyNode[]): Promise; /** * Update the adonisrc.ts file with new configuration settings. * This method allows modification of the AdonisJS runtime configuration. * * @param params - Parameters for updating the RC file (varies based on update type) * * @example * ```ts * await codemods.updateRcFile((rcFile) => { * rcFile.addCommand('make:custom') * rcFile.addPreloadFile('#app/events/main') * }) * ``` */ updateRcFile(...params: Parameters): Promise; /** * Register a new Vite plugin in the vite.config.ts file. * This method adds plugin configuration to the Vite build configuration. * * @param params - Parameters for adding the Vite plugin (varies based on plugin type) * * @example * ```ts * await codemods.registerVitePlugin({ * name: 'vue', * import: 'import vue from "@vitejs/plugin-vue"', * options: '()' * }) * ``` */ registerVitePlugin(...params: Parameters): Promise; /** * Register a new Japa plugin in the tests/bootstrap.ts file. * This method adds plugin configuration to the test runner setup. * * @param params - Parameters for adding the Japa plugin (varies based on plugin type) * * @example * ```ts * await codemods.registerJapaPlugin({ * name: 'expect', * import: 'import { expect } from "@japa/expect"' * }) * ``` */ registerJapaPlugin(...params: Parameters): Promise; /** * Add a new validator file to the validators directory. * This method creates a new validator file with the provided definition. * * @param params - Parameters for adding the validator * * @example * ```ts * await codemods.addValidator({ * validatorFileName: 'create_user', * exportName: 'createUserValidator', * contents: 'export const createUserValidator = vine.compile(...)' * }) * ``` */ addValidator(...params: Parameters): Promise; /** * Add a new rate limiter file to the limiters directory. * This method creates a new limiter file with the provided definition. * * @param params - Parameters for adding the limiter * * @example * ```ts * await codemods.addLimiter({ * limiterFileName: 'api_throttle', * exportName: 'apiThrottleLimiter', * contents: 'export const apiThrottleLimiter = limiter.define(...)' * }) * ``` */ addLimiter(...params: Parameters): Promise; /** * Add mixins to a model class. * This method adds mixin calls to the specified model file. * * @param params - Parameters for adding model mixins (modelFileName, mixins array) * * @example * ```ts * await codemods.addModelMixins('user', [ * { * name: 'SoftDeletes', * importPath: '@adonisjs/lucid/mixins/soft_deletes', * importType: 'named' * } * ]) * ``` */ addModelMixins(...params: Parameters): Promise; /** * Add a new method to an existing controller class. * This method injects a new method into the specified controller file. * * @param params - Parameters for adding the controller method * * @example * ```ts * await codemods.addControllerMethod({ * controllerFileName: 'users_controller', * className: 'UsersController', * name: 'destroy', * contents: 'async destroy({ params, response }: HttpContext) { ... }', * imports: [ * { isType: false, isNamed: true, name: 'HttpContext', path: '@adonisjs/core/http' } * ] * }) * ``` */ addControllerMethod(...params: Parameters): Promise; /** * Generate a file using a stub template * * @param stubsRoot - Root directory containing stub files * @param stubPath - Path to the specific stub file * @param stubState - Template variables for stub generation * * @example * ```ts * const result = await codemods.makeUsingStub( * './stubs', * 'controller.stub', * { * filename: 'UserController', * entity: { name: 'User', modelName: 'User' }, * resourceful: true * } * ) * ``` */ makeUsingStub(stubsRoot: string, stubPath: string, stubState: Record, options?: { contentsFromFile?: string; }): Promise; /** * 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'. * * @param packages - Array of packages with their dependency type * @param packageManager - Optional package manager to use (auto-detected if not provided) * * @example * ```ts * const success = await codemods.installPackages([ * { name: '@adonisjs/lucid', isDevDependency: false }, * { name: '@types/node', isDevDependency: true } * ]) * ``` */ installPackages(packages: { name: string; isDevDependency: boolean; }[], packageManager?: SupportedPackageManager | 'pnpm@6' | 'deno'): Promise; /** * 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. * * @param packages - Array of packages with their dependency type * * @example * ```ts * await codemods.listPackagesToInstall([ * { name: '@adonisjs/lucid', isDevDependency: false }, * { name: '@types/node', isDevDependency: true } * ]) * // Output: * // Please install following packages * // npm i -D @types/node * // npm i @adonisjs/lucid * ``` */ listPackagesToInstall(packages: { name: string; isDevDependency: boolean; }[]): Promise; }