Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add registerVitePlugin and registerJapaPlugin #4412

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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