This commit is contained in:
Zef Hemel
2023-05-29 09:53:49 +02:00
parent 50651dc185
commit c6a45be4bb
10 changed files with 129 additions and 47 deletions
+2
View File
@@ -124,3 +124,5 @@ export {
} from "https://esm.sh/@codemirror/lang-javascript@6.1.8?external=@codemirror/language,@codemirror/autocomplete,@codemirror/view,@codemirror/state,@codemirror/lint,@lezer/common,@lezer/lr,@lezer/javascript,@codemirror/commands";
export { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
export { compile as gitIgnoreCompiler } from "https://esm.sh/gitignore-parser@0.0.2";
+1 -12
View File
@@ -16,14 +16,10 @@ function normalizeForwardSlashPath(path: string) {
const excludedFiles = ["data.db", "data.db-journal", "sync.json"];
export type DiskSpaceOptions = {
maxFileSizeMB?: number;
};
export class DiskSpacePrimitives implements SpacePrimitives {
rootPath: string;
constructor(rootPath: string, private options: DiskSpaceOptions = {}) {
constructor(rootPath: string) {
this.rootPath = Deno.realPathSync(rootPath);
}
@@ -150,13 +146,6 @@ export class DiskSpacePrimitives implements SpacePrimitives {
const fullPath = file.path;
try {
const s = await Deno.stat(fullPath);
// Don't list file exceeding the maximum file size
if (
this.options.maxFileSizeMB &&
s.size / (1024 * 1024) > this.options.maxFileSizeMB
) {
continue;
}
const name = fullPath.substring(this.rootPath.length + 1);
if (excludedFiles.includes(name)) {
continue;
@@ -0,0 +1,35 @@
import { FileMeta } from "../types.ts";
import { SpacePrimitives } from "./space_primitives.ts";
export class FilteredSpacePrimitives implements SpacePrimitives {
constructor(
private wrapped: SpacePrimitives,
private filterFn: (name: FileMeta) => boolean,
private onFetchList?: () => Promise<void>,
) {
}
async fetchFileList(): Promise<FileMeta[]> {
if (this.onFetchList) {
await this.onFetchList();
}
return (await this.wrapped.fetchFileList()).filter(this.filterFn);
}
readFile(name: string): Promise<{ data: Uint8Array; meta: FileMeta }> {
return this.wrapped.readFile(name);
}
getFileMeta(name: string): Promise<FileMeta> {
return this.wrapped.getFileMeta(name);
}
writeFile(
name: string,
data: Uint8Array,
selfUpdate?: boolean | undefined,
lastModified?: number | undefined,
): Promise<FileMeta> {
return this.wrapped.writeFile(name, data, selfUpdate, lastModified);
}
deleteFile(name: string): Promise<void> {
return this.wrapped.deleteFile(name);
}
}
+7 -1
View File
@@ -36,14 +36,18 @@ export function parseYamlSettings(settingsMarkdown: string): {
export async function ensureSettingsAndIndex(
space: SpacePrimitives,
): Promise<any> {
let settingsText: string | undefined;
try {
await space.getFileMeta("SETTINGS.md");
settingsText = new TextDecoder().decode(
(await space.readFile("SETTINGS.md")).data,
);
} catch {
await space.writeFile(
"SETTINGS.md",
new TextEncoder().encode(SETTINGS_TEMPLATE),
true,
);
settingsText = SETTINGS_TEMPLATE;
// Ok, then let's also write the index page
try {
await space.getFileMeta("index.md");
@@ -60,4 +64,6 @@ Loading some onboarding content for you (but doing so does require a working int
);
}
}
return parseYamlSettings(settingsText);
}