-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendor.js
51 lines (44 loc) · 1018 Bytes
/
vendor.js
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
/**
* @import {S4} from "./shared.js"
*/
import {mkdir, readFile, writeFile} from "node:fs/promises"
import * as path from "node:path"
import {URL} from "node:url"
/**
* @param {string} u
* @returns {Promise<Response>}
*/
export async function fetch(u) {
u = u.replace("/blob/", "/raw/")
return await global.fetch(u, {redirect: "follow"})
}
/**
* @param {string} u
* @param {string} c
* @returns {Promise<void>}
*/
export async function write(u, c) {
let [o, p, r, f] = stat(u)
let d = path.join("vendor", o, p, r)
await mkdir(d, {recursive: true})
f = path.join(d, f)
await writeFile(f, c)
}
/**
* @param {string} u
* @returns {Promise<string>}
*/
export async function read(u) {
let [o, p, r, f] = stat(u)
f = path.join("vendor", o, p, r, f)
return await readFile(f, "utf8")
}
/**
* @param {string} u
* @returns {S4}
*/
function stat(u) {
let o = new URL(".", u)
let a = o.pathname.split("/")
return [a[1], a[2], a.slice(4, a.length - 2).join("/"), a[a.length - 2]]
}