Skip to content

Commit f09c85b

Browse files
committed
Refactor: Convert command addition to factory pattern
1 parent e1e7735 commit f09c85b

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

eslint.config.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ export default [{
1515
},
1616

1717
rules: {
18+
'curly': ['error', 'multi-line'],
1819
"@typescript-eslint/naming-convention": ["warn", {
1920
selector: "import",
2021
format: ["camelCase", "PascalCase"],
2122
}],
22-
23-
curly: "warn",
2423
eqeqeq: "warn",
2524
"no-throw-literal": "warn",
2625
semi: "warn",

src/commands/CreateReactComponent.command.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@ import * as fs from 'fs';
44
import { reactComponentFactory } from '../factory/ReactComponent.factory';
55
import { barrelFileFactory } from '../factory/BarrelFile.factory';
66

7-
export const createReactComponent = (dirPath: string, componentName: string) => {
8-
vscode.window.showInformationMessage(dirPath, componentName);
7+
export const createReactComponentCommand = (uri: vscode.Uri) => {
8+
if (uri && uri.fsPath) {
9+
const dirPath = uri.fsPath;
910

10-
const componentDir = path.join(dirPath, componentName);
11-
fs.mkdirSync(componentDir);
11+
vscode.window.showInputBox({
12+
prompt: 'Enter React component name',
13+
placeHolder: 'MyComponent'
14+
}).then(componentName => {
15+
if (componentName) {
16+
vscode.window.showInformationMessage(dirPath, componentName);
1217

13-
const componentContent = reactComponentFactory(componentName);
14-
const barrelFileContent = barrelFileFactory(componentName);
15-
16-
fs.writeFileSync(path.join(componentDir, `${componentName}.tsx`), componentContent);
17-
fs.writeFileSync(path.join(componentDir, `index.ts`), barrelFileContent);
18+
const componentDir = path.join(dirPath, componentName);
19+
fs.mkdirSync(componentDir);
20+
21+
const componentContent = reactComponentFactory(componentName);
22+
const barrelFileContent = barrelFileFactory(componentName);
23+
24+
fs.writeFileSync(path.join(componentDir, `${componentName}.tsx`), componentContent);
25+
fs.writeFileSync(path.join(componentDir, `index.ts`), barrelFileContent);
26+
}
27+
});
28+
}
1829
};

src/factory/Commands.factory.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as vscode from 'vscode';
2+
import { createReactComponentCommand } from "../commands/CreateReactComponent.command";
3+
4+
export const commandsFactory = (context: vscode.ExtensionContext) => {
5+
commands.forEach((command) => {
6+
context.subscriptions.push(command);
7+
});
8+
};
9+
10+
const commands = [
11+
vscode.commands.registerCommand(
12+
'vscode-react-developer-toolkit.createReactComponent',
13+
createReactComponentCommand,
14+
),
15+
];
16+
17+
export default commandsFactory;

src/factory/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Commands.factory';

0 commit comments

Comments
 (0)