Skip to content

Commit 2ab9bfa

Browse files
committed
✨ Add typescript templates
1 parent 4b86065 commit 2ab9bfa

3 files changed

Lines changed: 105 additions & 12 deletions

File tree

src/model.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import * as vscode from "vscode";
22
import * as fs from "fs";
33
import * as shell from "shelljs";
4-
import * as path from "path";
54

6-
import * as templates from "./template/component";
5+
import * as templatesJavascript from "./template/component";
6+
import * as templatesTypescript from "./template/componentsTypescrip";
77
import config from "./config";
88

99
export default class Model {
10+
getConfiguration(): vscode.WorkspaceConfiguration {
11+
return vscode.workspace.getConfiguration(config.namespace);
12+
}
13+
14+
getTemplates() {
15+
const configuration = this.getConfiguration();
16+
return configuration.typescript ? templatesTypescript : templatesJavascript;
17+
}
18+
1019
createComponent(contextUri: string) {
1120
vscode.window
1221
.showInputBox({
@@ -50,7 +59,7 @@ export default class Model {
5059
this.createFolder(folderPath);
5160
this.createIndexFile(name, folderPath, true);
5261
this.createComponentFile(folderPath, name, true);
53-
this.createFile(folderPath, `${name}.styles.js`, templates.styles);
62+
this.createStyleFile(folderPath, name);
5463

5564
vscode.window.showInformationMessage(
5665
"The component has been successfuly created."
@@ -59,30 +68,46 @@ export default class Model {
5968
}
6069

6170
createComponentFile(path: string, name: string, fela: boolean) {
62-
const fullName = name + ".jsx";
63-
const configuration = vscode.workspace.getConfiguration(config.namespace);
71+
const configuration = this.getConfiguration();
72+
const templates = this.getTemplates();
73+
const fullName = `${name}.${configuration.typescript ? "t" : "j"}sx`;
6474

6575
let content;
66-
if (fela && !configuration.felaHooks) {
76+
if (fela && !configuration.felaHooks) {
6777
content = templates.felaComponent(name, configuration.moduleDependencies);
68-
} else if (fela && configuration.felaHooks) {
69-
content = templates.felaHookComponent(name, configuration.moduleDependencies);
78+
} else if (fela && configuration.felaHooks) {
79+
content = templates.felaHookComponent(
80+
name,
81+
configuration.moduleDependencies
82+
);
7083
} else {
7184
content = templates.component(name, configuration.moduleDependencies);
7285
}
7386

7487
this.createFile(path, fullName, content);
7588
}
7689

90+
createStyleFile(path: string, name: string) {
91+
const configuration = this.getConfiguration();
92+
const templates = this.getTemplates();
93+
const fullName = `${name}.styles.${configuration.typescript ? "t" : "j"}s`;
94+
95+
this.createFile(
96+
path,
97+
fullName,
98+
templates.styles(configuration.moduleDependencies)
99+
);
100+
}
101+
77102
createIndexFile(componentName: string, path: string, fela: boolean) {
78-
const name = "index.js";
79103
const configuration = vscode.workspace.getConfiguration(config.namespace);
104+
const name = `index.${configuration.typescript ? "t" : "j"}s`;
80105

81106
let content;
82107
if (!fela || (fela && configuration.felaHooks)) {
83-
content = templates.index(componentName);
108+
content = templatesJavascript.index(componentName);
84109
} else {
85-
content = templates.indexFela(componentName);
110+
content = templatesJavascript.indexFela(componentName);
86111
}
87112

88113
this.createFile(path, name, content);

src/template/component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ ${name}.propTypes = {
5757
export default ${name};
5858
`;
5959

60-
export const styles = "export const container = () => ({});\n";
60+
export const styles = (dependencies: boolean) =>
61+
"export const container = () => ({});\n";
6162

6263
export const component = (name: string, dependencies: boolean) => `${
6364
dependencies
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
export const felaComponent = (name: string, dependencies: boolean) => `${
2+
dependencies
3+
? "import { React,  FunctionComponent } from '../../dependencies';"
4+
: "import React, { FunctionComponent } from 'react';"
5+
}
6+
7+
type ${name}Props = {
8+
styles: object
9+
}
10+
11+
const ${name}: FunctionComponent<${name}Props> = ({ styles }) => (
12+
<div className={styles.container}>
13+
14+
</div>
15+
);
16+
17+
export default ${name};
18+
`;
19+
20+
export const felaHookComponent = (name: string, dependencies: boolean) => `${
21+
dependencies
22+
? "import { React,  FunctionComponent, useFelaEnhanced } from '../../dependencies';"
23+
: "import React, { FunctionComponent } from 'react';\nimport { useFelaEnhanced } from 'hooks';"
24+
}
25+
26+
import * as rules from './${name}.styles';
27+
28+
type ${name}Props = {}
29+
30+
const ${name}: FunctionComponent<${name}Props> = () => {
31+
const { styles } = useFelaEnhanced(rules);
32+
33+
return (
34+
<div className={styles.container}>
35+
36+
</div>
37+
);
38+
};
39+
40+
export default ${name};
41+
`;
42+
43+
export const styles = (dependencies: boolean) => `${
44+
dependencies
45+
? "import { TRule } from '../../dependencies';\n"
46+
: "import type { TRule } from 'fela';"
47+
}
48+
49+
export const container: TRule = () => ({});
50+
`;
51+
52+
export const component = (name: string, dependencies: boolean) => `${
53+
dependencies
54+
? "import { React, FunctionComponent } from '../../dependencies';"
55+
: "import React, { FunctionComponent } from 'react';"
56+
}
57+
58+
type ${name}Props = {}
59+
60+
const ${name}: FunctionComponent<${name}Props> = () => (
61+
<>
62+
63+
</>
64+
);
65+
66+
export default ${name};
67+
`;

0 commit comments

Comments
 (0)