Skip to content

Commit 368f663

Browse files
add typescript template
1 parent 326d37d commit 368f663

File tree

4 files changed

+213
-11
lines changed

4 files changed

+213
-11
lines changed

package.json

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
"onCommand:canducci.createContextArrowFunctionJs",
3434
"onCommand:canducci.createContextFunctionJs",
3535
"onCommand:canducci.createContextArrowFunctionJsx",
36-
"onCommand:canducci.createContextFunctionJsx"
36+
"onCommand:canducci.createContextFunctionJsx",
37+
"onCommand:canducci.arrowFunctionComponentTs",
38+
"onCommand:canducci.functionComponentTs",
39+
"onCommand:canducci.functionContextTs",
40+
"onCommand:canducci.arrowFunctionContextTs"
3741
],
3842
"main": "./out/extension.js",
3943
"contributes": {
@@ -69,6 +73,22 @@
6973
{
7074
"command": "canducci.createContextFunctionJsx",
7175
"title": "React Function Context (jsx)"
76+
},
77+
{
78+
"command": "canducci.arrowFunctionComponentTs",
79+
"title": "React Arrow Function Component (tsx)"
80+
},
81+
{
82+
"command": "canducci.functionComponentTs",
83+
"title": "React Function Component (tsx)"
84+
},
85+
{
86+
"command": "canducci.arrowFunctionContextTs",
87+
"title": "React Arrow Function Context (tsx)"
88+
},
89+
{
90+
"command": "canducci.functionContextTs",
91+
"title": "React Function Context (tsx)"
7292
}
7393
],
7494
"menus": {
@@ -80,7 +100,7 @@
80100
},
81101
{
82102
"command": "canducci.functionComponentJs",
83-
"group": "1_modification",
103+
"group": "2_modification",
84104
"when": "explorerResourceIsFolder"
85105
},
86106
{
@@ -90,12 +110,12 @@
90110
},
91111
{
92112
"command": "canducci.functionComponentJsx",
93-
"group": "1_modification",
113+
"group": "2_modification",
94114
"when": "explorerResourceIsFolder"
95115
},
96116
{
97117
"command": "canducci.createContextArrowFunctionJs",
98-
"group": "2_modification",
118+
"group": "1_modification",
99119
"when": "explorerResourceIsFolder"
100120
},
101121
{
@@ -105,13 +125,33 @@
105125
},
106126
{
107127
"command": "canducci.createContextArrowFunctionJsx",
108-
"group": "2_modification",
128+
"group": "1_modification",
109129
"when": "explorerResourceIsFolder"
110130
},
111131
{
112132
"command": "canducci.createContextFunctionJsx",
113133
"group": "2_modification",
114134
"when": "explorerResourceIsFolder"
135+
},
136+
{
137+
"command": "canducci.arrowFunctionComponentTs",
138+
"group": "1_modification",
139+
"when": "explorerResourceIsFolder"
140+
},
141+
{
142+
"command": "canducci.functionComponentTs",
143+
"group": "2_modification",
144+
"when": "explorerResourceIsFolder"
145+
},
146+
{
147+
"command": "canducci.functionContextTs",
148+
"group": "2_modification",
149+
"when": "explorerResourceIsFolder"
150+
},
151+
{
152+
"command": "canducci.arrowFunctionContextTs",
153+
"group": "1_modification",
154+
"when": "explorerResourceIsFolder"
115155
}
116156
]
117157
}

src/extension.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import {
1111
createContextFunctionJsx,
1212
functionComponentJsx,
1313
} from './templates/jsx';
14+
import {
15+
arrowFunctionComponentTs,
16+
arrowFunctionContextTs,
17+
functionComponentTs,
18+
functionContextTs,
19+
} from './templates/tsx';
1420
import createComponent from './utils/index';
1521
import {
1622
getProjectComponentFolder,
@@ -94,6 +100,39 @@ export function activate(context: vscode.ExtensionContext) {
94100
getProjectContextFolder
95101
)
96102
),
103+
/* React Typescript*/
104+
registerCommand(
105+
'canducci.arrowFunctionComponentTs',
106+
async (args: any) =>
107+
await createComponent(
108+
args,
109+
arrowFunctionComponentTs,
110+
getProjectComponentFolder
111+
)
112+
),
113+
registerCommand(
114+
'canducci.functionComponentTs',
115+
async (args: any) =>
116+
await createComponent(
117+
args,
118+
functionComponentTs,
119+
getProjectComponentFolder
120+
)
121+
),
122+
registerCommand(
123+
'canducci.functionContextTs',
124+
async (args: any) =>
125+
await createComponent(args, functionContextTs, getProjectContextFolder)
126+
),
127+
registerCommand(
128+
'canducci.arrowFunctionContextTs',
129+
async (args: any) =>
130+
await createComponent(
131+
args,
132+
arrowFunctionContextTs,
133+
getProjectContextFolder
134+
)
135+
),
97136
];
98137

