deno.land / x / trex@v1.13.1 / utils / storage.ts

نووسراو ببینە
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/** * Copyright (c) Crew Dev. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */
import { exists, readJson, writeJson } from "tools-fs";import type { DefaultStore } from "./types.ts";import { crypto } from "crypto/mod.ts";import { join } from "path/mod.ts";
const { env, remove, mkdir, build } = Deno;const commonDir = "trex_storage";
/** * detect if is runnig on gh action workflow */export const isGH = !!env.get("GITHUB_ACTIONS")! || !!env.get("GITHUB_WORKFLOW")! || !!env.get("GITHUB_JOB")!;
/** * github actions fallback storage path */const ghFallBack = join(Deno.cwd(), commonDir);
/** * create a persistent json storage per CWD. */export async function JsonStorage() { const hash = await createHash("SHA-256", Deno.cwd());
const storagePath = isGH ? ghFallBack : build.os === "windows" ? join("C:", "Users", env.get("USERNAME")!, ".deno", `${commonDir}\\`) : join(env.get("HOME")!, ".deno", `${commonDir}/`);
const currentStorage = join(storagePath, `${hash}.json`);
if (!(await exists(storagePath))) await mkdir(storagePath);
if (!(await exists(currentStorage))) { await writeJson(currentStorage, {}, { spaces: 2, create: true }); }
return { async getStorage<T extends any>(): Promise<T> { if (await exists(currentStorage)) { return (await readJson(currentStorage)) as T; }
throw new Error("storage was removed").message; }, async getItem<T extends any>(item: string): Promise<T> { if (await exists(currentStorage)) { const store = (await readJson(currentStorage)) as DefaultStore;
return store[item] as T; }
throw new Error("storage was removed").message; }, async setItem(item: string, value: any) { if (await exists(currentStorage)) { const store = (await readJson(currentStorage)) as DefaultStore; store[item] = value;
return await writeJson(currentStorage, store, { create: false, spaces: 2, }); }
throw new Error("storage was removed").message; }, async has(item: string): Promise<boolean> { if (await exists(currentStorage)) { const store = (await readJson(currentStorage)) as DefaultStore;
return Object.keys(store).includes(item); }
throw new Error("storage was removed").message; }, async dropStorage() { return await remove(currentStorage, { recursive: true }); }, async deleteItem(item: string) { if (await exists(currentStorage)) { const store = (await readJson(currentStorage)) as DefaultStore; delete store[item];
return await writeJson(currentStorage, store, { create: false, spaces: 2, }); }
throw new Error("storage was removed").message; }, };}
export async function createHash(algorithm: "SHA-256", text: string) { const toHexString = (bytes: ArrayBuffer): string => new Uint8Array(bytes).reduce( (str, byte) => str + byte.toString(16).padStart(2, "0"), "", );
return toHexString( await crypto.subtle.digest(algorithm, new TextEncoder().encode(text)), );}
trex

Version Info

Tagged at
9 months ago