1
- import { injectable } from "inversify" ;
1
+ import { injectable , inject } from "inversify" ;
2
2
import { SketchesService , Sketch } from "../common/protocol/sketches-service" ;
3
3
import URI from "@theia/core/lib/common/uri" ;
4
- import { FileStat } from "@theia/filesystem/lib/common" ;
4
+ import { FileStat , FileSystem } from "@theia/filesystem/lib/common" ;
5
5
import * as fs from 'fs' ;
6
6
import * as path from 'path' ;
7
7
@@ -10,10 +10,14 @@ export const ALLOWED_FILE_EXTENSIONS = [".c", ".cpp", ".h", ".hh", ".hpp", ".s",
10
10
@injectable ( )
11
11
export class SketchesServiceImpl implements SketchesService {
12
12
13
+ @inject ( FileSystem )
14
+ protected readonly filesystem : FileSystem ;
15
+
13
16
async getSketches ( fileStat ?: FileStat ) : Promise < Sketch [ ] > {
14
17
const sketches : Sketch [ ] = [ ] ;
15
18
if ( fileStat && fileStat . isDirectory ) {
16
- const sketchFolderPath = this . getPath ( fileStat ) ;
19
+ const uri = new URI ( fileStat . uri ) ;
20
+ const sketchFolderPath = uri . path . toString ( )
17
21
const files = fs . readdirSync ( sketchFolderPath ) ;
18
22
files . forEach ( file => {
19
23
const filePath = path . join ( sketchFolderPath , file ) ;
@@ -32,18 +36,30 @@ export class SketchesServiceImpl implements SketchesService {
32
36
* Return all allowed files.
33
37
* File extensions: "c", "cpp", "h", "hh", "hpp", "s", "pde", "ino"
34
38
*/
35
- async getSketchFiles ( sketchDir : FileStat ) : Promise < string [ ] > {
39
+ async getSketchFiles ( sketchFileStat : FileStat ) : Promise < string [ ] > {
36
40
const files : string [ ] = [ ] ;
37
- const sketchDirPath = this . getPath ( sketchDir ) ;
38
- const sketchDirContents = fs . readdirSync ( sketchDirPath ) ;
39
- sketchDirContents . forEach ( fileName => {
40
- const filePath = path . join ( sketchDirPath , fileName ) ;
41
- if ( fs . existsSync ( filePath ) &&
42
- fs . lstatSync ( filePath ) . isFile ( ) &&
43
- ALLOWED_FILE_EXTENSIONS . indexOf ( path . extname ( filePath ) ) !== - 1 ) {
41
+ const sketchUri = new URI ( sketchFileStat . uri ) ;
42
+ const sketchPath = sketchUri . path . toString ( ) ;
43
+ if ( sketchFileStat . isDirectory && this . isSketchFolder ( sketchPath , sketchUri . displayName ) ) {
44
+ const sketchDirContents = fs . readdirSync ( sketchPath ) ;
45
+ sketchDirContents . forEach ( fileName => {
46
+ const filePath = path . join ( sketchPath , fileName ) ;
47
+ if ( fs . existsSync ( filePath ) &&
48
+ fs . lstatSync ( filePath ) . isFile ( ) &&
49
+ ALLOWED_FILE_EXTENSIONS . indexOf ( path . extname ( filePath ) ) !== - 1 ) {
44
50
files . push ( filePath ) ;
51
+ }
52
+ } ) ;
53
+ } else {
54
+ const sketchDir = sketchUri . path . dir ;
55
+ if ( this . isSketchFolder ( sketchDir . toString ( ) , sketchDir . name ) ) {
56
+ const sketchFolderStat = await this . filesystem . getFileStat ( sketchDir . toString ( ) ) ;
57
+ if ( sketchFolderStat ) {
58
+ const sketchDirContents = await this . getSketchFiles ( sketchFolderStat ) ;
59
+ files . push ( ...sketchDirContents ) ;
60
+ }
45
61
}
46
- } ) ;
62
+ }
47
63
return files ;
48
64
}
49
65
@@ -58,10 +74,4 @@ export class SketchesServiceImpl implements SketchesService {
58
74
}
59
75
return false ;
60
76
}
61
-
62
- protected getPath ( fileStat : FileStat ) {
63
- const fileStatUri = fileStat . uri ;
64
- const uri = new URI ( fileStatUri ) ;
65
- return uri . path . toString ( ) ;
66
- }
67
77
}
0 commit comments