-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentFileContent.js
83 lines (80 loc) · 2.17 KB
/
ComponentFileContent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @class ComponentFileContent
* @description ComponentFileContent is a class that represents the content of a file.
*/
class ComponentFileContent {
// private properties
#content = "";
#hasStyles = false;
#fileExtension = "js";
#hasProps = false;
#hasImportReact = false;
#componentName = "";
constructor(
componentName,
hasStyles = this.#hasStyles,
fileExtension = this.#fileExtension,
hasProps = this.#hasProps,
hasImportReact = this.#hasImportReact
) {
if (!componentName) {
throw new Error("Component name is required");
}
this.#hasStyles = hasStyles;
this.#fileExtension = fileExtension;
this.#hasProps = hasProps;
this.#hasImportReact = hasImportReact;
this.#componentName = componentName;
}
get componentName() {
return this.#componentName;
}
get hasStyles() {
return this.#hasStyles;
}
get fileExtension() {
return this.#fileExtension;
}
get hasProps() {
return this.#hasProps;
}
get hasImportReact() {
return this.#hasImportReact;
}
get content() {
return this.#content;
}
// generate the part of "import React from 'react'";
#generateImportReact() {
return this.#hasImportReact ? `import React from 'react';\n` : "";
}
// generate the part of props, considering if it is a ts file and if it is with props
#generateProps() {
if (this.fileExtension === "ts" && this.#hasProps) {
return `interface Props {}\n`;
}
return "";
}
// generate the part of the component content, considering the style of the file is ts and if it is with props
generateComponentContent() {
const importReactContent = this.#generateImportReact();
const propsObjectContent = this.#generateProps();
let propsParamContent = "";
if (this.#hasProps) {
propsParamContent = this.fileExtension === "ts" ? "props: Props" : "props";
}
this.#content = `${importReactContent}
${propsObjectContent}
function ${this.componentName}(${propsParamContent}) {
return (
<>
{/* Add your component content here */}
</>
);
};
export default ${this.componentName};
`;
return this.#content;
}
}
module.exports = ComponentFileContent;