Skip to content

Commit

Permalink
fix: initialization procedure (fix #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Mar 14, 2021
1 parent 619c5d1 commit 1d71418
Showing 1 changed file with 72 additions and 38 deletions.
110 changes: 72 additions & 38 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawnSync } from "child_process";
import { FileSystemAdapter, Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
import simpleGit, { CheckRepoActions, FileStatusResult, SimpleGit } from "simple-git";

Expand Down Expand Up @@ -35,6 +36,7 @@ export default class ObsidianGit extends Plugin {
state: PluginState = PluginState.idle;
intervalID: number;
lastUpdate: number;
gitReady = false;

setState(state: PluginState) {
this.state = state;
Expand All @@ -44,42 +46,6 @@ export default class ObsidianGit extends Plugin {
console.log('loading ' + this.manifest.name + " plugin");
await this.loadSettings();

let statusBarEl = this.addStatusBarItem();
this.statusBar = new StatusBar(statusBarEl, this);
this.setState(PluginState.idle);
this.registerInterval(
window.setInterval(() => this.statusBar.display(), 1000)
);
let path: string;
const adapter = this.app.vault.adapter;
if (adapter instanceof FileSystemAdapter) {
path = adapter.getBasePath();
}

this.git = simpleGit(path);

const isValidRepo = await this.git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);

if (!isValidRepo) {
this.displayMessage("Valid git repository not found.", 0);
return;
}

if (this.settings.autoPullOnBoot) {
const filesUpdated = await this.pull();

this.setState(PluginState.idle);
const message =
filesUpdated > 0
? `Pulled new changes. ${filesUpdated} files updated`
: "Everything up-to-date";
this.displayMessage(message);
}

if (this.settings.autoSaveInterval > 0) {
this.enableAutoBackup();
}

this.addSettingTab(new ObsidianGitSettingsTab(this.app, this));

this.addCommand({
Expand All @@ -93,6 +59,15 @@ export default class ObsidianGit extends Plugin {
name: "Commit *all* changes and push to remote repository",
callback: () => this.createBackup()
});
this.init();
// init statusBar
let statusBarEl = this.addStatusBarItem();
this.statusBar = new StatusBar(statusBarEl, this);
this.setState(PluginState.idle);
this.registerInterval(
window.setInterval(() => this.statusBar.display(), 1000)
);

}
async onunload() {
console.log('unloading ' + this.manifest.name + " plugin");
Expand All @@ -104,7 +79,46 @@ export default class ObsidianGit extends Plugin {
await this.saveData(this.settings);
}

async pullChangesFromRemote() {
async init(): Promise<void> {
if (!this.isGitInstalled()) {
this.displayError("Cannot run git command");
return;
}
try {
const adapter = this.app.vault.adapter as FileSystemAdapter;
const path = adapter.getBasePath();

this.git = simpleGit(path);

const isValidRepo = await this.git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);

if (!isValidRepo) {
this.displayError("Valid git repository not found.");
} else {
this.gitReady = true;

if (this.settings.autoPullOnBoot) {
this.pullChangesFromRemote();
}

if (this.settings.autoSaveInterval > 0) {
this.enableAutoBackup();
}
}

} catch (error) {
this.displayError(error);
console.error(error);
}
}

async pullChangesFromRemote(): Promise<void> {

if (!this.gitReady) {
await this.init();
}

if (!this.gitReady) return;
await this.pull().then((filesUpdated) => {
if (filesUpdated > 0) {
this.displayMessage(
Expand All @@ -119,7 +133,13 @@ export default class ObsidianGit extends Plugin {
this.setState(PluginState.idle);
}

async createBackup() {
async createBackup(): Promise<void> {

if (!this.gitReady) {
await this.init();
}
if (!this.gitReady) return;

this.setState(PluginState.status);
const status = await this.git.status();

Expand Down Expand Up @@ -168,6 +188,20 @@ export default class ObsidianGit extends Plugin {


// region: main methods

isGitInstalled(): boolean {
// https://github.com/steveukx/git-js/issues/402
const command = spawnSync('git', ['--version'], {
stdio: 'ignore'
});

if (command.error) {
console.error(command.error);
return false;
}
return true;
}

async add(): Promise<void> {
this.setState(PluginState.add);
await this.git.add(
Expand Down

0 comments on commit 1d71418

Please sign in to comment.