Skip to content

Commit

Permalink
fix: fix bug from refactor of symlink handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeodyssey committed Nov 24, 2023
1 parent 3f9e57c commit 4b93202
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
51 changes: 30 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {SymlinkHandler} from "./symlink";
export default class HexoIntegrationPlugin extends Plugin {
settings: HexoIntegrationSettings;
gitHandler: GitHandler;
symlinkHandler:SymlinkHandler;

async onload() {

await this.loadSettings();
this.addSettingTab(new HexoIntegrationSettingsTab(this.app, this));
this.addSettingTab(new HexoIntegrationSettingsTab(this.app, this,this.settings,this.symlinkHandler,));
const hexoBlogPath = this.settings.hexoSourcePath;

this.gitHandler = new GitHandler(hexoBlogPath);
Expand All @@ -32,32 +33,40 @@ export default class HexoIntegrationPlugin extends Plugin {
})


this.addSettingTab(new HexoIntegrationSettingsTab(this.app, this));
this.addSettingTab(new HexoIntegrationSettingsTab(this.app, this,this.settings,this.symlinkHandler,));
// Get the Hexo blog path from the plugin settings
// Initialize the SimpleGit instance with the Hexo blog path
// Call the `checkForChanges` function every minute (or any desired interval)
this.setAutoSync();
}

private setAutoSync() {
setInterval(async () => {
const status = await this.gitHandler.checkForChanges();
if (status != null) {
const changedFilesCount = status.created.length + status.modified.length + status.deleted.length;

if (changedFilesCount > 0) {
console.log('Changed files:', status.files);

// Commit and push the changes
try {
await this.gitHandler.commitChanges(status);
await this.gitHandler.pushChanges();
console.log('Changes committed and pushed successfully.');
new Notice('Changes committed and pushed successfully.');

} catch (error) {
console.error('Error during commit and push:', error);
throw new Error(`Error during commit and push:${error.message}`);
}
await this.handleAutoSync();
}, 60 * 1000);
}

private async handleAutoSync() {
const status = await this.gitHandler.checkForChanges();
if (status != null) {
const changedFilesCount = status.created.length + status.modified.length + status.deleted.length;

if (changedFilesCount > 0) {
console.log('Changed files:', status.files);

// Commit and push the changes
try {
await this.gitHandler.commitChanges(status);
await this.gitHandler.pushChanges();
console.log('Changes committed and pushed successfully.');
new Notice('Changes committed and pushed successfully.');

} catch (error) {
console.error('Error during commit and push:', error);
throw new Error(`Error during commit and push:${error.message}`);
}
}
}, 60 * 1000);
}
}

onunload() {
Expand Down
19 changes: 12 additions & 7 deletions src/settings/hexoIntegrationSettingsTab.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import {App, Notice, PluginSettingTab, Setting} from "obsidian";
import Index from "../index";

const path = require('path');
import {SymlinkHandler} from "../symlink";
import {HexoIntegrationSettings} from "../types";
require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {dialog} = require('electron').remote;


export default class HexoIntegrationSettingsTab extends PluginSettingTab {
plugin: Index;
private settings: HexoIntegrationSettings;
private symlinkHandler: SymlinkHandler;
private plugin: Index;

constructor(app: App, plugin: Index) {
super(app, plugin);
this.plugin = plugin;
constructor(app: App,plugin:Index, settings: HexoIntegrationSettings, symlinkHandler: SymlinkHandler) {
super(app,plugin);
this.settings = settings;
this.symlinkHandler = symlinkHandler;
}

display(): void {
Expand Down Expand Up @@ -51,7 +56,7 @@ export default class HexoIntegrationSettingsTab extends PluginSettingTab {
const addingNotice = new Notice("Adding...");

try {
const status = await this.plugin.c;
const status = await this.symlinkHandler.createSystemSpecificSymlink(this.plugin.settings.hexoSourcePath);

addingNotice.hide();

Expand Down

0 comments on commit 4b93202

Please sign in to comment.