Skip to content

Commit fcfbf6b

Browse files
苑朋飞苑朋飞
苑朋飞
authored and
苑朋飞
committed
modify readme and optimize code when create same name component to warn user
1 parent 49fd873 commit fcfbf6b

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,23 @@
44
> The reason I make it is to save my time in work, because my new team is still using Vue2 and write class style component, so I have to adapt to it.(I am a react user and favor into hooks and functional style.)
55
> I hope this little tool will help you too.
66
7+
8+
### code example
9+
10+
```html
11+
<template>
12+
</template>
13+
14+
<script lang="ts">
15+
16+
import { Vue, Component, Prop } from "vue-property-decorator";
17+
18+
@Component
19+
export default class A extends Vue {}
20+
21+
</script>
22+
23+
<style lang="less" scoped>
24+
25+
</style>
26+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "CreateVueClassComponent",
44
"description": "create vue class component",
55
"icon": "images/vue_logo.png",
6-
"version": "0.0.2",
6+
"version": "0.0.3",
77
"publisher": "pengfeiyuan",
88
"engines": {
99
"vscode": "^1.57.0"

src/createVueClassComponent.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ function getTemplate(componentName: string) {
1818
return `<template>\n</template>
1919
\n<script lang="ts">
2020
\nimport { Vue, Component, Prop } from "vue-property-decorator";
21-
\n@Component
22-
\nexport default class ${componentName} extends Vue {}
21+
\n@Component\nexport default class ${componentName} extends Vue {}
2322
\n</script>
2423
\n<style lang="less" scoped>
2524
\n</style>`;
@@ -31,31 +30,33 @@ function writeFile(content: Buffer | string, filePath: string, componentName: st
3130
fs.writeFileSync(_filePath, content);
3231
}
3332

34-
function createComponent(uri: { path: string }, componentName: string) {
35-
const content = getTemplate(componentName);
33+
async function createComponent(uri: vscode.Uri, componentName: string) {
34+
const path = uri.path;
35+
const arr = path.split('/');
3636

37-
38-
const arr = uri.path.split('/');
39-
40-
if (fs.lstatSync(uri.path).isFile()) {
37+
if (fs.lstatSync(path).isFile()) {
4138
arr.pop();
4239
}
4340

4441
const filePath = arr.join('/');
42+
43+
if (fs.existsSync(`${filePath}/${componentName}.vue`)) {
44+
vscode.window.showWarningMessage(`You already have one ${componentName} component.`);
45+
return;
46+
}
4547

4648

47-
writeFile(content, filePath, componentName);
49+
writeFile(getTemplate(componentName), filePath, componentName);
4850
}
4951

5052
module.exports = function (context: { subscriptions: any[]; }) {
51-
context.subscriptions.push(vscode.commands.registerCommand('createvueclasscomponent.create', async (uri: { path: string }) => {
53+
context.subscriptions.push(vscode.commands.registerCommand('createvueclasscomponent.create', async (uri: vscode.Uri) => {
5254
const componentName = await vscode.window.showInputBox({
5355
ignoreFocusOut: true,
5456
placeHolder: 'MyComponent',
5557
prompt: 'What is the name of the new component?',
5658
});
57-
59+
5860
createComponent(uri, convertComponentName(componentName!));
5961
}));
60-
6162
};

0 commit comments

Comments
 (0)