Skip to content

Commit 4037aab

Browse files
authored
feat: add comment option to IndexGeneratorSource (#89)
1 parent cfa754f commit 4037aab

3 files changed

Lines changed: 68 additions & 8 deletions

File tree

src/index_generator/source.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export class IndexGeneratorSource {
8181
#generateOutput = throttle(async () => {
8282
const buffer = new FileBuffer().eol(true)
8383

84+
if (this.#config.comment) {
85+
buffer.writeLine(this.#textToComment(this.#config.comment))
86+
}
87+
8488
if (this.#config.as === 'barrelFile') {
8589
this.#asBarrelFile(
8690
this.#vfs,
@@ -156,6 +160,13 @@ export class IndexGeneratorSource {
156160
})
157161
}
158162

163+
/**
164+
* Converts a text into a JS comment block. Respecting line-breaks.
165+
*/
166+
#textToComment(text: string) {
167+
return `/**\n * ${text.split('\n').join('\n * ')}\n */`
168+
}
169+
159170
/**
160171
* Transforms the barrel file index key. Converts basename to PascalCase
161172
* and all other paths to camelCase

src/types/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export type IndexGeneratorSourceConfig = (
6161
importAlias?: string
6262
removeSuffix?: string
6363
skipSegments?: string[]
64+
comment?: string
6465
} & VirtualFileSystemOptions
6566

6667
/**

tests/index_generator.spec.ts

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ test.group('Index generator', () => {
3434
output: outputPath,
3535
source: 'app/controllers',
3636
importAlias: '#controllers',
37+
comment: 'This file is automatically generated.\nDO NOT EDIT manually',
3738
})
3839
await transformer.generate()
3940

4041
assert.snapshot(await fs.contents(outputPath)).matchInline(`
41-
"export const controllers = {
42+
"/**
43+
* This file is automatically generated.
44+
* DO NOT EDIT manually
45+
*/
46+
47+
export const controllers = {
4248
auth: {
4349
SignupController: () => import('#controllers/auth/signup_controller'),
4450
},
@@ -69,11 +75,17 @@ test.group('Index generator', () => {
6975
exportName: 'controllers',
7076
output: outputPath,
7177
source: 'app/controllers',
78+
comment: 'This file is automatically generated.\nDO NOT EDIT manually',
7279
})
7380
await transformer.generate()
7481

7582
assert.snapshot(await fs.contents(outputPath)).matchInline(`
76-
"export const controllers = {
83+
"/**
84+
* This file is automatically generated.
85+
* DO NOT EDIT manually
86+
*/
87+
88+
export const controllers = {
7789
auth: {
7890
SignupController: () => import('../../app/controllers/auth/signup_controller.ts'),
7991
},
@@ -106,11 +118,17 @@ test.group('Index generator', () => {
106118
removeSuffix: 'controller',
107119
importAlias: '#controllers',
108120
source: 'app/controllers',
121+
comment: 'This file is automatically generated.\nDO NOT EDIT manually',
109122
})
110123
await transformer.generate()
111124

112125
assert.snapshot(await fs.contents(outputPath)).matchInline(`
113-
"export const controllers = {
126+
"/**
127+
* This file is automatically generated.
128+
* DO NOT EDIT manually
129+
*/
130+
131+
export const controllers = {
114132
auth: {
115133
Signup: () => import('#controllers/auth/signup_controller'),
116134
},
@@ -186,6 +204,7 @@ test.group('Index generator', () => {
186204
output: outputPath,
187205
source: 'app/controllers',
188206
importAlias: '#controllers',
207+
comment: 'This file is automatically generated.\nDO NOT EDIT manually',
189208
})
190209
await transformer.generate()
191210

@@ -204,7 +223,12 @@ test.group('Index generator', () => {
204223
await transformer.addFile(join(source, 'app/controllers/README.md'))
205224

206225
assert.snapshot(await fs.contents(outputPath)).matchInline(`
207-
"export const controllers = {
226+
"/**
227+
* This file is automatically generated.
228+
* DO NOT EDIT manually
229+
*/
230+
231+
export const controllers = {
208232
auth: {
209233
SignupController: () => import('#controllers/auth/signup_controller'),
210234
},
@@ -237,6 +261,7 @@ test.group('Index generator', () => {
237261
output: outputPath,
238262
source: 'app/controllers',
239263
importAlias: '#controllers',
264+
comment: 'This file is automatically generated.\nDO NOT EDIT manually',
240265
})
241266
await transformer.generate()
242267

@@ -251,7 +276,12 @@ test.group('Index generator', () => {
251276
await transformer.removeFile(join(source, 'app/controllers/user/users.ts'))
252277

253278
assert.snapshot(await fs.contents(outputPath)).matchInline(`
254-
"export const controllers = {
279+
"/**
280+
* This file is automatically generated.
281+
* DO NOT EDIT manually
282+
*/
283+
284+
export const controllers = {
255285
auth: {
256286
SignupController: () => import('#controllers/auth/signup_controller'),
257287
},
@@ -281,11 +311,17 @@ test.group('Index generator', () => {
281311
output: outputPath,
282312
source: 'app/controllers',
283313
importAlias: '#controllers',
314+
comment: 'This file is automatically generated.\nDO NOT EDIT manually',
284315
})
285316
await transformer.generate()
286317

287318
assert.snapshot(await fs.contents(outputPath)).matchInline(`
288-
"import AuthSignupController from '#controllers/auth/signup_controller'
319+
"/**
320+
* This file is automatically generated.
321+
* DO NOT EDIT manually
322+
*/
323+
324+
import AuthSignupController from '#controllers/auth/signup_controller'
289325
import PostsController from '#controllers/posts_controller'
290326
import PublicHomePage from '#controllers/public/home_page'
291327
import UserPostsController from '#controllers/user/posts_controller'
@@ -318,11 +354,17 @@ test.group('Index generator', () => {
318354
output: outputPath,
319355
source: 'app/controllers',
320356
importAlias: '#controllers',
357+
comment: 'This file is automatically generated.\nDO NOT EDIT manually',
321358
})
322359
await transformer.generate()
323360

324361
assert.snapshot(await fs.contents(outputPath)).matchInline(`
325-
"export const controllers = {}
362+
"/**
363+
* This file is automatically generated.
364+
* DO NOT EDIT manually
365+
*/
366+
367+
export const controllers = {}
326368
"
327369
`)
328370
})
@@ -345,11 +387,17 @@ test.group('Index generator', () => {
345387
importAlias: '#app',
346388
removeSuffix: 'controller',
347389
skipSegments: ['controllers'],
390+
comment: 'This file is automatically generated.\nDO NOT EDIT manually',
348391
})
349392
await transformer.generate()
350393

351394
assert.snapshot(await fs.contents(outputPath)).matchInline(`
352-
"export const controllers = {
395+
"/**
396+
* This file is automatically generated.
397+
* DO NOT EDIT manually
398+
*/
399+
400+
export const controllers = {
353401
core: {
354402
Health: () => import('#app/core/controllers/health_controller'),
355403
},

0 commit comments

Comments
 (0)