Skip to content

Commit f4ea676

Browse files
Clean Code
1 parent c164181 commit f4ea676

File tree

8 files changed

+143
-79
lines changed

8 files changed

+143
-79
lines changed

package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
{
22
"name": "create-reactjs-components",
3-
"displayName": "create-reactjs-components",
4-
"description": "Create Component and Context",
5-
"version": "1.2.1",
3+
"displayName": "Create React Component and Context",
4+
"description": "Create React Component and Context",
5+
"version": "1.2.2",
66
"publisher": "canducci",
77
"icon": "images/logo-three.png",
88
"engines": {
99
"vscode": "^1.48.0"
1010
},
11+
"galleryBanner": {
12+
"theme": "dark",
13+
"color": "#3f79a5"
14+
},
1115
"author": {
1216
"email": "fulviocanducci@hotmail.com",
1317
"name": "Fúlvio Cezar Canducci Dias"
@@ -171,16 +175,19 @@
171175
"test": "node ./out/test/runTest.js"
172176
},
173177
"devDependencies": {
174-
"@types/vscode": "^1.48.0",
175178
"@types/glob": "^7.1.3",
176179
"@types/mocha": "^8.0.0",
177180
"@types/node": "^14.0.27",
178-
"eslint": "^7.6.0",
181+
"@types/vscode": "^1.48.0",
179182
"@typescript-eslint/eslint-plugin": "^3.8.0",
180183
"@typescript-eslint/parser": "^3.8.0",
184+
"eslint": "^7.6.0",
181185
"glob": "^7.1.6",
182186
"mocha": "^8.0.1",
183187
"typescript": "^3.8.3",
184188
"vscode-test": "^1.4.0"
189+
},
190+
"dependencies": {
191+
"pascal-case": "^3.1.1"
185192
}
186193
}

src/commands/js.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import * as vscode from 'vscode';
22
import js from '../templates/js';
3-
import { componentFolder, contextFolder, createCommand } from '../utils/index';
3+
import { componentPath, contextPath, createCommandAsync } from '../utils/index';
44

55
const commands: Array<vscode.Disposable> = [
66
vscode.commands.registerCommand(
77
'canducci.js.component',
8-
async (args: any) => await createCommand(args, js.component, componentFolder, 'Component')
8+
async (args: any) => await createCommandAsync(args, js.component, componentPath, 'Component')
99
),
1010
vscode.commands.registerCommand(
1111
'canducci.js.statelessComponent',
1212
async (args: any) =>
13-
await createCommand(args, js.statelessComponent, componentFolder, 'Component')
13+
await createCommandAsync(args, js.statelessComponent, componentPath, 'Component')
1414
),
1515
vscode.commands.registerCommand(
1616
'canducci.js.context',
17-
async (args: any) => await createCommand(args, js.context, contextFolder, 'Context')
17+
async (args: any) => await createCommandAsync(args, js.context, contextPath, 'Context')
1818
),
1919
vscode.commands.registerCommand(
2020
'canducci.js.statelessContext',
21-
async (args: any) => await createCommand(args, js.statelessContext, contextFolder, 'Context')
21+
async (args: any) => await createCommandAsync(args, js.statelessContext, contextPath, 'Context')
2222
),
2323
];
2424

src/commands/jsx.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import * as vscode from 'vscode';
22
import jsx from '../templates/jsx';
3-
import { componentFolder, contextFolder, createCommand } from '../utils/index';
3+
import { componentPath, contextPath, createCommandAsync } from '../utils/index';
44

55
const commands: Array<vscode.Disposable> = [
66
vscode.commands.registerCommand(
77
'canducci.jsx.component',
8-
async (args: any) => await createCommand(args, jsx.component, componentFolder, 'Component')
8+
async (args: any) => await createCommandAsync(args, jsx.component, componentPath, 'Component')
99
),
1010
vscode.commands.registerCommand(
1111
'canducci.jsx.statelessComponent',
1212
async (args: any) =>
13-
await createCommand(args, jsx.statelessComponent, componentFolder, 'Component')
13+
await createCommandAsync(args, jsx.statelessComponent, componentPath, 'Component')
1414
),
1515
vscode.commands.registerCommand(
1616
'canducci.jsx.context',
17-
async (args: any) => await createCommand(args, jsx.context, contextFolder, 'Context')
17+
async (args: any) => await createCommandAsync(args, jsx.context, contextPath, 'Context')
1818
),
1919
vscode.commands.registerCommand(
2020
'canducci.jsx.statelessContext',
21-
async (args: any) => await createCommand(args, jsx.statelessContext, contextFolder, 'Context')
21+
async (args: any) =>
22+
await createCommandAsync(args, jsx.statelessContext, contextPath, 'Context')
2223
),
2324
];
2425

