Skip to content
Open
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
50 changes: 50 additions & 0 deletions src/service/gistChange.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";

import { File } from "./file.service";

export interface IGistFileMap {
[fileName: string]: {
content?: string;
};
}

export class GistChangeService {
public static GetChangedSettingFiles(
allSettingFiles: File[],
gistFiles: IGistFileMap
): File[] {
return allSettingFiles.filter(fileToUpload => {
if (fileToUpload.gistName === "cloudSettings") {
return false;
}
if (!gistFiles[fileToUpload.gistName]) {
return true;
}
return gistFiles[fileToUpload.gistName].content !== fileToUpload.content;
});
}

public static GetFilesToUpload(
allSettingFiles: File[],
gistFiles: IGistFileMap,
forceUpload: boolean
): File[] {
if (forceUpload) {
return allSettingFiles;
}

const changedSettingFiles = GistChangeService.GetChangedSettingFiles(
allSettingFiles,
gistFiles
);

return allSettingFiles.filter(fileToUpload => {
return (
fileToUpload.gistName === "cloudSettings" ||
changedSettingFiles.some(
changedFile => changedFile.gistName === fileToUpload.gistName
)
);
});
}
}
8 changes: 6 additions & 2 deletions src/service/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,16 @@ export class GitHubService {
}
}

public UpdateGIST(gistObject: any, files: File[]): any {
public UpdateGIST(
gistObject: any,
files: File[],
localFiles: File[] = files
): any {
const allFiles: string[] = Object.keys(gistObject.data.files);
for (const fileName of allFiles) {
let exists = false;

for (const settingFile of files) {
for (const settingFile of localFiles) {
if (settingFile.gistName === fileName) {
exists = true;
}
Expand Down
43 changes: 24 additions & 19 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ExtensionConfig } from "./models/extensionConfig.model";
import { LocalConfig } from "./models/localConfig.model";
import PragmaUtil from "./pragmaUtil";
import { File, FileService } from "./service/file.service";
import { GistChangeService } from "./service/gistChange.service";
import { GitHubService } from "./service/github.service";
import { ExtensionInformation, PluginService } from "./service/plugin.service";
import { state } from "./state";
Expand Down Expand Up @@ -226,6 +227,7 @@ export class Sync {
allSettingFiles.push(file);

let completed: boolean = false;
let filesToUpload: File[] = allSettingFiles;

let newGIST: boolean = false;
try {
Expand Down Expand Up @@ -284,23 +286,26 @@ export class Sync {
localConfig.publicGist = true;
}

if (
!allSettingFiles.some(fileToUpload => {
if (fileToUpload.gistName === "cloudSettings") {
return false;
}
if (!gistObj.data.files[fileToUpload.gistName]) {
return true;
}
if (
gistObj.data.files[fileToUpload.gistName].content !==
fileToUpload.content
) {
console.info(`Sync: file ${fileToUpload.gistName} has changed`);
return true;
}
})
) {
const changedSettingFiles = GistChangeService.GetChangedSettingFiles(
allSettingFiles,
gistObj.data.files
);

changedSettingFiles.forEach(fileToUpload => {
if (!gistObj.data.files[fileToUpload.gistName]) {
console.info(`Sync: file ${fileToUpload.gistName} is new`);
} else {
console.info(`Sync: file ${fileToUpload.gistName} has changed`);
}
});

filesToUpload = GistChangeService.GetFilesToUpload(
allSettingFiles,
gistObj.data.files,
localConfig.extConfig.forceUpload
);

if (changedSettingFiles.length === 0) {
// Gist files are the same as the local files.
if (!localConfig.extConfig.forceUpload) {
vscode.window.setStatusBarMessage(
Expand Down Expand Up @@ -359,7 +364,7 @@ export class Sync {
3000
);

gistObj = github.UpdateGIST(gistObj, allSettingFiles);
gistObj = github.UpdateGIST(gistObj, filesToUpload, allSettingFiles);
completed = await github.SaveGIST(gistObj.data);
if (!completed) {
vscode.window.showErrorMessage(
Expand Down Expand Up @@ -396,7 +401,7 @@ export class Sync {
if (!syncSetting.quietSync) {
state.commons.ShowSummaryOutput(
true,
allSettingFiles,
filesToUpload,
null,
uploadedExtensions,
ignoredExtensions,
Expand Down
46 changes: 46 additions & 0 deletions test/service/gistChange/gistChangeService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from "chai";

import { File } from "../../../src/service/file.service";
import { GistChangeService } from "../../../src/service/gistChange.service";

describe("GistChangeService", () => {
const unchanged = new File("settings.json", "same", "", "settings.json");
const changed = new File("keybindings.json", "new", "", "keybindings.json");
const created = new File("snippets|demo.json", "created", "", "snippets|demo.json");
const cloudSettings = new File("cloudSettings", "metadata", "", "cloudSettings");
const allFiles = [unchanged, changed, created, cloudSettings];
const gistFiles = {
"settings.json": { content: "same" },
"keybindings.json": { content: "old" }
};

it("returns only new and changed setting files", () => {
const result = GistChangeService.GetChangedSettingFiles(allFiles, gistFiles);

expect(result.map(file => file.gistName)).to.deep.equal([
"keybindings.json",
"snippets|demo.json"
]);
});

it("uploads changed files and cloud settings during normal upload", () => {
const result = GistChangeService.GetFilesToUpload(allFiles, gistFiles, false);

expect(result.map(file => file.gistName)).to.deep.equal([
"keybindings.json",
"snippets|demo.json",
"cloudSettings"
]);
});

it("uploads all files during forced upload", () => {
const result = GistChangeService.GetFilesToUpload(allFiles, gistFiles, true);

expect(result.map(file => file.gistName)).to.deep.equal([
"settings.json",
"keybindings.json",
"snippets|demo.json",
"cloudSettings"
]);
});
});