Skip to content

Commit 13f08bc

Browse files
authored
Merge pull request #4 from univdev/feature/create-react-context-and-provider
Create functions 'Create React Context' and 'Create React Provider'
2 parents 9a309a8 + a2ea62f commit 13f08bc

8 files changed

+105
-4
lines changed

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
{
2525
"command": "vscode-react-developer-toolkit.createReactHOC",
2626
"title": "Create React HOC"
27+
},
28+
{
29+
"command": "vscode-react-developer-toolkit.createReactContext",
30+
"title": "Create React Context"
31+
},
32+
{
33+
"command": "vscode-react-developer-toolkit.createReactProvider",
34+
"title": "Create React Provider"
2735
}
2836
],
2937
"menus": {
@@ -46,6 +54,14 @@
4654
{
4755
"command": "vscode-react-developer-toolkit.createReactHOC",
4856
"group": "1_create"
57+
},
58+
{
59+
"command": "vscode-react-developer-toolkit.createReactProvider",
60+
"group": "1_create"
61+
},
62+
{
63+
"command": "vscode-react-developer-toolkit.createReactContext",
64+
"group": "1_create"
4965
}
5066
]
5167
},

src/commands/CreateReactComponent.command.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export const createReactComponentCommand = (uri: vscode.Uri) => {
1313
placeHolder: 'MyComponent'
1414
}).then(componentName => {
1515
if (componentName) {
16-
vscode.window.showInformationMessage(dirPath, componentName);
17-
1816
const componentDir = path.join(dirPath, componentName);
1917
fs.mkdirSync(componentDir);
2018

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as vscode from 'vscode';
2+
import * as fs from 'fs';
3+
import * as fspath from 'path';
4+
import { createReactContextFactory } from '../factory/CreateReactContext.factory';
5+
6+
export const createReactContextCommand = (uri: vscode.Uri) => {
7+
const path = uri.fsPath;
8+
9+
vscode.window.showInputBox({
10+
prompt: 'Enter React context name',
11+
placeHolder: 'MyContext (A .context prefix is added to the filename.)',
12+
}).then((contextName) => {
13+
if (contextName) {
14+
const contextFileContent = createReactContextFactory(contextName);
15+
16+
fs.writeFileSync(
17+
fspath.join(path, `${contextName}.context.ts`),
18+
contextFileContent,
19+
);
20+
}
21+
});
22+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as vscode from 'vscode';
2+
import * as fs from 'fs';
3+
import * as fspath from 'path';
4+
import { barrelFileFactory } from '../factory/BarrelFile.factory';
5+
import { createReactProviderFactory } from '../factory/CreateReactProvider.factory';
6+
7+
export const createReactProviderCommand = (uri: vscode.Uri) => {
8+
const path = uri.fsPath;
9+
10+
vscode.window.showInputBox({
11+
prompt: 'Enter React provider name',
12+
placeHolder: 'MyProvider (Provider: use same name as context)',
13+
}).then((providerName) => {
14+
if (providerName) {
15+
const fileName = `${providerName}.provider.tsx`;
16+
17+
const contextFileContent = createReactProviderFactory(providerName);
18+
const barrelFile = barrelFileFactory(fileName);
19+
20+
fs.mkdirSync(fspath.join(path, providerName));
21+
fs.writeFileSync(
22+
fspath.join(path, providerName, fileName),
23+
contextFileContent,
24+
);
25+
fs.writeFileSync(
26+
fspath.join(path, providerName, 'index.ts'),
27+
barrelFile,
28+
);
29+
}
30+
});
31+
};

src/factory/BarrelFile.factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export const barrelFileFactory = (componentName: string) => {
2-
return `export * from './${componentName}';`;
1+
export const barrelFileFactory = (fileName: string) => {
2+
return `export * from './${fileName}';`;
33
};

src/factory/Commands.factory.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as vscode from 'vscode';
22
import { createReactComponentCommand } from "../commands/CreateReactComponent.command";
33
import { createReactHookCommand } from '../commands/CreateReactHook.factory';
44
import { createReactHOCCommand } from '../commands/CreateReactHOC.command';
5+
import { createReactProviderCommand } from '../commands/CreateReactProvider.command';
6+
import { createReactContextCommand } from '../commands/CreateReactContext.command';
57

68
export const commandsFactory = (context: vscode.ExtensionContext) => {
79
commands.forEach((command) => {
@@ -22,6 +24,14 @@ const commands = [
2224
'vscode-react-developer-toolkit.createReactHook',
2325
createReactHookCommand,
2426
),
27+
vscode.commands.registerCommand(
28+
'vscode-react-developer-toolkit.createReactProvider',
29+
createReactProviderCommand,
30+
),
31+
vscode.commands.registerCommand(
32+
'vscode-react-developer-toolkit.createReactContext',
33+
createReactContextCommand,
34+
),
2535
];
2636

2737
export default commandsFactory;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const createReactContextFactory = (contextName: string) => `import { createContext } from "react";
2+
3+
export type I${contextName}ContextValue = {
4+
// Enter context props
5+
};
6+
7+
export const ${contextName}Context = createContext<I${contextName}ContextValue | undefined>(undefined);
8+
9+
export default ${contextName}Context;
10+
`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const createReactProviderFactory = (contextName: string) => `import { FC } from "react";
2+
3+
export type ITestProviderProps = {
4+
// Enter provider props
5+
};
6+
7+
export const TestProvider: FC<ITestProviderProps> = () => {
8+
return (
9+
10+
);
11+
};
12+
13+
export default TestProvider;
14+
`;

0 commit comments

Comments
 (0)