diff --git a/package.json b/package.json index 7be11309..2e199e3b 100644 --- a/package.json +++ b/package.json @@ -162,6 +162,7 @@ "@types/fs-extra": "^8.0.0", "@types/mocha": "^5.2.6", "@types/node": "^12.0.9", + "@types/recursive-readdir": "^2.2.0", "chai": "^4.2.0", "clean-webpack-plugin": "^3.0.0", "mocha": "^6.0.2", @@ -180,6 +181,7 @@ "fs-extra": "^8.0.1", "https-proxy-agent": "^2.2.1", "lockfile": "^1.0.4", + "recursive-readdir": "^2.2.2", "temp": "^0.9.0", "vscode-chokidar": "^1.6.5" } diff --git a/src/service/file.service.ts b/src/service/file.service.ts index 50779568..89c0bfc7 100644 --- a/src/service/file.service.ts +++ b/src/service/file.service.ts @@ -2,6 +2,8 @@ import * as fs from "fs-extra"; import * as path from "path"; +import * as recursiveRead from "recursive-readdir"; +import { CustomConfig } from "../models/customConfig.model"; export class File { constructor( @@ -95,46 +97,36 @@ export class FileService { public static async ListFiles( directory: string, - depth: number, - fullDepth: number, - fileExtensions: string[] + customSettings: CustomConfig ): Promise { - const fileList = await fs.readdir(directory); - - const files: File[] = []; - for (const fileName of fileList) { - const fullPath: string = directory.concat(fileName); - if (await FileService.IsDirectory(fullPath)) { - if (depth < fullDepth) { - for (const element of await FileService.ListFiles( - fullPath + "/", - depth + 1, - fullDepth, - fileExtensions - )) { - files.push(element); - } - } - } else { - const hasExtension: boolean = fullPath.lastIndexOf(".") > 0; - let allowedFile: boolean = false; - if (hasExtension) { - const extension: string = fullPath - .substr(fullPath.lastIndexOf(".") + 1, fullPath.length) - .toLowerCase(); - allowedFile = fileExtensions.filter(m => m === extension).length > 0; - } else { - allowedFile = fileExtensions.filter(m => m === "").length > 0; - } - - if (allowedFile) { - const file: File = await FileService.GetFile(fullPath, fileName); - files.push(file); - } + function folderMatcher(file: string, stats: fs.Stats) { + if (stats.isDirectory()) { + return customSettings.ignoreUploadFolders.some(fold => { + return file.split(path.sep).includes(fold); + }); } + return false; } - - return files; + function fileExtensionMatcher(file: string, stats: fs.Stats) { + if (stats.isDirectory()) { + return false; + } + const ext = path.extname(file).slice(1); + if (!customSettings.supportedFileExtensions.includes(ext)) { + return true; + } + return false; + } + const files = await recursiveRead(directory, [ + ...customSettings.ignoreUploadFiles, + folderMatcher, + fileExtensionMatcher + ]); + return Promise.all( + files.map(file => { + return FileService.GetFile(file, path.basename(file)); + }) + ); } public static async CreateDirTree( diff --git a/src/sync.ts b/src/sync.ts index 3b916d5e..001b4b30 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -175,44 +175,15 @@ export class Sync { allSettingFiles.push(extensionFile); } - let contentFiles: File[] = []; - contentFiles = await FileService.ListFiles( + const contentFiles = await FileService.ListFiles( state.environment.USER_FOLDER, - 0, - 2, - customSettings.supportedFileExtensions + customSettings ); const customExist: boolean = await FileService.FileExists( state.environment.FILE_CUSTOMIZEDSETTINGS ); if (customExist) { - contentFiles = contentFiles.filter( - contentFile => - contentFile.fileName !== - state.environment.FILE_CUSTOMIZEDSETTINGS_NAME - ); - - if (customSettings.ignoreUploadFiles.length > 0) { - contentFiles = contentFiles.filter(contentFile => { - const isMatch: boolean = - customSettings.ignoreUploadFiles.indexOf(contentFile.fileName) === - -1 && - contentFile.fileName !== - state.environment.FILE_CUSTOMIZEDSETTINGS_NAME; - return isMatch; - }); - } - if (customSettings.ignoreUploadFolders.length > 0) { - contentFiles = contentFiles.filter((contentFile: File) => { - const matchedFolders = customSettings.ignoreUploadFolders.filter( - folder => { - return contentFile.filePath.indexOf(folder) !== -1; - } - ); - return matchedFolders.length === 0; - }); - } const customFileKeys: string[] = Object.keys( customSettings.customFiles ); @@ -251,13 +222,13 @@ export class Sync { snippetFile.content ); snippetFile.content = parsedContent; - allSettingFiles.push(snippetFile); } catch (e) { Commons.LogException(null, e.message, true); console.error(e); return; } } + allSettingFiles.push(snippetFile); } } } @@ -331,6 +302,9 @@ export class Sync { if (fileToUpload.fileName === "cloudSettings") { return false; } + if (!gistObj.data.files[fileToUpload.fileName]) { + return true; + } if ( gistObj.data.files[fileToUpload.fileName].content !== fileToUpload.content