Skip to content

Commit

Permalink
proper config push
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Xiong committed Dec 1, 2024
1 parent 2153937 commit 0515cc1
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 72 deletions.
65 changes: 65 additions & 0 deletions helpers/drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ interface QueryMatch {

export const folderMimeType = "application/vnd.google-apps.folder";

const BLACKLISTED_CONFIG_FILES = [
"graph.json",
"workspace.json",
"workspace-mobile.json",
];

const WHITELISTED_PLUGIN_FILES = [
"manifest.json",
"styles.css",
"main.js",
"data.json",
];

const stringSearchToQuery = (search: StringSearch) => {
if (typeof search === "string") return `='${search}'`;
if ("contains" in search) return ` contains '${search.contains}'`;
Expand Down Expand Up @@ -440,6 +453,57 @@ export const getDriveClient = (t: ObsidianGoogleDrive) => {
await Promise.all(files.map((file) => t.deleteFile(file)));
};

const getConfigFilesToSync = async () => {
const configFilesToSync: string[] = [];
const { vault } = t.app;
const { adapter } = vault;

const [configFiles, plugins] = await Promise.all([
adapter.list(vault.configDir),
adapter.list(vault.configDir + "/plugins"),
]);

await Promise.all(
configFiles.files
.filter(
(path) =>
!BLACKLISTED_CONFIG_FILES.includes(
fileNameFromPath(path)
)
)
.map(async (path) => {
const file = await adapter.stat(path);
if ((file?.mtime || 0) > t.settings.lastSyncedAt) {
configFilesToSync.push(path);
}
})
.concat(
plugins.folders.map(async (plugin) => {
const files = await adapter.list(plugin);
await Promise.all(
files.files
.filter((path) =>
WHITELISTED_PLUGIN_FILES.includes(
fileNameFromPath(path)
)
)
.map(async (path) => {
const file = await adapter.stat(path);
if (
(file?.mtime || 0) >
t.settings.lastSyncedAt
) {
configFilesToSync.push(path);
}
})
);
})
)
);

return configFilesToSync;
};

return {
paginateFiles,
searchFiles,
Expand All @@ -458,6 +522,7 @@ export const getDriveClient = (t: ObsidianGoogleDrive) => {
batchDelete,
checkConnection,
deleteFilesMinimumOperations,
getConfigFilesToSync,
};
};

Expand Down
57 changes: 2 additions & 55 deletions helpers/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ import {
} from "./drive";
import { pull } from "./pull";

const BLACKLISTED_CONFIG_FILES = [
"graph.json",
"workspace.json",
"workspace-mobile.json",
];

const WHITELISTED_PLUGIN_FILES = [
"manifest.json",
"styles.css",
"main.js",
"data.json",
];

class ConfirmPushModal extends Modal {
proceed: (res: boolean) => void;

Expand Down Expand Up @@ -416,47 +403,7 @@ export const push = async (t: ObsidianGoogleDrive) => {
);
}

const configFilesToSync: string[] = [];

const [configFiles, plugins] = await Promise.all([
adapter.list(vault.configDir),
adapter.list(vault.configDir + "/plugins"),
]);

await Promise.all(
configFiles.files
.filter(
(path) =>
!BLACKLISTED_CONFIG_FILES.includes(fileNameFromPath(path))
)
.map(async (path) => {
const file = await adapter.stat(path);
if ((file?.mtime || 0) > t.settings.lastSyncedAt) {
configFilesToSync.push(path);
}
})
.concat(
plugins.folders.map(async (plugin) => {
const files = await adapter.list(plugin);
await Promise.all(
files.files
.filter((path) =>
WHITELISTED_PLUGIN_FILES.includes(
fileNameFromPath(path)
)
)
.map(async (path) => {
const file = await adapter.stat(path);
if (
(file?.mtime || 0) > t.settings.lastSyncedAt
) {
configFilesToSync.push(path);
}
})
);
})
)
);
const configFilesToSync = await t.drive.getConfigFilesToSync();

const foldersToCreate = new Set<string>();
configFilesToSync.forEach((path) => {
Expand Down Expand Up @@ -536,7 +483,7 @@ export const push = async (t: ObsidianGoogleDrive) => {

t.settings.operations = {};

await t.endSync(syncNotice);
await t.endSync(syncNotice, false);

new Notice("Sync complete!");
};
37 changes: 22 additions & 15 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkConnection, folderMimeType, getDriveClient } from "helpers/drive";
import { checkConnection, getDriveClient } from "helpers/drive";
import { refreshAccessToken } from "helpers/ky";
import { pull } from "helpers/pull";
import { push } from "helpers/push";
Expand Down Expand Up @@ -96,17 +96,7 @@ export default class ObsidianGoogleDrive extends Plugin {
this.syncing = true;
this.ribbonIcon.addClass("spin");
await pull(this, true);
this.settings.lastSyncedAt = Date.now();
const changesToken = await this.drive.getChangesStartToken();
if (!changesToken) {
return new Notice(
"An error occurred fetching Google Drive changes token."
);
}
this.settings.changesToken = changesToken;
await this.saveSettings();
this.ribbonIcon.removeClass("spin");
this.syncing = false;
await this.endSync();
}
});
}
Expand Down Expand Up @@ -251,8 +241,25 @@ export default class ObsidianGoogleDrive extends Plugin {
return new Notice("Syncing (0%)", 0);
}

async endSync(syncNotice: Notice) {
this.settings.lastSyncedAt = Date.now();
async endSync(syncNotice?: Notice, retainConfigChanges = true) {
if (retainConfigChanges) {
const configFilesToSync = await this.drive.getConfigFilesToSync();

this.settings.lastSyncedAt = Date.now();

await Promise.all(
configFilesToSync.map(async (file) =>
this.app.vault.adapter.writeBinary(
file,
await this.app.vault.adapter.readBinary(file),
{ mtime: Date.now() }
)
)
);
} else {
this.settings.lastSyncedAt = Date.now();
}

const changesToken = await this.drive.getChangesStartToken();
if (!changesToken) {
return new Notice(
Expand All @@ -263,7 +270,7 @@ export default class ObsidianGoogleDrive extends Plugin {
await this.saveSettings();
this.ribbonIcon.removeClass("spin");
this.syncing = false;
syncNotice.hide();
syncNotice?.hide();
}
}

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "google-drive-sync",
"name": "Google Drive Sync",
"version": "2.2.1",
"version": "2.2.2",
"minAppVersion": "1.6.0",
"description": "Syncs a vault into Google Drive for cross-platform use (works for iOS).",
"author": "Richard Xiong",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-drive-sync",
"version": "2.2.1",
"version": "2.2.2",
"description": "This plugin allows for the use of Google Drive in syncing an Obsidian vault.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 0515cc1

Please sign in to comment.