Skip to content

Commit 2031e8d

Browse files
KominaKomina
authored andcommitted
fix: upload only changed setting files
1 parent eb332ba commit 2031e8d

4 files changed

Lines changed: 126 additions & 21 deletions

File tree

src/service/gistChange.service.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
3+
import { File } from "./file.service";
4+
5+
export interface IGistFileMap {
6+
[fileName: string]: {
7+
content?: string;
8+
};
9+
}
10+
11+
export class GistChangeService {
12+
public static GetChangedSettingFiles(
13+
allSettingFiles: File[],
14+
gistFiles: IGistFileMap
15+
): File[] {
16+
return allSettingFiles.filter(fileToUpload => {
17+
if (fileToUpload.gistName === "cloudSettings") {
18+
return false;
19+
}
20+
if (!gistFiles[fileToUpload.gistName]) {
21+
return true;
22+
}
23+
return gistFiles[fileToUpload.gistName].content !== fileToUpload.content;
24+
});
25+
}
26+
27+
public static GetFilesToUpload(
28+
allSettingFiles: File[],
29+
gistFiles: IGistFileMap,
30+
forceUpload: boolean
31+
): File[] {
32+
if (forceUpload) {
33+
return allSettingFiles;
34+
}
35+
36+
const changedSettingFiles = GistChangeService.GetChangedSettingFiles(
37+
allSettingFiles,
38+
gistFiles
39+
);
40+
41+
return allSettingFiles.filter(fileToUpload => {
42+
return (
43+
fileToUpload.gistName === "cloudSettings" ||
44+
changedSettingFiles.some(
45+
changedFile => changedFile.gistName === fileToUpload.gistName
46+
)
47+
);
48+
});
49+
}
50+
}

src/service/github.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,16 @@ export class GitHubService {
164164
}
165165
}
166166

167-
public UpdateGIST(gistObject: any, files: File[]): any {
167+
public UpdateGIST(
168+
gistObject: any,
169+
files: File[],
170+
localFiles: File[] = files
171+
): any {
168172
const allFiles: string[] = Object.keys(gistObject.data.files);
169173
for (const fileName of allFiles) {
170174
let exists = false;
171175

172-
for (const settingFile of files) {
176+
for (const settingFile of localFiles) {
173177
if (settingFile.gistName === fileName) {
174178
exists = true;
175179
}

src/sync.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ExtensionConfig } from "./models/extensionConfig.model";
1111
import { LocalConfig } from "./models/localConfig.model";
1212
import PragmaUtil from "./pragmaUtil";
1313
import { File, FileService } from "./service/file.service";
14+
import { GistChangeService } from "./service/gistChange.service";
1415
import { GitHubService } from "./service/github.service";
1516
import { ExtensionInformation, PluginService } from "./service/plugin.service";
1617
import { state } from "./state";
@@ -226,6 +227,7 @@ export class Sync {
226227
allSettingFiles.push(file);
227228

228229
let completed: boolean = false;
230+
let filesToUpload: File[] = allSettingFiles;
229231

230232
let newGIST: boolean = false;
231233
try {
@@ -284,23 +286,26 @@ export class Sync {
284286
localConfig.publicGist = true;
285287
}
286288

287-
if (
288-
!allSettingFiles.some(fileToUpload => {
289-
if (fileToUpload.gistName === "cloudSettings") {
290-
return false;
291-
}
292-
if (!gistObj.data.files[fileToUpload.gistName]) {
293-
return true;
294-
}
295-
if (
296-
gistObj.data.files[fileToUpload.gistName].content !==
297-
fileToUpload.content
298-
) {
299-
console.info(`Sync: file ${fileToUpload.gistName} has changed`);
300-
return true;
301-
}
302-
})
303-
) {
289+
const changedSettingFiles = GistChangeService.GetChangedSettingFiles(
290+
allSettingFiles,
291+
gistObj.data.files
292+
);
293+
294+
changedSettingFiles.forEach(fileToUpload => {
295+
if (!gistObj.data.files[fileToUpload.gistName]) {
296+
console.info(`Sync: file ${fileToUpload.gistName} is new`);
297+
} else {
298+
console.info(`Sync: file ${fileToUpload.gistName} has changed`);
299+
}
300+
});
301+
302+
filesToUpload = GistChangeService.GetFilesToUpload(
303+
allSettingFiles,
304+
gistObj.data.files,
305+
localConfig.extConfig.forceUpload
306+
);
307+
308+
if (changedSettingFiles.length === 0) {
304309
// Gist files are the same as the local files.
305310
if (!localConfig.extConfig.forceUpload) {
306311
vscode.window.setStatusBarMessage(
@@ -359,7 +364,7 @@ export class Sync {
359364
3000
360365
);
361366

362-
gistObj = github.UpdateGIST(gistObj, allSettingFiles);
367+
gistObj = github.UpdateGIST(gistObj, filesToUpload, allSettingFiles);
363368
completed = await github.SaveGIST(gistObj.data);
364369
if (!completed) {
365370
vscode.window.showErrorMessage(
@@ -396,7 +401,7 @@ export class Sync {
396401
if (!syncSetting.quietSync) {
397402
state.commons.ShowSummaryOutput(
398403
true,
399-
allSettingFiles,
404+
filesToUpload,
400405
null,
401406
uploadedExtensions,
402407
ignoredExtensions,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect } from "chai";
2+
3+
import { File } from "../../../src/service/file.service";
4+
import { GistChangeService } from "../../../src/service/gistChange.service";
5+
6+
describe("GistChangeService", () => {
7+
const unchanged = new File("settings.json", "same", "", "settings.json");
8+
const changed = new File("keybindings.json", "new", "", "keybindings.json");
9+
const created = new File("snippets|demo.json", "created", "", "snippets|demo.json");
10+
const cloudSettings = new File("cloudSettings", "metadata", "", "cloudSettings");
11+
const allFiles = [unchanged, changed, created, cloudSettings];
12+
const gistFiles = {
13+
"settings.json": { content: "same" },
14+
"keybindings.json": { content: "old" }
15+
};
16+
17+
it("returns only new and changed setting files", () => {
18+
const result = GistChangeService.GetChangedSettingFiles(allFiles, gistFiles);
19+
20+
expect(result.map(file => file.gistName)).to.deep.equal([
21+
"keybindings.json",
22+
"snippets|demo.json"
23+
]);
24+
});
25+
26+
it("uploads changed files and cloud settings during normal upload", () => {
27+
const result = GistChangeService.GetFilesToUpload(allFiles, gistFiles, false);
28+
29+
expect(result.map(file => file.gistName)).to.deep.equal([
30+
"keybindings.json",
31+
"snippets|demo.json",
32+
"cloudSettings"
33+
]);
34+
});
35+
36+
it("uploads all files during forced upload", () => {
37+
const result = GistChangeService.GetFilesToUpload(allFiles, gistFiles, true);
38+
39+
expect(result.map(file => file.gistName)).to.deep.equal([
40+
"settings.json",
41+
"keybindings.json",
42+
"snippets|demo.json",
43+
"cloudSettings"
44+
]);
45+
});
46+
});

0 commit comments

Comments
 (0)