Skip to content

Commit fbaae28

Browse files
committed
feat(core-component): add option to create barrel file for component
1 parent 0fc1e6e commit fbaae28

6 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/core/FileGenerateManager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { config } from '@/config';
22
import { IComponentVariables, IHOCVariables, IHookVariables } from '@/types';
3+
import { BarrelTemplate } from '@/core/temlpates/components/BarrelTemplate';
34
import * as utils from '../utils';
45
import { FileService } from './FileService';
56
import { TemplateGenerator } from './TemplateGenerator';
@@ -51,6 +52,11 @@ export class FileGenerateManager {
5152
fileService.genTest(testTemplate);
5253
}
5354

55+
if (variables.barrel) {
56+
const barrelTemplate = templateGenerator.generateTemplate(BarrelTemplate);
57+
fileService.genBarrel(barrelTemplate);
58+
}
59+
5460
const styleTemplate = config.cssModules ? '.root {}' : '';
5561
fileService.genStyle(styleTemplate);
5662
}

src/core/FileService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export class FileService {
3737
fs.mkdirpSync(this.dirPath);
3838
}
3939

40+
genBarrel(template: string) {
41+
const filePath = `${this.dirPath}/index.${config.ext.component}`;
42+
this.genFile(filePath, template, 'barrel');
43+
}
44+
4045
genJs(template: string) {
4146
const filePath = this.getFilePath(config.ext.component);
4247
this.genFile(filePath, template, 'component');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as t from '@babel/types';
2+
import { TemplateBase } from '@/core/TemplateBase';
3+
import { Template } from '@/core/TemplateGenerator';
4+
import { config } from '@/config';
5+
import * as c from '../shared';
6+
7+
export class BarrelTemplate extends TemplateBase implements Template {
8+
generateAST(): t.File {
9+
const body: t.Statement[] = [];
10+
11+
body.push(
12+
t.exportNamedDeclaration(
13+
null,
14+
[
15+
t.exportSpecifier(
16+
t.identifier(
17+
config.exportType === 'named'
18+
? this.vars.componentName
19+
: 'default'
20+
),
21+
t.identifier(this.vars.componentName)
22+
),
23+
],
24+
t.stringLiteral(`./${this.vars.fileName}`)
25+
)
26+
);
27+
28+
return c.program(body);
29+
}
30+
}

src/questions/questionTypes.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ export function getComponentQuestions() {
1818
mods.push('propTypes');
1919
}
2020

21-
const val = [
21+
const questionList = [
2222
questions.name('Component'),
2323
questions.test(),
24+
questions.barrel(),
2425
questions.hooks(reactHooks),
2526
];
2627

2728
if (mods.length) {
28-
val.push(questions.mods(mods));
29+
questionList.push(questions.mods(mods));
2930
}
3031

31-
return val;
32+
return questionList;
3233
}
3334

3435
export function getHOCQuestions() {

src/questions/questions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export const name = (genType: string, endStr = ':') => ({
2525
validate: (str: string) => str.length > 0,
2626
});
2727

28+
export const barrel = () => ({
29+
type: 'confirm',
30+
name: 'barrel',
31+
message: 'Create a barrel file?',
32+
});
33+
2834
export const test = () => ({
2935
type: 'confirm',
3036
name: 'test',

src/types/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface IComponentVariables extends IBaseVariables {
3535
type: 'component';
3636
name: string;
3737
test: boolean;
38+
barrel: boolean;
3839
hooks: ReactHooks;
3940
mods: string[];
4041
}

0 commit comments

Comments
 (0)