Move manifest file name definition

This commit is contained in:
Silvano Cerza 2025-01-23 17:25:44 +01:00
parent 0ba9420b3a
commit 476b17ba05
3 changed files with 19 additions and 14 deletions

View file

@ -1,5 +1,5 @@
import { Vault, TAbstractFile, TFolder } from "obsidian"; import { Vault, TAbstractFile, TFolder } from "obsidian";
import MetadataStore from "./metadata-store"; import MetadataStore, { MANIFEST_FILE_NAME } from "./metadata-store";
import { GitHubSyncSettings } from "./settings/settings"; import { GitHubSyncSettings } from "./settings/settings";
import Logger from "./logger"; import Logger from "./logger";
@ -132,7 +132,7 @@ export default class EventsListener {
} }
private isSyncable(filePath: string) { private isSyncable(filePath: string) {
if (filePath === `${this.vault.configDir}/github-sync-metadata.json`) { if (filePath === `${this.vault.configDir}/${MANIFEST_FILE_NAME}`) {
// Manifest file must always be synced // Manifest file must always be synced
return true; return true;
} else if ( } else if (

View file

@ -1,5 +1,7 @@
import { Vault, normalizePath } from "obsidian"; import { Vault, normalizePath } from "obsidian";
export const MANIFEST_FILE_NAME = "github-sync-metadata.json" as const;
/** /**
* A file metadata. * A file metadata.
* Store info that makes easier to track a file locally and in the remote repo. * Store info that makes easier to track a file locally and in the remote repo.
@ -42,7 +44,7 @@ export default class MetadataStore {
constructor(private vault: Vault) { constructor(private vault: Vault) {
this.metadataFile = normalizePath( this.metadataFile = normalizePath(
`${this.vault.configDir}/github-sync-metadata.json`, `${this.vault.configDir}/${MANIFEST_FILE_NAME}`,
); );
} }

View file

@ -4,7 +4,11 @@ import GithubClient, {
NewTreeRequestItem, NewTreeRequestItem,
RepoContent, RepoContent,
} from "./github/client"; } from "./github/client";
import MetadataStore, { FileMetadata, Metadata } from "./metadata-store"; import MetadataStore, {
FileMetadata,
Metadata,
MANIFEST_FILE_NAME,
} from "./metadata-store";
import EventsListener from "./events-listener"; import EventsListener from "./events-listener";
import { GitHubSyncSettings } from "./settings/settings"; import { GitHubSyncSettings } from "./settings/settings";
import Logger from "./logger"; import Logger from "./logger";
@ -93,7 +97,7 @@ export default class SyncManager {
// So we create a the manifest file as the first commit, since we're going // So we create a the manifest file as the first commit, since we're going
// to create that in any case right after this. // to create that in any case right after this.
await this.client.createFile( await this.client.createFile(
`${this.vault.configDir}/github-sync-metadata.json`, `${this.vault.configDir}/${MANIFEST_FILE_NAME}`,
"", "",
"Initial commit", "Initial commit",
); );
@ -144,7 +148,7 @@ export default class SyncManager {
if ( if (
this.settings.syncConfigDir && this.settings.syncConfigDir &&
filePath.startsWith(this.vault.configDir) && filePath.startsWith(this.vault.configDir) &&
filePath !== `${this.vault.configDir}/github-sync-metadata.json` filePath !== `${this.vault.configDir}/${MANIFEST_FILE_NAME}`
) { ) {
// Include files in the config dir only if the user has enabled it. // Include files in the config dir only if the user has enabled it.
// The metadata file must always be synced. // The metadata file must always be synced.
@ -239,7 +243,7 @@ export default class SyncManager {
async sync() { async sync() {
await this.logger.info("Starting sync"); await this.logger.info("Starting sync");
const { files, sha: treeSha } = await this.client.getRepoContent(); const { files, sha: treeSha } = await this.client.getRepoContent();
const manifest = files[`${this.vault.configDir}/github-sync-metadata.json`]; const manifest = files[`${this.vault.configDir}/${MANIFEST_FILE_NAME}`];
if (manifest === undefined) { if (manifest === undefined) {
await this.logger.error("Remote manifest is missing", { files, treeSha }); await this.logger.error("Remote manifest is missing", { files, treeSha });
@ -512,8 +516,7 @@ export default class SyncManager {
return actions.filter((action: SyncAction) => { return actions.filter((action: SyncAction) => {
return ( return (
!action.filePath.startsWith(this.vault.configDir) || !action.filePath.startsWith(this.vault.configDir) ||
action.filePath === action.filePath === `${this.vault.configDir}/${MANIFEST_FILE_NAME}`
`${this.vault.configDir}/github-sync-metadata.json`
); );
}); });
} }
@ -536,8 +539,8 @@ export default class SyncManager {
this.metadataStore.save(); this.metadataStore.save();
// Update manifest in list of new tree items // Update manifest in list of new tree items
delete treeFiles[`${this.vault.configDir}/github-sync-metadata.json`].sha; delete treeFiles[`${this.vault.configDir}/${MANIFEST_FILE_NAME}`].sha;
treeFiles[`${this.vault.configDir}/github-sync-metadata.json`].content = treeFiles[`${this.vault.configDir}/${MANIFEST_FILE_NAME}`].content =
JSON.stringify(this.metadataStore.data); JSON.stringify(this.metadataStore.data);
// Create the new tree // Create the new tree
@ -639,9 +642,9 @@ export default class SyncManager {
// Must be the first time we run, initialize the metadata store // Must be the first time we run, initialize the metadata store
// with itself and all files in the vault. // with itself and all files in the vault.
this.metadataStore.data.files[ this.metadataStore.data.files[
`${this.vault.configDir}/github-sync-metadata.json` `${this.vault.configDir}/${MANIFEST_FILE_NAME}`
] = { ] = {
path: `${this.vault.configDir}/github-sync-metadata.json`, path: `${this.vault.configDir}/${MANIFEST_FILE_NAME}`,
sha: null, sha: null,
dirty: false, dirty: false,
justDownloaded: false, justDownloaded: false,
@ -708,7 +711,7 @@ export default class SyncManager {
// Remove all them from the metadata store // Remove all them from the metadata store
files.forEach((filePath: string) => { files.forEach((filePath: string) => {
if (filePath === `${this.vault.configDir}/github-sync-metadata.json`) { if (filePath === `${this.vault.configDir}/${MANIFEST_FILE_NAME}`) {
// We don't want to remove the metadata file even if it's in the config dir // We don't want to remove the metadata file even if it's in the config dir
return; return;
} }