Skip to content

Commit

Permalink
feat: add registerVitePlugin and registerJapaPlugin (#4412)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 authored Feb 24, 2024
1 parent be6ca03 commit db03e4c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
48 changes: 47 additions & 1 deletion modules/ace/codemods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeTransformer['addVitePlugin']>) {
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<CodeTransformer['addJapaPlugin']>) {
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<string, any>) {
const stubs = await this.#app.stubs.create()
Expand Down
56 changes: 56 additions & 0 deletions tests/ace/codemods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit db03e4c

Please sign in to comment.