src/commands/tsx.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import * as vscode from 'vscode';
22
import tsx from '../templates/tsx';
3-
import { componentFolder, contextFolder, createCommand } from '../utils/index';
3+
import { componentPath, contextPath, createCommandAsync } from '../utils/index';
44

55
const commands: Array<vscode.Disposable> = [
66
vscode.commands.registerCommand(
77
'canducci.tsx.component',
8-
async (args: any) => await createCommand(args, tsx.component, componentFolder, 'Component')
8+
async (args: any) => await createCommandAsync(args, tsx.component, componentPath, 'Component')
99
),
1010
vscode.commands.registerCommand(
1111
'canducci.tsx.statelessComponent',
1212
async (args: any) =>
13-
await createCommand(args, tsx.statelessComponent, componentFolder, 'Component')
13+
await createCommandAsync(args, tsx.statelessComponent, componentPath, 'Component')
1414
),
1515
vscode.commands.registerCommand(
1616
'canducci.tsx.context',
17-
async (args: any) => await createCommand(args, tsx.context, contextFolder, 'Context')
17+
async (args: any) => await createCommandAsync(args, tsx.context, contextPath, 'Context')
1818
),
1919
vscode.commands.registerCommand(
2020
'canducci.tsx.statelessContext',
21-
async (args: any) => await createCommand(args, tsx.statelessContext, contextFolder, 'Context')
21+
async (args: any) =>
22+
await createCommandAsync(args, tsx.statelessContext, contextPath, 'Context')
2223
),
2324
];
2425

src/templates/base/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ import React, { createContext } from "react";
4040
4141
const ${name}Context = createContext();
4242
43-
const ${name}ContextProvider = ({ children }) => {
43+
const ${name}Provider = ({ children }) => {
4444
return (
4545
<${name}Context.Provider value={{ }}>
4646
{children}
4747
</${name}Context.Provider>
4848
);
4949
}
5050
51-
export default ${name}ContextProvider;
51+
export default ${name}Provider;
5252
`,
5353
};
5454
}
@@ -61,15 +61,15 @@ import React, { createContext } from "react";
6161
6262
const ${name}Context = createContext();
6363
64-
function ${name}ContextProvider({ children }) {
64+
function ${name}Provider({ children }) {
6565
return (
6666
<${name}Context.Provider value={{ }}>
6767
{children}
6868
</${name}Context.Provider>
6969
);
7070
}
7171
72-
export default ${name}ContextProvider;
72+
export default ${name}Provider;
7373
`,
7474
};
7575
}

src/utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { componentFolder, contextFolder, createCommand } from './operations';
1+
import { componentPath, contextPath, createCommandAsync } from './operations';
22

3-
export { contextFolder, componentFolder, createCommand };
3+
export { contextPath, componentPath, createCommandAsync };

src/utils/operations.ts

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
11
import * as fs from 'fs';
2+
import { pascalCase } from 'pascal-case';
23
import * as path from 'path';
34
import * as vscode from 'vscode';
45

