Skip to content

Commit

Permalink
feat: add addAssemblerHook codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Jun 2, 2024
1 parent c9d6fab commit 097bdb0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/code_transformer/rc_file_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { fileURLToPath } from 'node:url'
import type { AppEnvironments } from '@adonisjs/application/types'
import type { AppEnvironments, RcFile } from '@adonisjs/application/types'
import {
Node,
Project,
Expand Down Expand Up @@ -346,6 +346,30 @@ export class RcFileTransformer {
return this
}

/**
* Add a new assembler hook
*/
addAssemblerHook(type: keyof NonNullable<RcFile['hooks']>, path: string) {
const hooksProperty = this.#getPropertyAssignmentInDefineConfigCall('hooks', '{}')

const hooks = hooksProperty.getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression)
let hookArray = hooks.getProperty(type) as PropertyAssignment
if (!hookArray) {
hooks.addPropertyAssignment({ name: type, initializer: '[]' })
hookArray = hooks.getProperty(type) as PropertyAssignment
}

const hooksArray = hookArray.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression)
const existingHooks = this.#extractModulesFromArray(hooksArray)
if (existingHooks.includes(path)) {
return this
}

hooksArray.addElement(`() => import('${path}')`)

return this
}

/**
* Save the adonisrc.ts file
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/__snapshots__/code_transformer.spec.ts.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,36 @@ export const plugins: Config['plugins'] = [
]
"`

exports[`Code Transformer | addAssemblerHook > add assembler hook to the assembler file 1`] = `"import { defineConfig } from '@adonisjs/core/app'
export default defineConfig({
typescript: true,
preloads: [
() => import('./start/routes.ts'),
{
file: () => import('./start/ace.ts'),
environment: ['console'],
},
],
providers: [
() => import('@adonisjs/core/providers/app_provider'),
{
file: () => import('@adonisjs/core/providers/repl_provider'),
environment: ['repl'],
}
],
metaFiles: [
{
pattern: 'public/**',
reloadServer: true
},
],
commands: [
() => import('@adonisjs/core/commands')
],
hooks: {
onBuildCompleted: [() => import('@adonisjs/vite/hooks/onBuildCompleted')]
}
})
"`

29 changes: 29 additions & 0 deletions tests/code_transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,3 +894,32 @@ test.group('Code transformer | addVitePlugin', (group) => {
await transformer.addVitePlugin('vue()', [{ identifier: 'vue', module: 'vue', isNamed: false }])
}).throws(/Expected to find property named 'plugins'/)
})

test.group('Code Transformer | addAssemblerHook', (group) => {
group.each.setup(async ({ context }) => setupFakeAdonisproject(context.fs))

test('add assembler hook to the assembler file', async ({ assert, fs }) => {
const transformer = new CodeTransformer(fs.baseUrl)

await transformer.updateRcFile((rcFile) =>
rcFile.addAssemblerHook('onBuildCompleted', '@adonisjs/vite/hooks/onBuildCompleted')
)

const file = await fs.contents('adonisrc.ts')
assert.snapshot(file).match()
})

test('dont add duplicate assembler hooks', async ({ assert, fs }) => {
const transformer = new CodeTransformer(fs.baseUrl)

await transformer.updateRcFile((rcFile) => {
rcFile.addAssemblerHook('onBuildCompleted', '@adonisjs/vite/hooks/onBuildCompleted')
rcFile.addAssemblerHook('onBuildCompleted', '@adonisjs/vite/hooks/onBuildCompleted')
})

const file = await fs.contents('adonisrc.ts')
const occurrences = (file.match(/@adonisjs\/vite\/hooks\/onBuildCompleted/g) || []).length

assert.equal(occurrences, 1)
})
})

0 comments on commit 097bdb0

Please sign in to comment.