Skip to content

Commit df62f5c

Browse files
author
yuanpengfei
committed
finish check function
1 parent 930914a commit df62f5c

File tree

6 files changed

+134
-75
lines changed

6 files changed

+134
-75
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"out": true // set this to false to include "out" folder in search results
88
},
99
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
10-
"typescript.tsc.autoDetect": "off"
10+
"typescript.tsc.autoDetect": "off",
11+
"editor.quickSuggestions": {
12+
"strings": true
13+
}
1114
}

README.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,36 @@
66
77
### support input name style
88

9-
- DemoComponent -> DemoComponent `recommand`
10-
- demo component -> DemoComponent `recommand`
11-
- demo-component -> DemoComponent `recommand`
12-
- demoComponent -> DemoComponent
13-
- Demo-component -> DemoComponent
14-
- Demo Component -> DemoComponent
15-
- Demo component -> DemoComponent
16-
- Democomponent -> Democomponent `not recommand`
17-
- democomponent -> Democomponent `not recommand`
18-
9+
- DemoComponent -> DemoComponent `recommend`
10+
- demo component -> DemoComponent `recommend`
11+
- demo-component -> DemoComponent `recommend`
12+
- demoComponent -> DemoComponent
13+
- Demo-component -> DemoComponent
14+
- Demo Component -> DemoComponent
15+
- Demo component -> DemoComponent
16+
- Democomponent -> Democomponent `not recommend`
17+
- democomponent -> Democomponent `not recommend`
1918

2019
### code example
2120

2221
```html
23-
<template>
24-
</template>
25-
22+
<template> </template>
23+
2624
<script lang="ts">
27-
28-
import { Vue, Component, Prop } from "vue-property-decorator";
29-
30-
@Component
31-
export default class A extends Vue {}
32-
25+
import { Vue, Component, Prop } from "vue-property-decorator";
26+
27+
@Component
28+
export default class ComponentName extends Vue {}
3329
</script>
34-
35-
<style lang="less" scoped>
36-
37-
</style>
30+
31+
<style lang="less" scoped></style>
3832
```
3933

34+
### check vue filename
35+
36+
> Help you rename you irregularly vue filename, and it's an options choice,
37+
> you can just choose No to check the files details in terminal panel.
38+
4039
### package and publish
4140

4241
[Doc](https://code.visualstudio.com/api/working-with-extensions/publishing-extension)
@@ -46,4 +45,4 @@ export default class A extends Vue {}
4645
vsce package
4746
// publish
4847
vsce publish
49-
```
48+
```

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,41 @@
2323
{
2424
"command": "createvueclasscomponent.create",
2525
"title": "Create Vue Class Component"
26+
},
27+
{
28+
"command": "createvueclasscomponent.check",
29+
"title": "Check Vue Filename"
2630
}
2731
],
2832
"keybindings": [
2933
{
3034
"command": "createvueclasscomponent.create",
3135
"when": "explorerResourceIsFolder || resourceScheme == file"
36+
},
37+
{
38+
"command": "createvueclasscomponent.check",
39+
"when": "explorerResourceIsFolder || resourceScheme == file"
3240
}
3341
],
3442
"menus": {
3543
"editor/context": [
3644
{
3745
"command": "createvueclasscomponent.create",
3846
"group": "navigation"
47+
},
48+
{
49+
"command": "createvueclasscomponent.check",
50+
"group": "navigation"
3951
}
4052
],
4153
"explorer/context": [
4254
{
4355
"command": "createvueclasscomponent.create",
4456
"group": "navigation"
57+
},
58+
{
59+
"command": "createvueclasscomponent.check",
60+
"group": "navigation"
4561
}
4662
]
4763
}

src/checkDestinationVueFilename.ts

Lines changed: 84 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,98 @@ import * as fs from "fs";
33
import convertWords2BigCamelCaseStyle from "./convertWords2BigCamelCaseStyle";
44

55
interface Data {
6-
oldPath: string;
7-
newPath: string;
8-
}
9-
10-
/**
11-
* @param filename
12-
* @returns
13-
*/
14-
function isVueName(filename: string) {
15-
return /\.vue$/.test(filename);
6+
oldPath: string;
7+
tempPath: string;
8+
newPath: string;
169
}
1710

