Skip to content

Commit 293e100

Browse files
committed
feat: add addAssemblerHook codemod
1 parent e4f8902 commit 293e100

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/code_transformer/rc_file_transformer.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import { fileURLToPath } from 'node:url'
11-
import type { AppEnvironments } from '@adonisjs/application/types'
11+
import type { AppEnvironments, RcFile } from '@adonisjs/application/types'
1212
import {
1313
Node,
1414
Project,
@@ -346,6 +346,30 @@ export class RcFileTransformer {
346346
return this
347347
}
348348

349+
/**
350+
* Add a new assembler hook
351+
*/
352+
addAssemblerHook(type: keyof NonNullable<RcFile['hooks']>, path: string) {
353+
const hooksProperty = this.#getPropertyAssignmentInDefineConfigCall('hooks', '{}')
354+
355+
const hooks = hooksProperty.getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression)
356+
let hookArray = hooks.getProperty(type) as PropertyAssignment
357+
if (!hookArray) {
358+
hooks.addPropertyAssignment({ name: type, initializer: '[]' })
359+
hookArray = hooks.getProperty(type) as PropertyAssignment
360+
}
361+
362+
const hooksArray = hookArray.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression)
363+
const existingHooks = this.#extractModulesFromArray(hooksArray)
364+
if (existingHooks.includes(path)) {
365+
return this
366+
}
367+
368+
hooksArray.addElement(`() => import('${path}')`)
369+
370+
return this
371+
}
372+
349373
/**
350374
* Save the adonisrc.ts file
351375
*/

tests/__snapshots__/code_transformer.spec.ts.cjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,36 @@ export const plugins: Config['plugins'] = [
398398
]
399399
"`
400400

401+
exports[`Code Transformer | addAssemblerHook > add assembler hook to the assembler file 1`] = `"import { defineConfig } from '@adonisjs/core/app'
402+
403+
export default defineConfig({
404+
typescript: true,
405+
preloads: [
406+
() => import('./start/routes.ts'),
407+
{
408+
file: () => import('./start/ace.ts'),
409+
environment: ['console'],
410+
},
411+
],
412+
providers: [
413+
() => import('@adonisjs/core/providers/app_provider'),
414+
{
415+
file: () => import('@adonisjs/core/providers/repl_provider'),
416+
environment: ['repl'],
417+
}
418+
],
419+
metaFiles: [
420+
{
421+
pattern: 'public/**',
422+
reloadServer: true
423+
},
424+
],
425+
commands: [
426+
() => import('@adonisjs/core/commands')
427+
],
428+
hooks: {
429+
onBuildCompleted: [() => import('@adonisjs/vite/hooks/onBuildCompleted')]
430+
}
431+
})
432+
"`
433+

tests/code_transformer.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,3 +894,32 @@ test.group('Code transformer | addVitePlugin', (group) => {
894894
await transformer.addVitePlugin('vue()', [{ identifier: 'vue', module: 'vue', isNamed: false }])
895895
}).throws(/Expected to find property named 'plugins'/)
896896
})
897+
898+
test.group('Code Transformer | addAssemblerHook', (group) => {
899+
group.each.setup(async ({ context }) => setupFakeAdonisproject(context.fs))
900+
901+
test('add assembler hook to the assembler file', async ({ assert, fs }) => {
902+
const transformer = new CodeTransformer(fs.baseUrl)
903+
904+
await transformer.updateRcFile((rcFile) =>
905+
rcFile.addAssemblerHook('onBuildCompleted', '@adonisjs/vite/hooks/onBuildCompleted')
906+
)
907+
908+
const file = await fs.contents('adonisrc.ts')
909+
assert.snapshot(file).match()
910+
})
911+
912+
test('dont add duplicate assembler hooks', async ({ assert, fs }) => {
913+
const transformer = new CodeTransformer(fs.baseUrl)
914+
915+
await transformer.updateRcFile((rcFile) => {
916+
rcFile.addAssemblerHook('onBuildCompleted', '@adonisjs/vite/hooks/onBuildCompleted')
917+
rcFile.addAssemblerHook('onBuildCompleted', '@adonisjs/vite/hooks/onBuildCompleted')
918+
})
919+
920+
const file = await fs.contents('adonisrc.ts')
921+
const occurrences = (file.match(/@adonisjs\/vite\/hooks\/onBuildCompleted/g) || []).length
922+
923+
assert.equal(occurrences, 1)
924+
})
925+
})

0 commit comments

Comments
 (0)