@@ -3,98 +3,101 @@ import * as fs from "fs";
3
3
import convertWords2BigCamelCaseStyle from "./convertWords2BigCamelCaseStyle" ;
4
4
5
5
interface Data {
6
- oldPath : string ;
7
- tempPath : string ;
8
- newPath : string ;
6
+ oldPath : string ;
7
+ tempPath : string ;
8
+ newPath : string ;
9
9
}
10
10
11
11
function readFilesOfDest ( uri : vscode . Uri ) {
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 ( "/" ) ;
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
18
19
- return [ handleFile ( filename ! , dir ) ] ;
20
- }
19
+ return [ handleFile ( filename ! , dir ) ] ;
20
+ }
21
21
22
- return [ ] ;
23
- }
22
+ return [ ] ;
23
+ }
24
24
25
- const res : Data [ ] = [ ] ;
25
+ const res : Data [ ] = [ ] ;
26
26
27
- function handleFile ( v : string , dir : string ) : Data {
28
- if ( / \. v u e $ / . 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
- }
27
+ function handleFile ( v : string , dir : string ) : Data {
28
+ if ( / \. v u e $ / . 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
35
36
- return null as any as Data ;
37
- }
36
+ return null as any as Data ;
37
+ }
38
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
- }
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
+ d && res . push ( d ) ;
45
+ } else {
46
+ read ( `${ dir } /${ v } ` ) ;
47
+ }
48
+ } ) ;
49
+ }
50
50
51
- read ( _path ) ;
51
+ read ( _path ) ;
52
52
53
- return res ;
53
+ return res ;
54
54
}
55
55
56
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 ) ;
57
+ fs . copyFileSync ( oldPath , tempPath ) ;
58
+ fs . unlinkSync ( oldPath ) ;
59
+ fs . copyFileSync ( tempPath , newPath ) ;
60
+ fs . unlinkSync ( tempPath ) ;
61
61
}
62
62
63
63
function checkDestinationVueFilename ( context : vscode . ExtensionContext ) {
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
64
+ const win = vscode . window ;
65
+ context . subscriptions . push (
66
+ vscode . commands . registerCommand (
67
+ "createvueclasscomponent.check" ,
68
+ async ( uri : vscode . Uri ) => {
69
+ const files = readFilesOfDest ( uri ) ;
70
+ if ( ! files . length ) {
71
+ win . showInformationMessage (
72
+ "Congratulations, your vue filenames are all regularly!"
73
+ ) ;
74
+ return ;
75
+ }
76
+ win
77
+ . showInformationMessage (
78
+ `You have some files that are not named irregularly,\n
79
+ do you want me to rename them?\n
79
80
If you choose yes, I'll automatically rename them for you\n
80
81
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
- ) ;
82
+ "Yes" ,
83
+ "No"
84
+ )
85
+ . then ( ( v ) => {
86
+ if ( v === "Yes" ) {
87
+ files . forEach ( ( v ) => rename ( v ) ) ;
88
+ win . showInformationMessage (
89
+ "Already renamed for you, please refresh editor to check."
90
+ ) ;
91
+ } else {
92
+ const str = files . map ( ( v ) => v . oldPath ) . join ( "\n" ) ;
93
+ const terminal = win . createTerminal ( "File Details" ) ;
94
+ terminal . sendText ( str , true ) ;
95
+ terminal . show ( ) ;
96
+ }
97
+ } ) ;
98
+ }
99
+ )
100
+ ) ;
98
101
}
99
102
100
103
export default checkDestinationVueFilename ;
0 commit comments