1811
function readFilesOfDest(uri: vscode.Uri) {
19-
const _path = uri.path;
20-
const res: Data[] = [];
21-
function read(dir: string) {
22-
const files = fs.readdirSync(dir);
23-
files.map((v) => {
24-
const p = `${dir}/${v}`;
25-
if (fs.lstatSync(p).isFile()) {
26-
isVueName(v) &&
27-
!/[A-Z]/g.test(v[0]) &&
28-
res.push({
29-
oldPath: p,
30-
newPath: `${dir}/${convertWords2BigCamelCaseStyle(v)}`,
31-
});
32-
} else {
33-
read(p);
34-
}
35-
});
36-
}
37-
38-
read(_path);
39-
40-
return res;
41-
}
12+
const _path = uri.path;
13+
if (fs.lstatSync(_path).isFile()) {
14+
if (_path.endsWith(".vue")) {
15+
const arr = _path.split("/");
16+
const filename = arr.pop();
17+
const dir = arr.join("/");
18+
19+
return [handleFile(filename!, dir)];
20+
}
21+
22+
return [];
23+
}
24+
25+
const res: Data[] = [];
4226

43-
function rename({ oldPath, newPath }: Data) {
44-
fs.renameSync(oldPath, newPath);
27+
function handleFile(v: string, dir: string): Data {
28+
if (/\.vue$/.test(v) && !/[A-Z]/g.test(v[0])) {
29+
return {
30+
oldPath: `${dir}/${v}`,
31+
tempPath: `${dir}/${convertWords2BigCamelCaseStyle(v)}.temp`,
32+
newPath: `${dir}/${convertWords2BigCamelCaseStyle(v)}`,
33+
};
34+
}
35+
36+
return null as any as Data;
37+
}
38+
39+
function read(dir: string) {
40+
const files = fs.readdirSync(dir);
41+
files.map((v) => {
42+
if (fs.lstatSync(`${dir}/${v}`).isFile()) {
43+
const d = handleFile(v, dir);
44+
res.push(d);
45+
} else {
46+
read(`${dir}/${v}`);
47+
}
48+
});
49+
}
50+
51+
read(_path);
52+
53+
return res;
54+
}
4555

46-
fs.existsSync(oldPath) && fs.unlinkSync(oldPath);
56+
function rename({ oldPath, tempPath, newPath }: Data) {
57+
fs.copyFileSync(oldPath, tempPath);
58+
fs.unlinkSync(oldPath);
59+
fs.copyFileSync(tempPath, newPath);
60+
fs.unlinkSync(tempPath);
4761
}
4862

4963
function checkDestinationVueFilename(context: vscode.ExtensionContext) {
50-
context.subscriptions.push(
51-
vscode.commands.registerCommand(
52-
"createvueclasscomponent.create",
53-
async (uri: vscode.Uri) => {
54-
readFilesOfDest(uri).forEach((v) => rename(v));
55-
}
56-
)
57-
);
64+
context.subscriptions.push(
65+
vscode.commands.registerCommand(
66+
"createvueclasscomponent.check",
67+
async (uri: vscode.Uri) => {
68+
const files = readFilesOfDest(uri);
69+
if (!files.length) {
70+
vscode.window.showInformationMessage(
71+
"Congratulations, your vue filenames are all regularly!"
72+
);
73+
return;
74+
}
75+
vscode.window
76+
.showInformationMessage(
77+
`You have some files that are not named irregularly,\n
78+
do you want me to rename them for you?\n
79+
If you choose yes, I'll automatically rename them for you\n
80+
or I'll show you where they are.`,
81+
"Yes",
82+
"No"
83+
)
84+
.then((v) => {
85+
if (v === "Yes") {
86+
files.forEach((v) => rename(v));
87+
} else {
88+
const str = files.map((v) => v.oldPath).join("\n");
89+
const terminal =
90+
vscode.window.createTerminal("File Details");
91+
terminal.sendText(str, true);
92+
terminal.show();
93+
}
94+
});
95+
}
96+
)
97+
);
5898
}
5999

60100
export default checkDestinationVueFilename;

src/extension.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import * as vscode from 'vscode';
2-
// import createVueClassComponent from './createVueClassComponent';
3-
import checkDestinationVueFilename from './checkDestinationVueFilename';
1+
import * as vscode from "vscode";
2+
import createVueClassComponent from "./createVueClassComponent";
3+
import checkDestinationVueFilename from "./checkDestinationVueFilename";
44

55
export function activate(context: vscode.ExtensionContext) {
6+
createVueClassComponent(context);
67
checkDestinationVueFilename(context);
78
}
89

9-
export function deactivate() { }
10+
export function deactivate() {}

0 commit comments

Comments
 (0)