@@ -2,41 +2,45 @@ import * as vscode from 'vscode';
22import { StatusBarItems } from './interface' ;
33import { join , resolve } from 'path' ;
44import { platform } from 'os' ;
5- import { existsSync , mkdirSync , readdirSync , writeFile , statSync , Stats } from 'fs' ;
5+ import { existsSync , mkdirSync , readdirSync , statSync , Stats } from 'fs' ;
66import { gitignore , maincpp , makefile , readme } from './templates' ;
7+ import { promises as fsPromises } from 'fs' ;
78
89export class CodeManage {
9-
1010 private _terminal : vscode . Terminal ;
1111 private _folderPath : string ;
1212 private _items : StatusBarItems ;
1313 private _watcher : vscode . FileSystemWatcher | null ;
1414
15- constructor ( ) {
16-
15+ constructor ( ) {
1716 this . _terminal = vscode . window . createTerminal ( {
1817 name : 'Code Make' ,
19- hideFromUser : true
18+ hideFromUser : true ,
2019 } ) ;
21- this . _folderPath = vscode . workspace ?. workspaceFolders ?. [ 0 ] ?. uri ?. fsPath as string ;
20+ this . _folderPath = this . getFolderPath ( ) ;
2221 this . _items = {
2322 create : vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Right ) ,
24- start : vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Right ) ,
23+ start : vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Right ) ,
2524 } ;
2625 this . _watcher = null ;
26+
2727 this . setupFileSystemWatcher ( ) ;
2828 this . updateStatusBar ( ) ;
29+ }
2930
31+ private getFolderPath ( ) : string {
32+ if ( ! vscode . workspace || ! vscode . workspace . workspaceFolders ) {
33+ return '' ;
34+ }
35+ return vscode . workspace . workspaceFolders [ 0 ] . uri . fsPath ;
3036 }
3137
32- public start ( ) {
38+ public start ( ) {
39+ if ( ! this . _folderPath ) return ;
3340
3441 const binPath : string = join ( resolve ( this . _folderPath ) , 'bin' ) ;
35-
3642 try {
37-
3843 const binStats : Stats = statSync ( binPath ) ;
39-
4044 if ( ! binStats . isDirectory ( ) ) {
4145 this . build ( ) ;
4246 } else {
@@ -48,140 +52,118 @@ export class CodeManage {
4852 this . terminal ( 'make run' ) ;
4953 }
5054 }
51-
5255 } catch ( e ) {
5356 this . build ( ) ;
5457 }
58+ }
5559
60+ private updateStatusBar ( ) {
61+ const isProjectValid =
62+ this . _folderPath &&
63+ existsSync ( join ( this . _folderPath , 'src' ) ) &&
64+ existsSync ( join ( this . _folderPath , 'src' , 'main.cpp' ) ) &&
65+ existsSync ( join ( this . _folderPath , 'include' ) ) &&
66+ existsSync ( join ( this . _folderPath , 'Makefile' ) ) ;
67+
68+ this . _items . create . text = `$(heart)` ;
69+ this . _items . create . tooltip = 'Create C++ Project' ;
70+ this . _items . create . command = 'code-make-create.run' ;
71+ this . _items . create . color = '#FF79C6' ;
72+
73+ this . _items . start . text = `$(debug-start)` ;
74+ this . _items . start . tooltip = 'Start C++ Project' ;
75+ this . _items . start . command = 'code-make-start.run' ;
76+ this . _items . start . color = '#89D185' ;
77+
78+ ! isProjectValid
79+ ? ( this . _items . create . show ( ) , this . _items . start . hide ( ) )
80+ : ( this . _items . start . show ( ) , this . _items . create . hide ( ) ) ;
5681 }
5782
58- public create ( ) {
59-
83+ public create ( ) {
6084 try {
85+ if ( ! this . _folderPath ) {
86+ return vscode . window . showErrorMessage ( 'Please open a project folder first' ) ;
87+ }
6188
62- if ( ! this . _folderPath ) return vscode . window . showErrorMessage ( 'Please open a project folder first' ) ;
63-
64- const folders : string [ ] = [
65- 'assets' ,
66- 'doc' ,
67- 'include' ,
68- 'include/core' ,
69- 'include/ui' ,
70- 'include/utils' ,
71- 'lib' ,
72- 'src' ,
73- 'src/core' ,
74- 'src/ui' ,
75- 'src/utils' ,
76- 'tests'
77- ] ;
78-
79- folders . forEach ( ( folder : string ) => this . createFolder ( this . _folderPath , folder ) ) ;
80-
81- this . createFile ( join ( this . _folderPath , 'src' , 'main.cpp' ) , maincpp ) ;
82- this . createFile ( join ( this . _folderPath , '.gitignore' ) , gitignore ) ;
83- this . createFile ( join ( this . _folderPath , 'Makefile' ) , makefile ) ;
84- this . createFile ( join ( this . _folderPath , 'README.md' ) , readme ) ;
85-
89+ const folders : string [ ] = [
90+ 'assets' ,
91+ 'doc' ,
92+ 'include' ,
93+ 'include/core' ,
94+ 'include/ui' ,
95+ 'include/utils' ,
96+ 'lib' ,
97+ 'src' ,
98+ 'src/core' ,
99+ 'src/ui' ,
100+ 'src/utils' ,
101+ 'tests' ,
102+ ] ;
103+
104+ folders . forEach ( ( folder : string ) => this . createFolder ( this . _folderPath , folder ) ) ;
105+
106+ this . createFile ( join ( this . _folderPath , 'src' , 'main.cpp' ) , maincpp ) ;
107+ this . createFile ( join ( this . _folderPath , '.gitignore' ) , gitignore ) ;
108+ this . createFile ( join ( this . _folderPath , 'Makefile' ) , makefile ) ;
109+ this . createFile ( join ( this . _folderPath , 'README.md' ) , readme ) ;
86110 } catch ( e ) {
87111 console . log ( 'An error occurred.' , e ) ;
88112 }
89-
90113 }
91114
92- private build ( ) {
93-
115+ private build ( ) {
94116 try {
95-
96117 const srcFolder : string = join ( this . _folderPath , 'src' ) ;
97118 const mainFile : string = join ( this . _folderPath , 'src' , 'main.cpp' ) ;
98119
99- if ( ! existsSync ( srcFolder ) && ! existsSync ( mainFile ) ) return vscode . window . showErrorMessage ( 'No such file main.cpp or directory src' ) ;
100-
120+ if ( ! existsSync ( srcFolder ) && ! existsSync ( mainFile ) ) {
121+ return vscode . window . showErrorMessage ( 'No such file main.cpp or directory src' ) ;
122+ }
123+
101124 this . terminal ( 'make' ) ;
102-
103125 } catch ( e ) {
104126 console . log ( 'An error occurred.' , e ) ;
105127 }
106-
107128 }
108129
109- private updateStatusBar ( ) {
110-
111- try {
112-
113- const srcPath : string = join ( this . _folderPath , 'src' ) ;
114- const maincpp : string = join ( this . _folderPath , 'src' , 'main.cpp' ) ;
115- const includePath : string = join ( this . _folderPath , 'include' ) ;
116- const makefilePath : string = join ( this . _folderPath , 'Makefile' ) ;
117-
118- const isProjectValid : boolean = existsSync ( srcPath ) && existsSync ( maincpp ) && existsSync ( includePath ) && existsSync ( makefilePath ) ;
119-
120- this . _items . create . text = `$(heart)` ;
121- this . _items . create . tooltip = 'Code Make (Create Project)' ;
122- this . _items . create . command = 'code-make-create.run' ;
123- this . _items . create . color = '#FF79C6' ;
124-
125- this . _items . start . text = `$(debug-start)` ;
126- this . _items . start . tooltip = 'Code Make (Start Project)' ;
127- this . _items . start . command = 'code-make-start.run' ;
128- this . _items . start . color = '#89D185' ;
129-
130- ! isProjectValid
131- ? ( this . _items . create . show ( ) , this . _items . start . hide ( ) )
132- : ( this . _items . start . show ( ) , this . _items . create . hide ( ) ) ;
133-
134- } catch ( e : any ) {
135- vscode . window . showErrorMessage ( e . message || e . toString ( ) ) ;
136- }
137-
138- }
139-
140130 private terminal ( command : string ) {
141-
142131 try {
132+ if ( this . _terminal . exitStatus ) {
133+ this . _terminal = vscode . window . createTerminal ( {
134+ name : 'Code Make' ,
135+ hideFromUser : true ,
136+ } ) ;
137+ }
143138
144- if ( this . _terminal . exitStatus ) this . _terminal = vscode . window . createTerminal ( {
145- name : 'Code Make' ,
146- hideFromUser : true
147- } ) ;
148-
149139 this . _terminal . show ( ) ;
150-
151140 platform ( ) === 'win32' ? this . _terminal . sendText ( 'cls' ) : this . _terminal . sendText ( 'clear' ) ;
152-
153141 this . _terminal . sendText ( `${ command } ` ) ;
154-
155- } catch ( e : any ) {
156- vscode . window . showErrorMessage ( e . message || e . toString ( ) ) ;
142+ } catch ( error ) {
143+ vscode . window . showErrorMessage ( `Terminal error: ${ error instanceof Error ? error . message : error } ` ) ;
157144 }
158-
159145 }
160146
161147 private createFolder ( folderPath : string , folderName : string ) {
162-
163148 const fullPath : string = join ( folderPath , folderName ) ;
164149
165- if ( ! existsSync ( fullPath ) ) {
150+ if ( ! existsSync ( fullPath ) ) {
166151 try {
167152 mkdirSync ( fullPath , { recursive : true } ) ;
168- } catch ( e ) {
169- vscode . window . showErrorMessage ( ' Failed to create folder' ) ;
153+ } catch ( error ) {
154+ vscode . window . showErrorMessage ( ` Failed to create folder: ${ error instanceof Error ? error . message : error } ` ) ;
170155 }
171156 }
172-
173157 }
174158
175- private createFile ( filePath : string , templatePath : string ) {
176-
177- if ( ! existsSync ( filePath ) ) {
178- writeFile ( filePath , templatePath , 'utf8' , ( err : NodeJS . ErrnoException | null ) => {
179- if ( err ) {
180- vscode . window . showErrorMessage ( 'Failed to create file' ) ;
181- }
182- } ) ;
159+ private async createFile ( filePath : string , templatePath : string ) {
160+ if ( ! existsSync ( filePath ) ) {
161+ try {
162+ await fsPromises . writeFile ( filePath , templatePath , 'utf8' ) ;
163+ } catch ( error ) {
164+ vscode . window . showErrorMessage ( `Failed to create file: ${ error instanceof Error ? error . message : error } ` ) ;
165+ }
183166 }
184-
185167 }
186168
187169 private setupFileSystemWatcher ( ) {
@@ -190,5 +172,4 @@ export class CodeManage {
190172 this . _watcher . onDidCreate ( this . updateStatusBar . bind ( this ) ) ;
191173 this . _watcher . onDidDelete ( this . updateStatusBar . bind ( this ) ) ;
192174 }
193-
194- } ;
175+ }
0 commit comments