Skip to content

Commit c17c6a9

Browse files
authored
feat: add CodeTransform addNamedImport, addDefaultImport and raw assembler hooks (#87)
1 parent 62563ef commit c17c6a9

4 files changed

Lines changed: 200 additions & 6 deletions

File tree

src/code_transformer/rc_file_transformer.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,18 @@ export default defineConfig({
440440

441441
/**
442442
* Add a new assembler hook
443+
* The format `thunk` write `() => import(path)`.
443444
*
444445
* @param type - The type of hook to add
445-
* @param path - The path to the hook file
446+
* @param value - The path to the hook file or value to write
447+
* @param raw - Wether to write a thunk import or as raw value
446448
* @returns This RcFileTransformer instance for method chaining
447449
*/
448-
addAssemblerHook(type: keyof Exclude<AssemblerRcFile['hooks'], undefined>, path: string) {
450+
addAssemblerHook(
451+
type: keyof Exclude<AssemblerRcFile['hooks'], undefined>,
452+
value: string,
453+
raw: boolean = false
454+
) {
449455
const hooksProperty = this.#getPropertyAssignmentInDefineConfigCall('hooks', '{}')
450456

451457
const hooks = hooksProperty.getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression)
@@ -456,12 +462,53 @@ export default defineConfig({
456462
}
457463

458464
const hooksArray = hookArray.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression)
459-
const existingHooks = this.#extractModulesFromArray(hooksArray)
460-
if (existingHooks.includes(path)) {
461-
return this
465+
466+
if (raw) {
467+
hooksArray.addElement(value)
468+
} else {
469+
const existingHooks = this.#extractModulesFromArray(hooksArray)
470+
if (existingHooks.includes(value)) {
471+
return this
472+
}
473+
474+
hooksArray.addElement(`() => import('${value}')`)
462475
}
463476

464-
hooksArray.addElement(`() => import('${path}')`)
477+
return this
478+
}
479+
480+
/**
481+
* Add a named import
482+
*
483+
* @param specifier - The module specifier to import
484+
* @param names - Names to import from the module
485+
* @returns This RcFileTransformer instance for method chaining
486+
*/
487+
addNamedImport(specifier: string, names: string[]) {
488+
const file = this.#getRcFileOrThrow()
489+
490+
file.addImportDeclaration({
491+
moduleSpecifier: specifier,
492+
namedImports: names,
493+
})
494+
495+
return this
496+
}
497+
498+
/**
499+
* Add a default import
500+
*
501+
* @param specifier - The module specifier to import
502+
* @param name - Name of the default import
503+
* @returns This RcFileTransformer instance for method chaining
504+
*/
505+
addDefaultImport(specifier: string, name: string) {
506+
const file = this.#getRcFileOrThrow()
507+
508+
file.addImportDeclaration({
509+
moduleSpecifier: specifier,
510+
defaultImport: name,
511+
})
465512

466513
return this
467514
}

src/types/code_transformer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ export type EnvValidationNode = {
106106
variables: Record<string, string>
107107
}
108108

109+
export type HookNode =
110+
| {
111+
type: 'thunk'
112+
path: string
113+
}
114+
| {
115+
type: 'import'
116+
path: string
117+
name?: string
118+
}
119+
109120
/**
110121
* The supported package managers for installing packages and managing lockfiles.
111122
* Each package manager has specific lockfiles and install commands.

tests/__snapshots__/code_transformer.spec.ts.cjs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,98 @@ export default defineConfig({
431431
})
432432
"`
433433

434+
exports[`Code Transformer | addAssemblerHook > add raw assembler hook to the assembler file 1`] = `"import { defineConfig } from '@adonisjs/core/app'
435+
436+
export default defineConfig({
437+
typescript: true,
438+
preloads: [
439+
() => import('./start/routes.ts'),
440+
{
441+
file: () => import('./start/ace.ts'),
442+
environment: ['console'],
443+
},
444+
],
445+
providers: [
446+
() => import('@adonisjs/core/providers/app_provider'),
447+
{
448+
file: () => import('@adonisjs/core/providers/repl_provider'),
449+
environment: ['repl'],
450+
}
451+
],
452+
metaFiles: [
453+
{
454+
pattern: 'public/**',
455+
reloadServer: true
456+
},
457+
],
458+
commands: [
459+
() => import('@adonisjs/core/commands')
460+
],
461+
hooks: {
462+
init: [indexPages()]
463+
}
464+
})
465+
"`
466+
467+
exports[`Code Transformer | addDefaultImport > add default import to the assembler file 1`] = `"import { defineConfig } from '@adonisjs/core/app'
468+
import indexPages from '@adonisjs/inertia'
469+
470+
export default defineConfig({
471+
typescript: true,
472+
preloads: [
473+
() => import('./start/routes.ts'),
474+
{
475+
file: () => import('./start/ace.ts'),
476+
environment: ['console'],
477+
},
478+
],
479+
providers: [
480+
() => import('@adonisjs/core/providers/app_provider'),
481+
{
482+
file: () => import('@adonisjs/core/providers/repl_provider'),
483+
environment: ['repl'],
484+
}
485+
],
486+
metaFiles: [
487+
{
488+
pattern: 'public/**',
489+
reloadServer: true
490+
},
491+
],
492+
commands: [
493+
() => import('@adonisjs/core/commands')
494+
]
495+
})
496+
"`
497+
498+
exports[`Code Transformer | addNamedImport > add named import to the assembler file 1`] = `"import { defineConfig } from '@adonisjs/core/app'
499+
import { indexPages } from '@adonisjs/inertia'
500+
501+
export default defineConfig({
502+
typescript: true,
503+
preloads: [
504+
() => import('./start/routes.ts'),
505+
{
506+
file: () => import('./start/ace.ts'),
507+
environment: ['console'],
508+
},
509+
],
510+
providers: [
511+
() => import('@adonisjs/core/providers/app_provider'),
512+
{
513+
file: () => import('@adonisjs/core/providers/repl_provider'),
514+
environment: ['repl'],
515+
}
516+
],
517+
metaFiles: [
518+
{
519+
pattern: 'public/**',
520+
reloadServer: true
521+
},
522+
],
523+
commands: [
524+
() => import('@adonisjs/core/commands')
525+
]
526+
})
527+
"`
528+

tests/code_transformer.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,4 +1284,45 @@ test.group('Code Transformer | addAssemblerHook', (group) => {
12841284

12851285
assert.equal(occurrences, 1)
12861286
})
1287+
1288+
test('add raw assembler hook to the assembler file', async ({ assert, fs }) => {
1289+
const transformer = new CodeTransformer(fs.baseUrl)
1290+
1291+
await transformer.updateRcFile((rcFile) =>
1292+
rcFile.addAssemblerHook('init', 'indexPages()', true)
1293+
)
1294+
1295+
const file = await fs.contents('adonisrc.ts')
1296+
assert.snapshot(file).match()
1297+
})
1298+
})
1299+
1300+
test.group('Code Transformer | addDefaultImport', (group) => {
1301+
group.each.setup(async ({ context }) => setupFakeAdonisproject(context.fs))
1302+
1303+
test('add default import to the assembler file', async ({ assert, fs }) => {
1304+
const transformer = new CodeTransformer(fs.baseUrl)
1305+
1306+
await transformer.updateRcFile((rcFile) =>
1307+
rcFile.addDefaultImport('@adonisjs/inertia', 'indexPages')
1308+
)
1309+
1310+
const file = await fs.contents('adonisrc.ts')
1311+
assert.snapshot(file).match()
1312+
})
1313+
})
1314+
1315+
test.group('Code Transformer | addNamedImport', (group) => {
1316+
group.each.setup(async ({ context }) => setupFakeAdonisproject(context.fs))
1317+
1318+
test('add named import to the assembler file', async ({ assert, fs }) => {
1319+
const transformer = new CodeTransformer(fs.baseUrl)
1320+
1321+
await transformer.updateRcFile((rcFile) =>
1322+
rcFile.addNamedImport('@adonisjs/inertia', ['indexPages'])
1323+
)
1324+
1325+
const file = await fs.contents('adonisrc.ts')
1326+
assert.snapshot(file).match()
1327+
})
12871328
})

0 commit comments

Comments
 (0)