Skip to content

Commit

Permalink
chore: add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
hiimjako committed Jan 7, 2025
1 parent 4d388f3 commit e17d333
Show file tree
Hide file tree
Showing 30 changed files with 3,848 additions and 3,501 deletions.
3 changes: 0 additions & 3 deletions .eslintignore

This file was deleted.

23 changes: 0 additions & 23 deletions .eslintrc

This file was deleted.

28 changes: 28 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 4,
"lineWidth": 80
},
"javascript": {
"formatter": {
"quoteStyle": "double"
}
},
"files": {
"include": ["main.ts", "src/**/*.ts"],
"ignore": ["node_modules", "dist", "build", "main.js"]
}
}
227 changes: 115 additions & 112 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,122 @@
import { Notice, Plugin } from "obsidian";
import type { App, PluginManifest } from "obsidian";
import { log } from "src/logger/logger";
import { Syncinator as SyncinatorPlugin } from "src/plugin";
import { Disk } from "src/storage/storage";
import { ApiClient } from "./src/api/api";
import { HttpClient } from "./src/api/http";
import { WsClient } from "./src/api/ws";
import {
DEFAULT_SETTINGS,
SettingTab,
type PluginSettings,
DEFAULT_SETTINGS,
type PluginSettings,
SettingTab,
} from "./src/settings";
import { WsClient } from "./src/api/ws";
import { HttpClient } from "./src/api/http";
import { ApiClient } from "./src/api/api";
import { Disk } from "src/storage/storage";
import { Syncinator as SyncinatorPlugin } from "src/plugin";
import { log } from "src/logger/logger";

export default class Syncinator extends Plugin {
settings: PluginSettings = DEFAULT_SETTINGS;
private statusBar: HTMLElement;
private uploadingFiles = 0;
private downloadingFiles = 0;
private wsClient: WsClient;
private storage: Disk;
private apiClient: ApiClient;

constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
this.statusBar = this.addStatusBarItem();
}

async registerPlugin() {
const plugin = new SyncinatorPlugin(
this.storage,
this.apiClient,
this.wsClient,
{
conflictResolution: this.settings.conflictResolution,
},
);

await plugin.init();

this.registerEvent(this.app.vault.on("create", plugin.events.create));
this.registerEvent(this.app.vault.on("modify", plugin.events.modify));
this.registerEvent(this.app.vault.on("delete", plugin.events.delete));
this.registerEvent(this.app.vault.on("rename", plugin.events.rename));
}

private async refreshToken() {
try {
const res = await this.apiClient.login(
this.settings.workspaceName,
this.settings.workspacePass,
);
this.apiClient.setAuthorizationHeader(res.token);
this.wsClient.setAuthorization(res.token);
} catch (error) {
log.error(error);
}
}

async onload() {
// Settings
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this));
log.setGlobalLevel(this.settings.logLevel);

// Init
this.storage = new Disk(this.app.vault);
const httpClient = new HttpClient(
this.settings.useTLS ? "https" : "http",
this.settings.domain,
{},
);
this.apiClient = new ApiClient(httpClient);
this.wsClient = new WsClient(
this.settings.useTLS ? "wss" : "ws",
this.settings.domain,
);

await this.refreshToken();
this.registerInterval(
window.setInterval(async () => await this.refreshToken(), 5 * 60 * 1000),
);

// Deferred startup
setTimeout(async () => {
await this.registerPlugin();
this.updateStatusBar();
new Notice("Obsidian Live Syncinator inizialized");
}, 2000);
}

onunload() {
this.wsClient.close(true);
}

async loadSettings() {
this.settings = Object.assign({}, this.settings, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}

private updateStatusBar() {
this.statusBar.setText(
`sync: ${this.uploadingFiles}${this.downloadingFiles}↓`,
);
}

addUploadingFiles(n: number) {
this.uploadingFiles += n;
this.updateStatusBar();
}

addDownlodingFiles(n: number) {
this.downloadingFiles += n;
this.updateStatusBar();
}
settings: PluginSettings = DEFAULT_SETTINGS;
private statusBar: HTMLElement;
private uploadingFiles = 0;
private downloadingFiles = 0;
private wsClient: WsClient;
private storage: Disk;
private apiClient: ApiClient;

constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
this.statusBar = this.addStatusBarItem();
}

async registerPlugin() {
const plugin = new SyncinatorPlugin(
this.storage,
this.apiClient,
this.wsClient,
{
conflictResolution: this.settings.conflictResolution,
},
);

await plugin.init();

this.registerEvent(this.app.vault.on("create", plugin.events.create));
this.registerEvent(this.app.vault.on("modify", plugin.events.modify));
this.registerEvent(this.app.vault.on("delete", plugin.events.delete));
this.registerEvent(this.app.vault.on("rename", plugin.events.rename));
}

private async refreshToken() {
try {
const res = await this.apiClient.login(
this.settings.workspaceName,
this.settings.workspacePass,
);
this.apiClient.setAuthorizationHeader(res.token);
this.wsClient.setAuthorization(res.token);
} catch (error) {
log.error(error);
}
}

async onload() {
// Settings
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this));
log.setGlobalLevel(this.settings.logLevel);

// Init
this.storage = new Disk(this.app.vault);
const httpClient = new HttpClient(
this.settings.useTLS ? "https" : "http",
this.settings.domain,
{},
);
this.apiClient = new ApiClient(httpClient);
this.wsClient = new WsClient(
this.settings.useTLS ? "wss" : "ws",
this.settings.domain,
);

await this.refreshToken();
this.registerInterval(
window.setInterval(
async () => await this.refreshToken(),
5 * 60 * 1000,
),
);

// Deferred startup
setTimeout(async () => {
await this.registerPlugin();
this.updateStatusBar();
new Notice("Obsidian Live Syncinator inizialized");
}, 2000);
}

onunload() {
this.wsClient.close(true);
}

async loadSettings() {
this.settings = Object.assign({}, this.settings, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}

private updateStatusBar() {
this.statusBar.setText(
`sync: ${this.uploadingFiles}${this.downloadingFiles}↓`,
);
}

addUploadingFiles(n: number) {
this.uploadingFiles += n;
this.updateStatusBar();
}

addDownlodingFiles(n: number) {
this.downloadingFiles += n;
this.updateStatusBar();
}
}
Loading

0 comments on commit e17d333

Please sign in to comment.