This repository was archived by the owner on Aug 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
65 lines (56 loc) · 1.74 KB
/
utils.ts
File metadata and controls
65 lines (56 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { dirname } from "std/path/dirname.ts";
import { DOMAIN_SUFFIX, IS_LOCAL } from "./constants.ts";
export function stripDotSlash(path: string): string {
return path.replace(/^\.\//, "");
}
export function pathToUrl(path: string): string {
const parts = path.split("/");
const serviceName = parts[parts.length - 1];
const domain = `${serviceName}.${DOMAIN_SUFFIX}`;
return IS_LOCAL ? `http://${domain}` : domain;
}
export function safeServiceName(path: string): string {
const parts = path.split("/").filter((part) => part.length > 0);
const relevantParts = parts.slice(-2);
return relevantParts.join("_").toLowerCase();
}
export function isHidden(name: string): boolean {
return name.startsWith(".");
}
function parseContent<T>(content: string, defaultValue: T): T {
if (!content.trim()) return defaultValue;
switch (typeof defaultValue) {
case "number":
return Number(content) as T;
case "string":
case "object":
return JSON.parse(content) as T;
default:
return defaultValue;
}
}
export async function readFileData<T>(
filepath: string,
defaultValue: T,
): Promise<T> {
try {
const content = await Deno.readTextFile(filepath);
return parseContent(content, defaultValue);
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
await writeFileData(filepath, defaultValue);
return defaultValue;
}
throw err;
}
}
export async function writeFileData<T>(
filepath: string,
data: T,
): Promise<void> {
const serialized = typeof data === "number" || typeof data === "string"
? String(data)
: JSON.stringify(data, null, 2);
await Deno.mkdir(dirname(filepath), { recursive: true });
await Deno.writeTextFile(filepath, serialized, { create: true });
}