Skip to content

Commit 0e316cf

Browse files
authored
Merge pull request #5 from hvg2416/feature/class-component-template
ADD class based component's templates
2 parents 4b22b3f + 9580a92 commit 0e316cf

7 files changed

Lines changed: 52 additions & 12 deletions

File tree

bin/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const args: any = yargs
2222
description:
2323
"Set this flag to true to scaffold new react component along with it's css style file",
2424
type: "boolean",
25+
})
26+
.option("class", {
27+
description:
28+
"Set this flag to true to scaffold new class based react component",
29+
type: "boolean",
2530
}).argv;
2631

2732
let params: RCTParams = parseParams(args);

src/lib/gc.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { mkdirSync, writeFileSync } from "fs";
22
import { chdir } from "process";
33
import { RCTParams } from "../models/rct_params";
4-
import { getRCTSTemplate } from "../templates/rc-ts-template";
4+
import { getRCCCTemplate } from "../templates/rc-cc-template";
5+
import { getRCFCTemplate } from "../templates/rc-fc-template";
56
import { transformIntoPascalCase } from "../utils/transformers/pascalcase.transformer";
67

78
const generateReactComponent = (params: RCTParams) => {
@@ -18,7 +19,7 @@ const generateReactComponent = (params: RCTParams) => {
1819

1920
if(params.flags.css) writeFileSync(`${componentName}.css`, "", {});
2021

21-
writeFileSync(`${componentName}.${params.flags.js? "js": "tsx"}`, getRCTSTemplate(params), {});
22+
writeFileSync(`${componentName}.${params.flags.js? "js": "tsx"}`, params.flags.class? getRCCCTemplate(params): getRCFCTemplate(params), {});
2223

2324
console.log(`Done`);
2425
} catch (error) {

src/models/rct_params.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface RCTParams {
55
export interface RCTFlags {
66
js: boolean,
77
css: boolean,
8+
class: boolean,
89
}
910
export interface RCTCommand {
1011
name: string,

src/templates/rc-cc-template.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { RCTParams } from "../models/rct_params";
2+
3+
export function getRCCCTemplate(params: RCTParams) {
4+
if (!params.flags.js) {
5+
return `import React from 'react'; ${
6+
params.flags.css ? `\nimport './${params.command.args[0]}.css';` : ""
7+
}\n\ntype ${params.command.args[0]}Props = {\n\n};\n\ntype ${
8+
params.command.args[0]
9+
}State = {\n\n};\n\nclass ${
10+
params.command.args[0]
11+
} extends React.Component<${params.command.args[0]}Props, ${
12+
params.command.args[0]
13+
}State> {\n\n\tstate: ${
14+
params.command.args[0]
15+
}State = {\n\n\t};\n\n\trender() {\n\t\treturn (\n\t\t\t<></>\n\t\t);\n\t}\n}\n\nexport default ${params.command.args[0]};`;
16+
} else {
17+
return `import React from 'react';${
18+
params.flags.css ? `\nimport './${params.command.args[0]}.css';` : ""
19+
}\n\nclass ${
20+
params.command.args[0]
21+
} extends React.Component {\n\trender() {\n\t\treturn <></>;\n\t}\n}\n\nexport default ${params.command.args[0]};`;
22+
}
23+
}

src/templates/rc-fc-template.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { RCTParams } from "../models/rct_params";
2+
3+
export function getRCFCTemplate(params: RCTParams) {
4+
if (params.flags.js) {
5+
return `import React from 'react';${
6+
params.flags.css ? `\nimport './${params.command.args[0]}.css';` : ""
7+
}\n\nexport const ${
8+
params.command.args[0]
9+
} = (props) => {\n\n\treturn (\n\t\t<></>\n\t);\n\n}`;
10+
} else {
11+
return `import React from 'react';${
12+
params.flags.css ? `\nimport './${params.command.args[0]}.css';` : ""
13+
}\n\ntype ${params.command.args[0]}Props = {\n\n}\n\nexport const ${
14+
params.command.args[0]
15+
} = (props: ${
16+
params.command.args[0]
17+
}Props) => {\n\n\treturn (\n\t\t<></>\n\t);\n\n}`;
18+
}
19+
}

src/templates/rc-ts-template.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/utils/parsers/rctParams.parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function parseParams(args: any): RCTParams {
2222
flags: {
2323
js: false,
2424
css: false,
25+
class: false,
2526
...flags,
2627
},
2728
};

0 commit comments

Comments
 (0)