mirror of
https://github.com/silvanocerza/github-gitless-sync.git
synced 2026-07-22 12:10:28 +00:00
Fix changing sync config settings
This commit is contained in:
parent
8e65f2767f
commit
2c463ca34e
3 changed files with 82 additions and 9 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { Vault, TAbstractFile, TFolder, TFile } from "obsidian";
|
||||
import { Vault, TAbstractFile, TFolder } from "obsidian";
|
||||
import MetadataStore from "./metadata-store";
|
||||
import { GitHubSyncSettings } from "./settings/settings";
|
||||
|
||||
/**
|
||||
* Tracks changes to local sync directory and updates files metadata.
|
||||
|
|
@ -8,8 +9,7 @@ export default class EventsListener {
|
|||
constructor(
|
||||
private vault: Vault,
|
||||
private metadataStore: MetadataStore,
|
||||
private localContentDir: string,
|
||||
private repoContentDir: string,
|
||||
private settings: GitHubSyncSettings,
|
||||
) {}
|
||||
|
||||
start() {
|
||||
|
|
@ -39,8 +39,11 @@ export default class EventsListener {
|
|||
}
|
||||
|
||||
let remotePath: string;
|
||||
if (file.path.startsWith(this.localContentDir)) {
|
||||
remotePath = file.path.replace(this.localContentDir, this.repoContentDir);
|
||||
if (file.path.startsWith(this.settings.localContentDir)) {
|
||||
remotePath = file.path.replace(
|
||||
this.settings.localContentDir,
|
||||
this.settings.repoContentDir,
|
||||
);
|
||||
} else if (
|
||||
file.path.startsWith(`${this.vault.configDir}/github-sync-metadata.json`)
|
||||
) {
|
||||
|
|
@ -129,8 +132,9 @@ export default class EventsListener {
|
|||
|
||||
private isSyncable(filePath: string) {
|
||||
return (
|
||||
filePath.startsWith(this.localContentDir) ||
|
||||
filePath.startsWith(`${this.vault.configDir}/github-sync-metadata.json`)
|
||||
filePath.startsWith(this.settings.localContentDir) ||
|
||||
filePath === `${this.vault.configDir}/github-sync-metadata.json` ||
|
||||
(this.settings.syncConfigDir && filePath.startsWith(this.vault.configDir))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -189,6 +189,11 @@ export default class GitHubSyncSettingsTab extends PluginSettingTab {
|
|||
.setValue(this.plugin.settings.syncConfigDir)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.syncConfigDir = value;
|
||||
if (value) {
|
||||
await this.plugin.syncManager.addConfigDirToMetadata();
|
||||
} else {
|
||||
await this.plugin.syncManager.removeConfigDirFromMetadata();
|
||||
}
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -39,8 +39,7 @@ export default class SyncManager {
|
|||
this.eventsListener = new EventsListener(
|
||||
this.vault,
|
||||
this.metadataStore,
|
||||
this.settings.localContentDir,
|
||||
this.settings.repoContentDir,
|
||||
this.settings,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -660,6 +659,71 @@ export default class SyncManager {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all the files in the config dir in the metadata store.
|
||||
* This is mainly useful when the user changes the sync config settings
|
||||
* as we need to add those files to the metadata store or they would never be synced.
|
||||
*/
|
||||
async addConfigDirToMetadata() {
|
||||
// Get all the files in the config dir
|
||||
let files = [];
|
||||
let folders = [this.vault.configDir];
|
||||
while (folders.length > 0) {
|
||||
const folder = folders.pop();
|
||||
if (folder === undefined) {
|
||||
continue;
|
||||
}
|
||||
const res = await this.vault.adapter.list(folder);
|
||||
files.push(...res.files);
|
||||
folders.push(...res.folders);
|
||||
}
|
||||
// Add them to the metadata store
|
||||
files.forEach((filePath: string) => {
|
||||
this.metadataStore.data.files[filePath] = {
|
||||
localPath: filePath,
|
||||
// Remote path is the same for config dir files
|
||||
remotePath: filePath,
|
||||
sha: null,
|
||||
dirty: false,
|
||||
justDownloaded: false,
|
||||
lastModified: Date.now(),
|
||||
};
|
||||
});
|
||||
this.metadataStore.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all the files in the config dir from the metadata store.
|
||||
* The metadata file is not removed as it must always be present.
|
||||
* This is mainly useful when the user changes the sync config settings
|
||||
* as we need to remove those files to the metadata store or they would
|
||||
* keep being synced.
|
||||
*/
|
||||
async removeConfigDirFromMetadata() {
|
||||
// Get all the files in the config dir
|
||||
let files = [];
|
||||
let folders = [this.vault.configDir];
|
||||
while (folders.length > 0) {
|
||||
const folder = folders.pop();
|
||||
if (folder === undefined) {
|
||||
continue;
|
||||
}
|
||||
const res = await this.vault.adapter.list(folder);
|
||||
files.push(...res.files);
|
||||
folders.push(...res.folders);
|
||||
}
|
||||
|
||||
// Remove all them from the metadata store
|
||||
files.forEach((filePath: string) => {
|
||||
if (filePath === `${this.vault.configDir}/github-sync-metadata.json`) {
|
||||
// We don't want to remove the metadata file even if it's in the config dir
|
||||
return;
|
||||
}
|
||||
delete this.metadataStore.data.files[filePath];
|
||||
});
|
||||
this.metadataStore.save();
|
||||
}
|
||||
|
||||
getFileMetadata(filePath: string): FileMetadata {
|
||||
return this.metadataStore.data.files[filePath];
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue