2022-04-06 13:39:20 +00:00
|
|
|
import { mkdir, readdir, readFile, stat, unlink, utimes, writeFile } from "fs/promises";
|
2022-03-20 08:56:28 +00:00
|
|
|
import * as path from "path";
|
2022-04-06 13:39:20 +00:00
|
|
|
import { PageMeta } from "../common/types";
|
|
|
|
import { EventHook } from "../plugos/hooks/event";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
export interface Storage {
|
|
|
|
listPages(): Promise<PageMeta[]>;
|
|
|
|
readPage(pageName: string): Promise<{ text: string; meta: PageMeta }>;
|
2022-04-06 13:39:20 +00:00
|
|
|
writePage(
|
|
|
|
pageName: string,
|
|
|
|
text: string,
|
|
|
|
lastModified?: number
|
|
|
|
): Promise<PageMeta>;
|
2022-03-31 12:28:07 +00:00
|
|
|
getPageMeta(pageName: string): Promise<PageMeta>;
|
|
|
|
deletePage(pageName: string): Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EventedStorage implements Storage {
|
|
|
|
constructor(private wrapped: Storage, private eventHook: EventHook) {}
|
|
|
|
|
|
|
|
listPages(): Promise<PageMeta[]> {
|
|
|
|
return this.wrapped.listPages();
|
|
|
|
}
|
|
|
|
|
|
|
|
readPage(pageName: string): Promise<{ text: string; meta: PageMeta }> {
|
|
|
|
return this.wrapped.readPage(pageName);
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
async writePage(
|
|
|
|
pageName: string,
|
|
|
|
text: string,
|
|
|
|
lastModified?: number
|
|
|
|
): Promise<PageMeta> {
|
|
|
|
const newPageMeta = this.wrapped.writePage(pageName, text, lastModified);
|
2022-03-31 12:28:07 +00:00
|
|
|
// This can happen async
|
2022-04-04 09:51:41 +00:00
|
|
|
this.eventHook
|
|
|
|
.dispatchEvent("page:saved", pageName)
|
|
|
|
.then(() => {
|
|
|
|
return this.eventHook.dispatchEvent("page:index", {
|
|
|
|
name: pageName,
|
|
|
|
text: text,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error("Error dispatching page:saved event", e);
|
2022-03-31 12:28:07 +00:00
|
|
|
});
|
|
|
|
return newPageMeta;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPageMeta(pageName: string): Promise<PageMeta> {
|
|
|
|
return this.wrapped.getPageMeta(pageName);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deletePage(pageName: string): Promise<void> {
|
|
|
|
await this.eventHook.dispatchEvent("page:deleted", pageName);
|
|
|
|
return this.wrapped.deletePage(pageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DiskStorage implements Storage {
|
2022-03-20 08:56:28 +00:00
|
|
|
rootPath: string;
|
2022-04-06 13:39:20 +00:00
|
|
|
plugPrefix: string;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
constructor(rootPath: string, plugPrefix: string = "_plug/") {
|
2022-03-20 08:56:28 +00:00
|
|
|
this.rootPath = rootPath;
|
2022-04-06 13:39:20 +00:00
|
|
|
this.plugPrefix = plugPrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
pageNameToPath(pageName: string) {
|
|
|
|
if (pageName.startsWith(this.plugPrefix)) {
|
|
|
|
return path.join(this.rootPath, pageName + ".plug.json");
|
|
|
|
}
|
|
|
|
return path.join(this.rootPath, pageName + ".md");
|
|
|
|
}
|
|
|
|
|
|
|
|
pathToPageName(fullPath: string): string {
|
|
|
|
let extLength = fullPath.endsWith(".plug.json")
|
|
|
|
? ".plug.json".length
|
|
|
|
: ".md".length;
|
|
|
|
return fullPath.substring(
|
|
|
|
this.rootPath.length + 1,
|
|
|
|
fullPath.length - extLength
|
|
|
|
);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async listPages(): Promise<PageMeta[]> {
|
|
|
|
let fileNames: PageMeta[] = [];
|
|
|
|
|
|
|
|
const walkPath = async (dir: string) => {
|
|
|
|
let files = await readdir(dir);
|
|
|
|
for (let file of files) {
|
|
|
|
const fullPath = path.join(dir, file);
|
|
|
|
let s = await stat(fullPath);
|
2022-04-06 13:39:20 +00:00
|
|
|
// console.log("Encountering", fullPath, s);
|
2022-03-20 08:56:28 +00:00
|
|
|
if (s.isDirectory()) {
|
|
|
|
await walkPath(fullPath);
|
|
|
|
} else {
|
2022-04-06 13:39:20 +00:00
|
|
|
if (file.endsWith(".md") || file.endsWith(".json")) {
|
2022-03-20 08:56:28 +00:00
|
|
|
fileNames.push({
|
2022-04-06 13:39:20 +00:00
|
|
|
name: this.pathToPageName(fullPath),
|
2022-03-20 08:56:28 +00:00
|
|
|
lastModified: s.mtime.getTime(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await walkPath(this.rootPath);
|
|
|
|
return fileNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
async readPage(pageName: string): Promise<{ text: string; meta: PageMeta }> {
|
2022-04-06 13:39:20 +00:00
|
|
|
const localPath = this.pageNameToPath(pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
try {
|
|
|
|
const s = await stat(localPath);
|
|
|
|
return {
|
|
|
|
text: await readFile(localPath, "utf8"),
|
|
|
|
meta: {
|
|
|
|
name: pageName,
|
|
|
|
lastModified: s.mtime.getTime(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (e) {
|
2022-03-23 14:41:12 +00:00
|
|
|
// console.error("Error while reading page", pageName, e);
|
2022-03-20 08:56:28 +00:00
|
|
|
throw Error(`Could not read page ${pageName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
async writePage(
|
|
|
|
pageName: string,
|
|
|
|
text: string,
|
|
|
|
lastModified?: number
|
|
|
|
): Promise<PageMeta> {
|
|
|
|
let localPath = this.pageNameToPath(pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
try {
|
2022-03-23 14:41:12 +00:00
|
|
|
// Ensure parent folder exists
|
|
|
|
await mkdir(path.dirname(localPath), { recursive: true });
|
|
|
|
|
|
|
|
// Actually write the file
|
2022-03-20 08:56:28 +00:00
|
|
|
await writeFile(localPath, text);
|
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
if (lastModified) {
|
|
|
|
let d = new Date(lastModified);
|
|
|
|
console.log("Going to set the modified time", d);
|
2022-04-07 12:04:50 +00:00
|
|
|
await utimes(localPath, d, d);
|
2022-04-06 13:39:20 +00:00
|
|
|
}
|
2022-03-23 14:41:12 +00:00
|
|
|
// Fetch new metadata
|
2022-03-20 08:56:28 +00:00
|
|
|
const s = await stat(localPath);
|
|
|
|
return {
|
|
|
|
name: pageName,
|
|
|
|
lastModified: s.mtime.getTime(),
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error while writing page", pageName, e);
|
|
|
|
throw Error(`Could not write ${pageName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPageMeta(pageName: string): Promise<PageMeta> {
|
2022-04-06 13:39:20 +00:00
|
|
|
let localPath = this.pageNameToPath(pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
try {
|
|
|
|
const s = await stat(localPath);
|
|
|
|
return {
|
|
|
|
name: pageName,
|
|
|
|
lastModified: s.mtime.getTime(),
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error while getting page meta", pageName, e);
|
|
|
|
throw Error(`Could not get meta for ${pageName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
async deletePage(pageName: string): Promise<void> {
|
2022-04-06 13:39:20 +00:00
|
|
|
let localPath = this.pageNameToPath(pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
await unlink(localPath);
|
|
|
|
}
|
|
|
|
}
|