5-
export function mergerDir(...values: Array<string>): string {
6-
return values.join('');
6+
interface IPath {
7+
dir: string;
8+
file: string;
79
}
810

9-
export function getProjetctRoot(): string {
10-
return (vscode.workspace.workspaceFolders as any)[0].uri.fsPath;
11-
}
11+
const joinDir = (...values: Array<string>): string => values.join('');
1212

13-
export function componentFolder(name: string, sep: string): string {
14-
return mergerDir(getProjetctRoot(), sep, 'src', sep, 'components', sep, name);
15-
}
13+
const getPath = (name: string, type: string, extension: string): IPath => {
14+
const fsPath = (vscode.workspace.workspaceFolders as any)[0].uri.fsPath;
15+
const sep = path.sep;
16+
const dir = joinDir(fsPath, sep, 'src', sep, type, sep, name);
17+
const file = joinDir(dir, path.sep, 'index.', extension);
18+
return {
19+
dir,
20+
file,
21+
};
22+
};
1623

17-
export function contextFolder(name: string, sep: string): string {
18-
return mergerDir(getProjetctRoot(), sep, 'src', sep, 'contexts', sep, name);
19-
}
24+
export const componentPath = (name: string, extension: string): IPath =>
25+
getPath(name, 'components', extension);
2026

21-
export function existsDir(dir: string): boolean {
22-
return fs.existsSync(dir);
23-
}
27+
export const contextPath = (name: string, extension: string): IPath =>
28+
getPath(name, 'contexts', extension);
2429

25-
export function createDir(dir: string): boolean {
30+
const existsDir = (dir: string): boolean => fs.existsSync(dir);
31+
32+
const createDir = (dir: string) => {
2633
try {
27-
fs.mkdirSync(dir, { recursive: true });
28-
return true;
34+
return fs.mkdirSync(dir, { recursive: true });
2935
} catch (error) {}
30-
return false;
31-
}
36+
};
3237

33-
export function createFile(dirFile: string, context: string): boolean {
38+
const createFile = (file: string, context: string) => {
3439
try {
35-
fs.writeFileSync(dirFile, context.trimLeft().trimRight());
36-
return true;
40+
fs.writeFileSync(file, context.trimLeft().trimRight());
3741
} catch (error) {}
38-
return false;
39-
}
42+
};
4043

41-
export function openFileCreated(dirFile: string) {
42-
setTimeout(() => {
43-
vscode.workspace.openTextDocument(dirFile).then((editor) => {
44-
if (!editor) {
45-
return;
46-
}
47-
vscode.window.showTextDocument(editor);
48-
});
49-
}, 50);
50-
}
44+
const openFile = (file: string) => {
45+
vscode.workspace.openTextDocument(file).then((editor) => {
46+
if (!editor) {
47+
return;
48+
}
49+
vscode.window.showTextDocument(editor, { preview: false });
50+
});
51+
};
5152

52-
export const optionsFileName = (title: string): vscode.InputBoxOptions => {
53+
const optionsFileName = (title: string): vscode.InputBoxOptions => {
5354
return {
5455
prompt: `Enter the ${title.toLowerCase()} name: `,
5556
placeHolder: `${title} name`,
@@ -58,31 +59,62 @@ export const optionsFileName = (title: string): vscode.InputBoxOptions => {
5859
};
5960
};
6061

61-
export async function createCommand(
62+
const multiNames = (name: string): Array<string> => {
63+
const names: Array<string> = name.split(new RegExp('[,;]')).map((v) => pascalCase(v.trim()));
64+
return names;
65+
};
66+
67+
const nameRules = (name: string | null): boolean => {
68+
return (
69+
name !== null &&
70+
name.length > 0 &&
71+
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(name.charAt(0)) === false
72+
);
73+
};
74+
75+
export const createCommandAsync = async (
6276
args: any,
6377
callBackTemplate: Function,
6478
callBackFolder: Function,
6579
title: string
66-
) {
80+
) => {
6781
const { showInputBox, showErrorMessage, showInformationMessage } = vscode.window;
68-
const name = await showInputBox(optionsFileName(title));
69-
if (!name) {
82+
let names = await showInputBox(optionsFileName(title));
83+
if (!names) {
7084
showErrorMessage('Operation canceled !!!');
7185
} else {
72-
const dir = callBackFolder(name, path.sep);
73-
const { content, extension } = callBackTemplate(name);
74-
const dirFile = mergerDir(dir, path.sep, 'index.', extension);
75-
try {
76-
if (!existsDir(dir)) {
77-
createDir(dir);
78-
createFile(dirFile, content);
79-
openFileCreated(dirFile);
80-
showInformationMessage(`${title} successfully created.`);
86+
let created: Array<string> = [];
87+
let existed: Array<string> = [];
88+
let errors: Array<string> = [];
89+
for (const name of multiNames(names)) {
90+
if (nameRules(name)) {
91+
const { content, extension } = callBackTemplate(name);
92+
const { dir, file } = callBackFolder(name, extension);
93+
try {
94+
if (!existsDir(dir)) {
95+
createDir(dir);
96+
createFile(file, content);
97+
openFile(file);
98+
created.push(name);
99+
} else {
100+
existed.push(name);
101+
}
102+
} catch (error) {
103+
await showErrorMessage(error);
104+
}
81105
} else {
82-
showInformationMessage(`${title} exists`);
106+
errors.push(name.length === 0 ? 'empty' : name);
83107
}
84-
} catch (error) {
85-
showInformationMessage(error);
108+
}
109+
110+
if (created.length > 0) {
111+
showInformationMessage(`${title} ${created.join(', ')} successfully created.`);
112+
}
113+
if (existed.length > 0) {
114+
showInformationMessage(`${title} ${existed.join(', ')} exists.`);
115+
}
116+
if (errors.length > 0) {
117+
showErrorMessage(`${title} ${errors.join(', ')} is invalid.`);
86118
}
87119
}
88-
}
120+
};

0 commit comments

Comments
 (0)