Skip to content

Commit 57d9286

Browse files
authored
added barrel file generation (#1015)
1 parent d20213f commit 57d9286

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

.changeset/fluffy-turkeys-grow.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'qwik-ui': minor
3+
---
4+
5+
FEAT: added a barrel file to the components root folder
6+
7+
Now when you generate a component qwik-ui will create an `index.ts` file in your components folder which exports the newly generated components.
8+
9+
Example: `qwik-ui add input`
10+
11+
Generated output:
12+
13+
```bash
14+
src/components/index.ts # exports * from './input/input'
15+
src/components/input/input.tsx
16+
```

packages/cli/src/generators/component/component-generator.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ describe('Component generator', () => {
3333

3434
await componentGenerator(tree, options);
3535

36+
expect(tree.exists(`${DEFAULT_COMPONENTS_LOCATION}/index.ts`)).toBeTruthy();
37+
const barrelContent = tree.read(`${DEFAULT_COMPONENTS_LOCATION}/index.ts`, 'utf-8');
38+
expect(barrelContent).toContain(`export * from './button/button';`);
39+
3640
expect(tree.exists(`${DEFAULT_COMPONENTS_LOCATION}/button/button.tsx`)).toBeTruthy();
3741
});
3842

@@ -48,5 +52,9 @@ describe('Component generator', () => {
4852

4953
expect(tree.exists(`${DEFAULT_COMPONENTS_LOCATION}/button/button.tsx`)).toBeTruthy();
5054
expect(tree.exists(`${DEFAULT_COMPONENTS_LOCATION}/input/input.tsx`)).toBeTruthy();
55+
56+
const barrelContent = tree.read(`${DEFAULT_COMPONENTS_LOCATION}/index.ts`, 'utf-8');
57+
expect(barrelContent).toContain(`export * from './button/button';
58+
export * from './input/input';`);
5159
});
5260
});

packages/cli/src/generators/component/component-generator.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@ export async function componentGenerator(tree: Tree, options: ComponentGenerator
4242
components: ComponentConfig[];
4343
}>(componentsJsonPath);
4444

45-
const componentTypes = componentsRegistry.components.map((component) => component.type);
45+
const allComponentTypes = componentsRegistry.components.map(
46+
(component) => component.type,
47+
);
4648
const requestedComponents = options.types.split(',');
4749

50+
// generate barrel file if does not exist
51+
const barrelPath = outputComponentsFolder + '/index.ts';
52+
if (!tree.exists(barrelPath)) {
53+
tree.write(barrelPath, '');
54+
}
55+
56+
let barrelContentToAppend = '';
57+
4858
requestedComponents.forEach((component) => {
49-
const indexOfComponent = componentTypes.indexOf(component.trim());
59+
const indexOfComponent = allComponentTypes.indexOf(component.trim());
5060
if (indexOfComponent === -1) {
5161
throw new Error(`${component} is not a registered component.
5262
Please file an issue if you want it to be implemented.`);
@@ -70,8 +80,19 @@ Please file an issue if you want it to be implemented.`);
7080
outputComponentBasePath,
7181
specificComponentConfig.files,
7282
);
83+
84+
specificComponentConfig.files.map((file) => {
85+
barrelContentToAppend += `export * from './${specificComponentConfig.componentFolder}/${file.split('.')[0]}';\n`;
86+
});
7387
});
7488

89+
// update barrel file
90+
const barrelContent = tree.read(barrelPath, 'utf-8');
91+
if (barrelContent !== '' && barrelContent.slice(-1) !== '\n') {
92+
barrelContentToAppend = '\n' + barrelContentToAppend;
93+
}
94+
tree.write(barrelPath, barrelContent + barrelContentToAppend);
95+
7596
await formatFiles(tree);
7697
}
7798

0 commit comments

Comments
 (0)