This repository was archived by the owner on May 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.ts
More file actions
178 lines (154 loc) · 4.32 KB
/
Copy pathlib.ts
File metadata and controls
178 lines (154 loc) · 4.32 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { encodeHex } from "@std/encoding/hex";
import shlex from "shlex";
import { createEmphasize } from "emphasize";
import json from "highlight.js/lib/languages/json";
import markdown from "highlight.js/lib/languages/markdown";
import typescript from "highlight.js/lib/languages/typescript";
import yaml from "highlight.js/lib/languages/yaml";
export function getValTownApiKey() {
const token = Deno.env.get("VAL_TOWN_API_KEY") ||
Deno.env.get("VALTOWN_TOKEN") || Deno.env.get("valtown");
if (!token) {
throw new Error("VAL_TOWN_API_KEY is required");
}
return token;
}
export async function fetchValTown(
path: string,
options?: RequestInit & {
paginate?: boolean;
},
): Promise<Response> {
const apiURL = Deno.env.get("VALTOWN_API_URL") || "https://api.val.town";
const headers = {
...options?.headers,
Authorization: `Bearer ${getValTownApiKey()}`,
};
if (options?.paginate) {
const data = [];
let url = new URL(`${apiURL}${path}`);
url.searchParams.set("limit", "100");
while (true) {
const resp = await fetch(url, {
headers,
});
if (!resp.ok) {
throw new Error(await resp.text());
}
const res = await resp.json();
data.push(...res.data);
if (!res.links.next) {
break;
}
url = new URL(res.links.next);
}
return new Response(JSON.stringify(data), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}
return await fetch(`${apiURL}${path}`, {
...options,
headers,
});
}
async function hash(msg: string) {
const data = new TextEncoder().encode(msg);
const hashBuffer = await crypto.subtle.digest("SHA-1", data);
return encodeHex(hashBuffer);
}
export async function loadUser() {
const userHash = await hash(getValTownApiKey());
const item = localStorage.getItem(userHash);
if (item) {
return JSON.parse(item);
}
const resp = await fetchValTown("/v1/me");
if (!resp.ok) {
throw new Error(await resp.text());
}
const user = await resp.json();
await localStorage.setItem(userHash, JSON.stringify(user));
return user;
}
export async function parseVal(val: string) {
if (val.startsWith("@")) {
val = val.slice(1);
}
const parts = val.split(/[.\/]/);
if (parts.length == 1) {
const user = await loadUser();
return {
author: user.username,
name: val,
};
} else if (parts.length == 2) {
return {
author: parts[0],
name: parts[1],
};
}
throw new Error("invalid val");
}
export async function editText(text: string, extension: string) {
const tempfile = await Deno.makeTempFile({
suffix: `.${extension}`,
});
await Deno.writeTextFile(tempfile, text);
const editor = Deno.env.get("EDITOR") || "vim";
const [name, ...args] = [...shlex.split(editor), tempfile];
const command = new Deno.Command(name, {
args,
stdin: "inherit",
stderr: "inherit",
stdout: "inherit",
});
const { code } = await command.output();
if (code !== 0) {
console.error(`editor exited with code ${code}`);
Deno.exit(1);
}
return Deno.readTextFile(tempfile);
}
export function printYaml(value: string) {
if (Deno.stdout.isTerminal() || Deno.env.get("FORCE_COLOR")) {
const emphasize = createEmphasize();
emphasize.register({ yaml });
console.log(emphasize.highlight("yaml", value).value);
} else {
console.log(value);
}
}
export function printTypescript(value: string) {
if (Deno.stdout.isTerminal() || Deno.env.get("FORCE_COLOR")) {
const emphasize = createEmphasize();
emphasize.register({ typescript });
console.log(emphasize.highlight("typescript", value).value);
} else {
console.log(value);
}
}
export function printMarkdown(value: string) {
if (Deno.stdout.isTerminal() || Deno.env.get("FORCE_COLOR")) {
const emphasize = createEmphasize();
emphasize.register({ markdown });
console.log(emphasize.highlight("markdown", value).value);
} else {
console.log(value);
}
}
export function printJson(obj: unknown) {
if (Deno.stdout.isTerminal() || Deno.env.get("FORCE_COLOR")) {
const emphasize = createEmphasize();
emphasize.register({
json,
});
console.log(
emphasize.highlight("json", JSON.stringify(obj, null, 2)).value,
);
} else {
console.log(JSON.stringify(obj));
}
}