Skip to content

Commit 5056af3

Browse files
committed
feat(diff): Diff files download to local and persist mock folder.
1 parent 990a0a5 commit 5056af3

File tree

4 files changed

+57
-124
lines changed

4 files changed

+57
-124
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/diff/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ConflictDiffView from './conflict_diff_view';
55
import { TDiffType } from './abstract_diff_view';
66
import type InvioPlugin from '../main';
77
import { diff_match_patch } from './effective_diff.js';
8+
import { log } from '../moreOnLog'
89

910
export * from './abstract_diff_view';
1011

@@ -21,20 +22,23 @@ export function openDiffModal(app: App, plugin: InvioPlugin, file: TFile, remote
2122
}
2223
}
2324

24-
25-
export async function getRemoteFileDiff(vault: Vault, filePath: string, remoteMD: string) {
25+
// For local files not exist or invalid, always return diff: true
26+
export async function isRemoteFileDiff(vault: Vault, filePath: string, remoteMD: string) {
27+
const localExist = await vault.adapter.exists(filePath)
28+
if (!localExist) {
29+
log.info(`file ${filePath} not existed locally`)
30+
return true;
31+
}
2632
const file = vault.getAbstractFileByPath(filePath)
2733
if (!(file instanceof TFile)) {
28-
new Notice('Not valid file');
29-
return;
34+
log.info(`file ${filePath} not a valid file`)
35+
return true;
3036
}
3137
const localContent = await vault.adapter.readBinary(filePath).then(buf => new TextDecoder().decode(buf));
32-
console.log('updated local contents: ', filePath, localContent);
3338
const dmp: any = new diff_match_patch();
3439
const uDiff = dmp.diff_main(localContent, remoteMD);
3540
dmp.diff_cleanupSemantic(uDiff);
3641

3742
const diff = (uDiff?.filter((item: any) => item[0] !== 0)).length > 0
38-
console.log('diff result: ', diff, uDiff);
3943
return !!diff;
4044
}

src/sync.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import { isInsideObsFolder, ObsConfigDirFileType } from "./obsFolderLister";
5656
import { Utils } from './utils/utils';
5757
import { Path } from './utils/path';
5858
import { log } from "./moreOnLog";
59-
import { getRemoteFileDiff } from './diff/index';
59+
import { isRemoteFileDiff } from './diff/index';
6060

6161
export const RemoteSrcPrefix = 'op-remote-source-raw/'
6262
export const RemoteAttPrefix = 'op-remote-attach-p/'
@@ -443,14 +443,30 @@ export const pruneTouchedFiles = async (vault: Vault, client: RemoteClient, list
443443
if (!remoteMD) {
444444
return;
445445
}
446-
const diff = await getRemoteFileDiff(vault, item.key, remoteMD.data);
446+
const diff = await isRemoteFileDiff(vault, item.key, remoteMD.data);
447447
if (!diff) {
448-
log.info('file contents diff nothing', attr, item)
448+
log.info('file contents diff nothing', attr, item.key)
449449
list[attr] = null;
450450
delete list[attr];
451451
return attr;
452452
}
453453
return;
454+
} else if (item.decision === 'downloadRemoteToLocal') {
455+
const remoteMD = await fetchRemoteFileMD(
456+
item.key,
457+
client,
458+
vault,
459+
).catch(err => {
460+
log.info('remote empty ', err);
461+
return null;
462+
})
463+
log.info('remote md: ', remoteMD);
464+
if (!remoteMD || !(await isRemoteFileDiff(vault, item.key, remoteMD.data))) {
465+
log.info('file contents diff nothing', attr, item.key)
466+
list[attr] = null;
467+
delete list[attr];
468+
return attr;
469+
}
454470
}
455471
})
456472
const result = await Promise.all(promises);

src/utils.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { AppHostServerUrl } from './remoteForS3';
1010
import { loadGA } from './ga';
1111
import type InvioPlugin from './main';
1212
import { DEFAULT_DIR, DEFAULT_FILE_URL } from './settings';
13-
import { mkdirpInVault } from './misc'
13+
import { log } from './moreOnLog'
1414

1515
const logger = console;
1616
logger.info = console.log;
@@ -84,38 +84,37 @@ const gotoMainSite = () => {
8484
}
8585

8686
const mockLocaleFile = async (plugin:InvioPlugin) => {
87-
let defaultFolder = DEFAULT_DIR
88-
const existed = await plugin.app.vault.adapter.exists(defaultFolder)
89-
if (existed) {
90-
defaultFolder += `_${Math.random().toFixed(4).slice(2)}`
91-
}
92-
plugin.app.vault.adapter.mkdir(defaultFolder)
93-
.then(() => {
87+
try {
88+
const defaultFolder = DEFAULT_DIR
89+
const existed = await plugin.app.vault.adapter.exists(defaultFolder)
90+
if (!existed) {
91+
await plugin.app.vault.adapter.mkdir(defaultFolder)
92+
}
9493
plugin.settings.localWatchDir = defaultFolder
95-
return plugin.saveSettings()
96-
})
97-
.then(() => {
94+
await plugin.saveSettings()
9895
plugin.ga.trace('boot_project', {
9996
dirname: plugin.settings.localWatchDir
10097
});
101-
plugin.switchWorkingDir(plugin.settings.localWatchDir);
102-
})
103-
.then(async () => {
104-
// Add a new file
105-
const arrayBuffer = await requestUrl({
106-
url: DEFAULT_FILE_URL
107-
}).then(resp => resp.arrayBuffer)
108-
return plugin.app.vault.adapter.writeBinary(`${defaultFolder}/Introduction.md`, arrayBuffer)
109-
})
110-
.then(async () => {
111-
const created = await plugin.app.vault.adapter.exists(`${defaultFolder}/Introduction.md`);
112-
if (created) {
113-
let parentNode: any = document.querySelector(`[data-path="${defaultFolder}"]`);
114-
parentNode?.click();
115-
let node: any = document.querySelector(`[data-path="${defaultFolder}/Introduction.md"]`);
116-
node?.click();
117-
}
118-
})
98+
await plugin.switchWorkingDir(plugin.settings.localWatchDir)
99+
100+
const arrayBuffer = await requestUrl({
101+
url: DEFAULT_FILE_URL
102+
}).then(resp => resp.arrayBuffer)
103+
104+
const introFilePath = `${defaultFolder}/Introduction.md`
105+
await plugin.app.vault.adapter.exists(introFilePath)
106+
.then(existed => {
107+
if (!existed) {
108+
return plugin.app.vault.adapter.writeBinary(introFilePath, arrayBuffer)
109+
}
110+
})
111+
let parentNode: any = document.querySelector(`[data-path="${defaultFolder}"]`);
112+
parentNode?.click();
113+
let node: any = document.querySelector(`[data-path="${introFilePath}"]`);
114+
node?.click();
115+
} catch (error) {
116+
log.error('create mock folder failed: ', error);
117+
}
119118
}
120119
const Utils = {
121120
md5Hash,

0 commit comments

Comments
 (0)