Fixed sketch service

This commit is contained in:
jbicker 2019-07-23 13:42:52 +02:00
parent cd8c138e1e
commit 75f7d3ca7c
3 changed files with 43 additions and 32 deletions

View File

@ -359,7 +359,7 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
if (fileStat) { if (fileStat) {
const sketchFiles = await this.sketches.getSketchFiles(fileStat); const sketchFiles = await this.sketches.getSketchFiles(fileStat);
sketchFiles.forEach(sketchFile => { sketchFiles.forEach(sketchFile => {
const uri = new URI(sketchFile); const uri = new URI(sketchFile.uri);
this.editorManager.open(uri); this.editorManager.open(uri);
}); });
} }

View File

@ -4,7 +4,7 @@ export const SketchesServicePath = '/services/sketches-service';
export const SketchesService = Symbol('SketchesService'); export const SketchesService = Symbol('SketchesService');
export interface SketchesService { export interface SketchesService {
getSketches(fileStat?: FileStat): Promise<Sketch[]> getSketches(fileStat?: FileStat): Promise<Sketch[]>
getSketchFiles(fileStat: FileStat): Promise<string[]> getSketchFiles(fileStat: FileStat): Promise<FileStat[]>
} }
export interface Sketch { export interface Sketch {

View File

@ -17,17 +17,19 @@ export class SketchesServiceImpl implements SketchesService {
const sketches: Sketch[] = []; const sketches: Sketch[] = [];
if (fileStat && fileStat.isDirectory) { if (fileStat && fileStat.isDirectory) {
const uri = new URI(fileStat.uri); const uri = new URI(fileStat.uri);
const sketchFolderPath = uri.path.toString() const sketchFolderPath = await this.filesystem.getFsPath(uri.toString());
const files = fs.readdirSync(sketchFolderPath); if (sketchFolderPath) {
files.forEach(file => { const files = fs.readdirSync(sketchFolderPath);
const filePath = path.join(sketchFolderPath, file); files.forEach(file => {
if (this.isSketchFolder(filePath, file)) { const filePath = path.join(sketchFolderPath, file);
sketches.push({ if (this.isSketchFolder(filePath, file)) {
name: file, sketches.push({
uri: filePath name: file,
}); uri: filePath
} });
}); }
});
}
} }
return sketches; return sketches;
} }
@ -36,30 +38,39 @@ export class SketchesServiceImpl implements SketchesService {
* Return all allowed files. * Return all allowed files.
* File extensions: "c", "cpp", "h", "hh", "hpp", "s", "pde", "ino" * File extensions: "c", "cpp", "h", "hh", "hpp", "s", "pde", "ino"
*/ */
async getSketchFiles(sketchFileStat: FileStat): Promise<string[]> { async getSketchFiles(sketchFileStat: FileStat): Promise<FileStat[]> {
const files: string[] = []; const files: FileStat[] = [];
const sketchUri = new URI(sketchFileStat.uri); const sketchUri = new URI(sketchFileStat.uri);
const sketchPath = sketchUri.path.toString(); const sketchPath = await this.filesystem.getFsPath(sketchUri.toString());
if (sketchFileStat.isDirectory && this.isSketchFolder(sketchPath, sketchUri.displayName)) { if (sketchPath) {
const sketchDirContents = fs.readdirSync(sketchPath); if (sketchFileStat.isDirectory && this.isSketchFolder(sketchPath, sketchUri.displayName)) {
sketchDirContents.forEach(fileName => { const sketchDirContents = fs.readdirSync(sketchPath);
const filePath = path.join(sketchPath, fileName); for (const i in sketchDirContents) {
if (fs.existsSync(filePath) && const fileName = sketchDirContents[i];
fs.lstatSync(filePath).isFile() && const filePath = path.join(sketchPath, fileName);
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) { if (fs.existsSync(filePath) &&
files.push(filePath); fs.lstatSync(filePath).isFile() &&
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) {
const fileStat = await this.filesystem.getFileStat(filePath);
if (fileStat) {
console.log("111111111111", fileStat);
files.push(fileStat);
console.log("222222222222222", files);
}
}
} }
}); } else {
} else { const sketchDir = path.dirname(sketchPath);
const sketchDir = sketchUri.path.dir; if (sketchDir && this.isSketchFolder(sketchDir, sketchUri.path.dir.name)) {
if (this.isSketchFolder(sketchDir.toString(), sketchDir.name)) { const sketchFolderStat = await this.filesystem.getFileStat(sketchUri.path.dir.toString());
const sketchFolderStat = await this.filesystem.getFileStat(sketchDir.toString()); if (sketchFolderStat) {
if (sketchFolderStat) { const sketchDirContents = await this.getSketchFiles(sketchFolderStat);
const sketchDirContents = await this.getSketchFiles(sketchFolderStat); files.push(...sketchDirContents);
files.push(...sketchDirContents); }
} }
} }
} }
console.log("###FILEPATH###", files);
return files; return files;
} }