This repository has been archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinjector.ts
172 lines (157 loc) · 4.74 KB
/
injector.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
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
import { createHash } from "https://deno.land/[email protected]/hash/mod.ts";
import { ensureDir } from "https://deno.land/[email protected]/fs/ensure_dir.ts";
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import { exists } from "https://deno.land/[email protected]/fs/exists.ts";
import { resolve } from "https://deno.land/[email protected]/path/posix.ts";
import { parse as parseArgs } from "https://deno.land/[email protected]/flags/mod.ts";
/*
* Deno cache injector
* cache algorithm is referenced from Deno source
* https://github.com/denoland/deno/blob/main/cli/disk_cache.rs
* https://github.com/denoland/deno/blob/main/cli/deno_dir.rs
*
* License is the same as Deno: MIT
* https://github.com/denoland/deno/blob/main/LICENSE.md
*/
type ScriptLocation = {
protocol: string;
host: string;
port?: string;
rest: string;
};
type Option = {
delete: boolean;
useSymlink: boolean;
verbose: boolean;
};
function parseURL(url: string): ScriptLocation {
const parsed = url.match(/(\w+):\/\/([^:\/]+)(?::(\d+))?(.*)/);
if (!parsed) {
throw new Error(`Malformed URL '${url}'`);
}
return {
protocol: parsed[1],
host: parsed[2],
port: parsed[3],
rest: parsed[4],
};
}
export async function getDenoDir() {
await Deno.permissions.request({ name: "env" });
const denoDir = Deno.env.get("DENO_DIR");
if (denoDir) {
return denoDir;
}
switch (Deno.build.os) {
case "linux":
return (Deno.env.get("XDG_CACHE_HOME") ??
Deno.env.get("HOME") + "/.cache") + "/deno";
case "darwin":
return Deno.env.get("HOME") + "/Library/Caches/deno";
}
}
async function process(target: string, prefix: string, option: Option) {
const depsDir = (await getDenoDir()) + "/deps";
await Deno.permissions.request({ name: "read" });
await Deno.permissions.request({ name: "write" });
Deno.chdir(target);
for await (const e of walk(".")) {
if (!e.isFile || !(e.path.endsWith("js") || e.path.endsWith("ts"))) {
continue;
}
const url = prefix + "/" + e.path;
const loc = parseURL(url);
const host = loc.host + (loc.port ? "_PORT" + loc.port : "");
const dir = `${depsDir}/${loc.protocol}/${host}`;
await ensureDir(dir);
const hasher = createHash("sha256");
hasher.update(loc.rest);
const fromPath = resolve(e.path);
const destPath = `${dir}/${hasher}`;
if (await exists(destPath)) {
await Deno.remove(destPath);
if (option.verbose) {
console.log(`delete: ${destPath}`);
}
}
if (option.delete) {
return;
}
if (option.useSymlink) {
await Deno.symlink(fromPath, destPath);
} else {
await Deno.copyFile(e.path, destPath);
}
if (option.verbose) {
console.log(
`${option.useSymlink ? "link" : "copy"}: ${fromPath} => ${destPath}`,
);
}
const mpath = destPath + ".metadata.json";
await Deno.writeTextFile(
mpath,
JSON.stringify({
headers: {},
url,
}),
);
}
}
async function main(args: string[]): Promise<number> {
const parsed = parseArgs(args, {
boolean: ["d", "s", "v"],
});
if (parsed._.length !== 2) {
const scriptName = import.meta.url.replace(/.*\//, "");
console.log("deno cache injector");
console.log(
"Replace JavaScript/TypeScript files in the cache to files in the particular directory",
);
console.log("");
console.log("To use files in <libpath> for <liburl>:");
console.log("");
console.log(
` deno run ${scriptName} ./denops-std-deno https://deno.land/x/[email protected]`,
);
console.log("");
console.log("USAGE:");
console.log(` deno run ${scriptName} <option>... <libpath> <liburl>`);
console.log("");
console.log("OPTIONS:");
console.log(
" -d Delete cache (Only files that exists in <libpath>)",
);
console.log(" -s Use symbolic link");
console.log(" -v Display verbose log");
console.log("");
console.log("ARGUMENTS:");
console.log(
" <libpath> A local directory path which is used to replace the cache",
);
console.log(
" <liburl> A base URL of a library in the cache to replace",
);
console.log("");
console.log("PERMISSIONS:");
console.log(" --allow-env");
console.log(" --allow-read");
console.log(" --allow-write");
console.log("");
return 1;
}
const target = parsed._[0].toString();
const prefix = parsed._[1].toString();
const option = {
delete: parsed.d,
useSymlink: parsed.s,
verbose: parsed.v,
};
await process(target, prefix, option);
console.log(
`All JavaScript/TypeScript files for ${prefix} in the cache are replaced to files in ${target}`,
);
return 0;
}
if (import.meta.main) {
Deno.exit(await main(Deno.args));
}