diff --git a/modules/ace/codemods.ts b/modules/ace/codemods.ts index 54175c0b..ca11d20b 100644 --- a/modules/ace/codemods.ts +++ b/modules/ace/codemods.ts @@ -211,7 +211,53 @@ export class Codemods extends EventEmitter { } /** - * Generats the stub + * Register a new Vite plugin in the `vite.config.ts` file + */ + async registerVitePlugin(...params: Parameters) { + await this.#importAssembler() + if (!this.#codeTransformer) { + this.#cliLogger.warning( + 'Cannot update "vite.config.ts" file. Install "@adonisjs/assembler" to modify source files' + ) + return + } + + const transformer = new this.#codeTransformer.CodeTransformer(this.#app.appRoot) + const action = this.#cliLogger.action('update vite.config.ts file') + try { + await transformer.addVitePlugin(...params) + action.succeeded() + } catch (error) { + this.emit('error', error) + action.failed(error.message) + } + } + + /** + * Register a new Japa plugin in the `tests/bootstrap.ts` file + */ + async registerJapaPlugin(...params: Parameters) { + await this.#importAssembler() + if (!this.#codeTransformer) { + this.#cliLogger.warning( + 'Cannot update "tests/bootstrap.ts" file. Install "@adonisjs/assembler" to modify source files' + ) + return + } + + const transformer = new this.#codeTransformer.CodeTransformer(this.#app.appRoot) + const action = this.#cliLogger.action('update tests/bootstrap.ts file') + try { + await transformer.addJapaPlugin(...params) + action.succeeded() + } catch (error) { + this.emit('error', error) + action.failed(error.message) + } + } + + /** + * Generate the stub */ async makeUsingStub(stubsRoot: string, stubPath: string, stubState: Record) { const stubs = await this.#app.stubs.create() diff --git a/tests/ace/codemods.spec.ts b/tests/ace/codemods.spec.ts index 765c0aee..21e54bf6 100644 --- a/tests/ace/codemods.spec.ts +++ b/tests/ace/codemods.spec.ts @@ -166,6 +166,62 @@ test.group('Codemods | registerPolicies', (group) => { }) }) +test.group('Codemods | registerVitePlugin', (group) => { + group.tap((t) => t.timeout(60 * 1000)) + + test('register vite plugin', async ({ assert, fs }) => { + const ace = await new AceFactory().make(fs.baseUrl) + await ace.app.init() + ace.ui.switchMode('raw') + + await fs.createJson('tsconfig.json', {}) + await fs.createJson('package.json', {}) + await fs.create('vite.config.ts', 'export default { plugins: [] }') + + const codemods = new Codemods(ace.app, ace.ui.logger) + await codemods.registerVitePlugin('vue()', [ + { identifier: 'vue', module: '@vitejs/plugin-vue', isNamed: false }, + ]) + + assert.deepEqual(ace.ui.logger.getLogs(), [ + { + message: 'green(DONE:) update vite.config.ts file', + stream: 'stdout', + }, + ]) + + await assert.fileContains('vite.config.ts', 'vue()') + }) +}) + +test.group('Codemods | registerJapaPlugin', (group) => { + group.tap((t) => t.timeout(60 * 1000)) + + test('register japa plugin', async ({ assert, fs }) => { + const ace = await new AceFactory().make(fs.baseUrl) + await ace.app.init() + ace.ui.switchMode('raw') + + await fs.createJson('tsconfig.json', {}) + await fs.createJson('package.json', {}) + await fs.create('tests/bootstrap.ts', 'export const plugins = []') + + const codemods = new Codemods(ace.app, ace.ui.logger) + await codemods.registerJapaPlugin('apiClient()', [ + { identifier: 'apiClient', module: '@japa/api-client', isNamed: true }, + ]) + + assert.deepEqual(ace.ui.logger.getLogs(), [ + { + message: 'green(DONE:) update tests/bootstrap.ts file', + stream: 'stdout', + }, + ]) + + await assert.fileContains('tests/bootstrap.ts', 'apiClient()') + }) +}) + test.group('Codemods | install packages', (group) => { group.tap((t) => t.timeout(60 * 1000))