Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Snippet Sync #928

Merged
merged 4 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
"@types/fs-extra": "^7.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",
Expand All @@ -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"
}
Expand Down
66 changes: 29 additions & 37 deletions src/service/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -95,46 +97,36 @@ export class FileService {

public static async ListFiles(
directory: string,
depth: number,
fullDepth: number,
fileExtensions: string[]
customSettings: CustomConfig
): Promise<File[]> {
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(
Expand Down
38 changes: 6 additions & 32 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -334,6 +305,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
Expand Down