Skip to content

Commit b3a570a

Browse files
committed
feat: share helpers to create importPath with index generator
1 parent 9845619 commit b3a570a

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/index_generator/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class IndexGenerator {
3333
/**
3434
* The application root directory path
3535
*/
36-
#appRoot: string
36+
appRoot: string
3737

3838
/**
3939
* Collection of registered index generator sources
@@ -48,7 +48,7 @@ export class IndexGenerator {
4848
* @param cliLogger - Logger instance for CLI output
4949
*/
5050
constructor(appRoot: string, cliLogger: Logger) {
51-
this.#appRoot = appRoot
51+
this.appRoot = appRoot
5252
this.#cliLogger = cliLogger
5353
}
5454

@@ -60,7 +60,7 @@ export class IndexGenerator {
6060
* @returns This IndexGenerator instance for method chaining
6161
*/
6262
add(name: string, config: IndexGeneratorSourceConfig) {
63-
this.#sources[name] = new IndexGeneratorSource(name, this.#appRoot, this.#cliLogger, config)
63+
this.#sources[name] = new IndexGeneratorSource(name, this.appRoot, this.#cliLogger, config)
6464
return this
6565
}
6666

src/index_generator/source.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ export class IndexGeneratorSource {
8484
if (this.#config.as === 'barrelFile') {
8585
this.#asBarrelFile(this.#vfs, buffer, this.#config.exportName)
8686
} else {
87-
this.#config.as(this.#vfs, buffer, this.#config)
87+
this.#config.as(this.#vfs, buffer, this.#config, {
88+
toImportPath: this.#createBarrelFileImportGenerator(
89+
this.#source,
90+
this.#outputDirname,
91+
this.#config
92+
),
93+
})
8894
}
8995

9096
await mkdir(dirname(this.#output), { recursive: true })
@@ -180,10 +186,10 @@ export class IndexGeneratorSource {
180186
return function (filePath: string) {
181187
if (config.importAlias) {
182188
debug('converting "%s" to import alias, source "%s"', filePath, source)
183-
return `() => import('${removeExtension(filePath.replace(source, config.importAlias))}')`
189+
return removeExtension(filePath.replace(source, config.importAlias))
184190
}
185191
debug('converting "%s" to relative import, source "%s"', filePath, outputDirname)
186-
return `() => import('${relative(outputDirname, filePath)}')`
192+
return relative(outputDirname, filePath)
187193
}
188194
}
189195

@@ -207,7 +213,9 @@ export class IndexGeneratorSource {
207213

208214
const tree = vfs.asTree({
209215
transformKey: keyGenerator,
210-
transformValue: importGenerator,
216+
transformValue: (filePath) => {
217+
return `() => import('${importGenerator(filePath)}')`
218+
},
211219
})
212220

213221
buffer.write(`export const ${exportName} = {`).indent()

src/types/common.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ export type IndexGeneratorSourceConfig = (
3737
as: 'barrelFile'
3838
}
3939
| {
40-
as: (vfs: VirtualFileSystem, buffer: FileBuffer, config: IndexGeneratorSourceConfig) => void
40+
as: (
41+
vfs: VirtualFileSystem,
42+
buffer: FileBuffer,
43+
config: IndexGeneratorSourceConfig,
44+
helpers: {
45+
toImportPath(filePath: string): string
46+
}
47+
) => void
4148
}
4249
) & {
4350
source: string

tests/index_generator.spec.ts

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

1010
import { test } from '@japa/runner'
11+
import { join } from 'node:path/posix'
1112
import { cliui } from '@poppinss/cliui'
1213
import string from '@poppinss/utils/string'
13-
import { join, relative } from 'node:path/posix'
1414
import { IndexGenerator } from '../src/index_generator/main.ts'
1515

1616
const ui = cliui({
@@ -134,10 +134,10 @@ test.group('Index generator', () => {
134134

135135
const transformer = new IndexGenerator(source, ui.logger)
136136
transformer.add('inertiaPages', {
137-
as(vfs, buffer) {
137+
as(vfs, buffer, _, helpers) {
138138
const list = vfs.asList({
139139
transformValue(filePath) {
140-
return `InferPageProps<typeof import('${relative(join(source, outputPath), filePath)}').default>`
140+
return `InferPageProps<typeof import('${helpers.toImportPath(filePath)}').default>`
141141
},
142142
})
143143

@@ -157,10 +157,10 @@ test.group('Index generator', () => {
157157
assert.snapshot(await fs.contents(outputPath)).matchInline(`
158158
"declare module '@adonisjs/inertia' {
159159
interface Pages {
160-
'blog/comments/index': InferPageProps<typeof import('../../../inertia/pages/blog/comments/index.tsx').default>
161-
'blog/comments/show': InferPageProps<typeof import('../../../inertia/pages/blog/comments/show.tsx').default>
162-
'blog/posts/index': InferPageProps<typeof import('../../../inertia/pages/blog/posts/index.tsx').default>
163-
'home': InferPageProps<typeof import('../../../inertia/pages/home.tsx').default>
160+
'blog/comments/index': InferPageProps<typeof import('../../inertia/pages/blog/comments/index.tsx').default>
161+
'blog/comments/show': InferPageProps<typeof import('../../inertia/pages/blog/comments/show.tsx').default>
162+
'blog/posts/index': InferPageProps<typeof import('../../inertia/pages/blog/posts/index.tsx').default>
163+
'home': InferPageProps<typeof import('../../inertia/pages/home.tsx').default>
164164
}
165165
}"
166166
`)

0 commit comments

Comments
 (0)