Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.

Commit 7dc9d75

Browse files
committed
feat: add support for registering nested index.edge file with the parent folder name
1 parent 5975ecf commit 7dc9d75

File tree

10 files changed

+118
-8
lines changed

10 files changed

+118
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ title }}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@modal(title = 'Hello world')
2+
@endmodal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ title }}

fixtures/index-path/index.edge

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@modal(title = 'Hello world')
2+
@endmodal

fixtures/index-path/index.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

fixtures/index-path/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world

src/Supercharged/index.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,39 @@ export class Supercharged {
118118
.map((segment) => string.camelCase(segment)) // Convert each segment to camelCase
119119
.join('.') // Join by .
120120

121+
/**
122+
* Add prefix to name when defined
123+
*/
124+
const prefixedName = options.prefix ? `${options.prefix}.${name}` : name
125+
126+
/**
127+
* Do not normalize the path here. Edge wants unix style paths
128+
*/
129+
const componentPath = `${options.diskName ? `${options.diskName}::` : ''}components/${file}`
130+
121131
/**
122132
* Register the component
123133
*/
124-
this.registerComponent(
125-
options.prefix ? `${options.prefix}.${name}` : name,
126-
127-
/**
128-
* Do not normalize the path here. Edge wants unix style paths
129-
*/
130-
`${options.diskName ? `${options.diskName}::` : ''}components/${file}`
131-
)
134+
this.registerComponent(prefixedName, componentPath)
135+
136+
/**
137+
* Register components with `.index` with the parent name. Doing this can also
138+
* run into race conditions as described below
139+
*
140+
* - There is a file called `components/modal.edge`
141+
* - Then there is a file called `components/modal/index.edge`
142+
* - The file discovered later will win over the file discovered first.
143+
*
144+
* So is this a bad practice?? Not really. Coz someone creating directory structure
145+
* like this themselves opting into the confusion of which component they are
146+
* intending to use.
147+
*
148+
* Infact this race condition will force them to re-think the directory structure. Something
149+
* they should
150+
*/
151+
if (name.endsWith('.index')) {
152+
this.registerComponent(prefixedName.replace(/\.index$/, ''), componentPath)
153+
}
132154
})
133155

134156
return this

test/supercharged.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,82 @@ test.group('SuperCharged', (group) => {
168168
},
169169
})
170170
})
171+
172+
test('register nested .index files with the parent name', async (assert) => {
173+
await fs.add('components/form/input.edge', '')
174+
await fs.add('components/form/label.edge', '')
175+
await fs.add('components/form/button.edge', '')
176+
await fs.add('components/form/index.edge', '')
177+
178+
const charged = new Supercharged()
179+
charged.discoverComponents(fs.basePath)
180+
181+
assert.deepEqual(charged.components, {
182+
'form': {
183+
path: 'components/form/index.edge',
184+
},
185+
'form.button': {
186+
path: 'components/form/button.edge',
187+
},
188+
'form.index': {
189+
path: 'components/form/index.edge',
190+
},
191+
'form.label': {
192+
path: 'components/form/label.edge',
193+
},
194+
'form.input': {
195+
path: 'components/form/input.edge',
196+
},
197+
})
198+
})
199+
200+
test('register index files as index when there is no parent', async (assert) => {
201+
await fs.add('components/form/input.edge', '')
202+
await fs.add('components/form/label.edge', '')
203+
await fs.add('components/form/button.edge', '')
204+
await fs.add('components/index.edge', '')
205+
206+
const charged = new Supercharged()
207+
charged.discoverComponents(fs.basePath)
208+
209+
assert.deepEqual(charged.components, {
210+
'form.button': {
211+
path: 'components/form/button.edge',
212+
},
213+
'form.label': {
214+
path: 'components/form/label.edge',
215+
},
216+
'form.input': {
217+
path: 'components/form/input.edge',
218+
},
219+
'index': {
220+
path: 'components/index.edge',
221+
},
222+
})
223+
})
224+
225+
test('register index files as index when there is no parent with a prefix', async (assert) => {
226+
await fs.add('components/form/input.edge', '')
227+
await fs.add('components/form/label.edge', '')
228+
await fs.add('components/form/button.edge', '')
229+
await fs.add('components/index.edge', '')
230+
231+
const charged = new Supercharged()
232+
charged.discoverComponents(fs.basePath, { prefix: 'hl' })
233+
234+
assert.deepEqual(charged.components, {
235+
'hl.form.button': {
236+
path: 'components/form/button.edge',
237+
},
238+
'hl.form.label': {
239+
path: 'components/form/label.edge',
240+
},
241+
'hl.form.input': {
242+
path: 'components/form/input.edge',
243+
},
244+
'hl.index': {
245+
path: 'components/index.edge',
246+
},
247+
})
248+
})
171249
})

0 commit comments

Comments
 (0)