99138
context.subscriptions.push(...disposables);

src/templates/base/index.ts

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IComponentReturn } from './types';
22

3+
/** Base - Component */
34
export function functionComponentBase(
45
name: string,
56
extension: string
@@ -11,9 +12,7 @@ export function functionComponentBase(
1112
import React from 'react';
1213
1314
function ${name}() {
14-
return (
15-
<div>${name}</div>
16-
)
15+
return <div>${name}</div>
1716
}
1817
1918
export default ${name};
@@ -32,16 +31,15 @@ export function arrowFunctionComponentBase(
3231
import React from 'react';
3332
3433
const ${name} = () => {
35-
return (
36-
<div>${name}</div>
37-
)
34+
return <div>${name}</div>
3835
}
3936
4037
export default ${name};
4138
`,
4239
};
4340
}
4441

42+
/** Base - ContextApi */
4543
export function createContextArrowFunctionBase(
4644
name: string,
4745
extension: string
@@ -91,3 +89,104 @@ export default ${name}ContextProvider;
9189
`,
9290
};
9391
}
92+
93+
/** base - typescript */
94+
export function functionComponentTypescriptBase(
95+
name: string,
96+
extension: string
97+
): IComponentReturn {
98+
return {
99+
title: 'Component',
100+
extension: extension,
101+
content: `
102+
import React from 'react';
103+
104+
interface ${name}Props { }
105+
106+
function ${name}({ }: ${name}Props) {
107+
return <div>${name}</div>;
108+
}
109+
110+
export default ${name};
111+
`,
112+
};
113+
}
114+
115+
export function arrowFunctionComponentTypescriptBase(
116+
name: string,
117+
extension: string
118+
): IComponentReturn {
119+
return {
120+
title: 'Component',
121+
extension: extension,
122+
content: `
123+
import React, { FC } from 'react';
124+
125+
interface ${name}Props {}
126+
127+
const ${name}: FC<${name}Props> = ({ }:${name}Props) => {
128+
return <div>${name}</div>;
129+
};
130+
131+
export default ${name};
132+
`,
133+
};
134+
}
135+
export function functionContextTypescriptBase(
136+
name: string,
137+
extension: string
138+
): IComponentReturn {
139+
return {
140+
title: 'Context',
141+
extension: extension,
142+
content: `
143+
import React, { createContext, ReactNode } from 'react';
144+
145+
interface ${name}ProviderProps {
146+
children: ReactNode;
147+
}
148+
149+
interface ${name}Props { }
150+
151+
export const ${name}Context = createContext<${name}Props>({} as ${name}Props);
152+
153+
function ${name}Provider({ children }: ${name}ProviderProps) {
154+
return (
155+
<${name}Context.Provider value={{ }}>
156+
{children}
157+
</${name}Context.Provider>
158+
);
159+
}
160+
161+
export default ${name}Provider;`,
162+
};
163+
}
164+
165+
export function arrowFunctionContextTypescriptBase(
166+
name: string,
167+
extension: string
168+
): IComponentReturn {
169+
return {
170+
title: 'Context',
171+
extension: extension,
172+
content: `
173+
import React, { createContext, FC, ReactNode } from 'react';
174+
175+
interface ${name}ContextProps { }
176+
177+
export const ${name}Context = createContext<${name}ContextProps>(
178+
{} as ${name}ContextProps
179+
);
180+
181+
const ${name}Provider: FC<{ children: ReactNode }> = ({ children }) => {
182+
return (
183+
<${name}Context.Provider value={{ }}>
184+
{children}
185+
</${name}Context.Provider>
186+
);
187+
};
188+
189+
export default ${name}Provider;
190+
`,
191+
};
192+
}

src/templates/tsx.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
arrowFunctionComponentTypescriptBase,
3+
arrowFunctionContextTypescriptBase,
4+
functionComponentTypescriptBase,
5+
functionContextTypescriptBase,
6+
} from './base/index';
7+
8+
const arrowFunctionComponentTs = (name: string) =>
9+
arrowFunctionComponentTypescriptBase(name, 'tsx');
10+
11+
const functionComponentTs = (name: string) =>
12+
functionComponentTypescriptBase(name, 'tsx');
13+
14+
const functionContextTs = (name: string) =>
15+
functionContextTypescriptBase(name, 'tsx');
16+
const arrowFunctionContextTs = (name: string) =>
17+
arrowFunctionContextTypescriptBase(name, 'tsx');
18+
19+
export {
20+
arrowFunctionComponentTs,
21+
functionComponentTs,
22+
functionContextTs,
23+
arrowFunctionContextTs,
24+
};

0 commit comments

Comments
 (0)