Lint files

This commit is contained in:
Andras Schmelczer 2024-12-20 16:14:46 +00:00
parent 2f7cad602a
commit ff5af8aea5
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
11 changed files with 184 additions and 276 deletions

View file

@ -1,22 +1,22 @@
import { RelativePath } from "src/database/document-metadata";
import type { RelativePath } from "src/database/document-metadata";
export interface FileOperations {
listAllFiles(): Promise<RelativePath[]>;
listAllFiles: () => Promise<RelativePath[]>;
read(path: RelativePath): Promise<Uint8Array>;
read: (path: RelativePath) => Promise<Uint8Array>;
getModificationTime(path: RelativePath): Promise<Date>;
getModificationTime: (path: RelativePath) => Promise<Date>;
create(path: RelativePath, newContent: Uint8Array): Promise<void>;
create: (path: RelativePath, newContent: Uint8Array) => Promise<void>;
// Writes new content to the file at the given path. If the file's content has changed since the expectedContent was read, the write will merge the changes.
write(
write: (
path: RelativePath,
expectedContent: Uint8Array,
newContent: Uint8Array
): Promise<Uint8Array>;
) => Promise<Uint8Array>;
remove(path: RelativePath): Promise<void>;
remove: (path: RelativePath) => Promise<void>;
move(oldPath: RelativePath, newPath: RelativePath): Promise<void>;
move: (oldPath: RelativePath, newPath: RelativePath) => Promise<void>;
}

View file

@ -1,30 +1,33 @@
import { normalizePath, Vault } from "obsidian";
import { FileOperations } from "./file-operations";
import type { Vault } from "obsidian";
import { normalizePath } from "obsidian";
import type { FileOperations } from "./file-operations";
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
import { isEqualBytes } from "src/utils/is-equal-bytes";
import { RelativePath } from "src/database/document-metadata";
import type { RelativePath } from "src/database/document-metadata";
export class ObsidianFileOperations implements FileOperations {
public constructor(private vault: Vault) {}
public constructor(private readonly vault: Vault) {}
async listAllFiles(): Promise<RelativePath[]> {
public async listAllFiles(): Promise<RelativePath[]> {
const files = this.vault.getFiles();
return files.map((file) => file.path);
}
async read(path: RelativePath): Promise<Uint8Array> {
public async read(path: RelativePath): Promise<Uint8Array> {
return new Uint8Array(
await this.vault.adapter.readBinary(normalizePath(path))
);
}
async getModificationTime(path: RelativePath): Promise<Date> {
return new Date(
(await this.vault.adapter.stat(normalizePath(path)))!.mtime
);
public async getModificationTime(path: RelativePath): Promise<Date> {
const file = await this.vault.adapter.stat(normalizePath(path));
if (!file) {
throw new Error(`File not found: ${path}`);
}
return new Date(file.mtime);
}
async write(
public async write(
path: RelativePath,
expectedContent: Uint8Array,
newContent: Uint8Array
@ -44,17 +47,16 @@ export class ObsidianFileOperations implements FileOperations {
await this.vault.adapter.writeBinary(normalizePath(path), result);
return result;
} else {
await this.vault.adapter.writeBinary(
normalizePath(path),
newContent
);
return newContent;
}
await this.vault.adapter.writeBinary(normalizePath(path), newContent);
return newContent;
}
async create(path: RelativePath, newContent: Uint8Array): Promise<void> {
public async create(
path: RelativePath,
newContent: Uint8Array
): Promise<void> {
if (await this.vault.adapter.exists(normalizePath(path))) {
await this.write(path, new Uint8Array(0), newContent);
return;
@ -63,18 +65,21 @@ export class ObsidianFileOperations implements FileOperations {
await this.vault.adapter.writeBinary(normalizePath(path), newContent);
}
async remove(path: RelativePath): Promise<void> {
public async remove(path: RelativePath): Promise<void> {
if (await this.vault.adapter.exists(normalizePath(path))) {
return this.vault.adapter.remove(normalizePath(path));
}
}
async move(oldPath: RelativePath, newPath: RelativePath): Promise<void> {
public async move(
oldPath: RelativePath,
newPath: RelativePath
): Promise<void> {
if (oldPath === newPath) {
return;
}
this.vault.adapter.rename(
await this.vault.adapter.rename(
normalizePath(oldPath),
normalizePath(newPath)
);