-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
dlink.ts
executable file
·297 lines (281 loc) · 8.12 KB
/
dlink.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env deno run --allow-write --allow-read --allow-net
import * as path from "./vendor/https/deno.land/std/path/mod.ts";
import { exists } from "./vendor/https/deno.land/std/fs/exists.ts";
import * as flags from "./vendor/https/deno.land/std/flags/mod.ts";
import { sprintf } from "./vendor/https/deno.land/std/fmt/printf.ts";
import { gray, green, red } from "./vendor/https/deno.land/std/fmt/colors.ts";
import * as util from "./_util.ts";
const VERSION = "0.9.0";
export type Module = {
version: string;
modules: string[];
types?: {
[key: string]: string;
};
};
export type Modules = {
[key: string]: Module;
};
function isDlinkModules(x: any, errors: string[]): x is Modules {
if (typeof x !== "object") {
errors.push("is not object");
return false;
}
for (const k in x) {
if (x.hasOwnProperty(k)) {
const m = x[k];
if (typeof m["version"] !== "string") {
errors.push('"version" must be string');
return false;
}
if (!Array.isArray(m["modules"])) {
errors.push(`\"modules\" must be array`);
return false;
}
for (const mod of m["modules"]) {
if (typeof mod !== "string") {
errors.push(`"content of "modules" must be string`);
return false;
}
}
}
}
return true;
}
async function deleteRemovedFiles(modules: Modules, lockFile: Modules) {
const removedFiles: string[] = [];
for (const [k, v] of Object.entries(lockFile)) {
const url = new URL(k);
const { protocol, hostname, pathname } = url;
const scheme = protocol.slice(0, protocol.length - 1);
const dir = path.join("./vendor", scheme, hostname, pathname);
if (!modules[k]) {
for (const i of v.modules) {
removedFiles.push(path.join(dir, i));
}
} else {
const mod = modules[k];
for (const i of v.modules) {
if (!mod.modules.includes(i)) {
removedFiles.push(path.join(dir, i));
}
}
}
}
const removeFileDirs: string[] = [];
// Remove unlinked files
for (const file of removedFiles) {
const dir = path.dirname(file);
if (await exists(file)) {
await Deno.remove(file);
console.log(`${red("Removed")}: ./${file}`);
removeFileDirs.push(dir);
}
}
// Clean up empty dirs
let dir: string | undefined;
while ((dir = removeFileDirs.pop()) && dir !== "vendor") {
let fileCount = 0;
for await (const _ of Deno.readDir(dir)) {
fileCount++;
}
if (fileCount === 0) {
await Deno.remove(dir);
const parentDir = path.dirname(dir);
removeFileDirs.push(parentDir);
}
}
}
const encoder = new TextEncoder();
async function ensure(modules: Modules, opts: DlinkOptions) {
const lockFile = await readLockFile();
if (lockFile) {
await deleteRemovedFiles(modules, lockFile);
}
for (const [host, module] of Object.entries(modules)) {
await writeLinkFiles({ host, module, lockFile, opts });
await generateLockFile(modules);
}
}
async function writeLinkFiles({
host,
module,
lockFile,
opts,
}: {
host: string;
module: Module;
lockFile?: Modules;
opts: DlinkOptions;
}): Promise<void> {
const { version } = module;
const types = module.types || {};
const func = async (mod: string) => {
const url = new URL(host);
const { protocol, hostname, pathname } = url;
const scheme = protocol.slice(0, protocol.length - 1);
const dir = path.join("./vendor", scheme, hostname, pathname);
const modFile = path.join(dir, mod);
const modDir = path.dirname(modFile);
let lockedVersion: string | undefined;
const typeFile = types[mod];
let lockedTypeFile: string | undefined;
if (lockFile && lockFile[host]) {
lockedVersion = lockFile[host].version;
const lockedTypes = lockFile[host].types;
if (lockedTypes) {
lockedTypeFile = lockedTypes[mod];
}
}
const specifier = `${host}${version}${mod}`;
const hasLink = await exists(modFile);
if (
!opts.reload &&
hasLink &&
version === lockedVersion &&
typeFile === lockedTypeFile
) {
console.log(gray(`Linked: ${specifier} -> ./${modFile}`));
return;
}
const resp = await fetch(specifier, { method: "GET" });
if (resp.status !== 200) {
throw new Error(`failed to fetch metadata for ${specifier}`);
}
const contentLength = parseInt(resp.headers.get("content-length") || "0");
if (contentLength > 10000000) {
throw new Error(`too big source file: ${contentLength}bytes`);
}
const code = await resp.text();
const hasDefaultExport = util.hasDefaultExport(code);
let typeDefinition: string | undefined;
if (typeFile) {
if (typeFile.match(/^(file|https?):\/\//) || typeFile.startsWith("/")) {
// URL or absolute path
typeDefinition = `// @deno-types="${typeFile}"\n`;
} else {
// Relative
const v = path.relative(modDir, typeFile);
typeDefinition = `// @deno-types="${v}"\n`;
}
}
let link = "";
if (typeDefinition) {
link += typeDefinition;
}
link += sprintf('export * from "%s";\n', resp.url);
if (hasDefaultExport) {
if (typeDefinition) {
link += typeDefinition;
}
link += sprintf('import {default as dew} from "%s";\n', resp.url);
link += "export default dew;\n";
}
await Deno.mkdir(modDir, { recursive: true });
const f = await Deno.open(modFile, {
create: true,
write: true,
truncate: true,
});
try {
await Deno.write(f.rid, encoder.encode(link));
} finally {
f.close();
}
console.log(`${green("Linked")}: ${specifier} -> ./${modFile}`);
};
await Promise.all(module.modules.map(func));
}
async function generateSkeletonFile() {
// Currently no way to fetch latest std release
// because core and std release cycles were separated
// const resp = await fetch("https://api.github.com/repos/denoland/deno/tags");
// const json = await resp.json();
// const [latest] = json;
const bin = encoder.encode(
JSON.stringify(
{
"https://deno.land/std": {
version: `@0.100.0`,
modules: ["/testing/asserts.ts"],
},
},
null,
" ",
),
);
await Deno.writeFile("./modules.json", bin);
}
async function generateLockFile(modules: Modules) {
const obj = new TextEncoder().encode(JSON.stringify(modules, null, " "));
await Deno.writeFile("./modules-lock.json", obj);
}
async function readLockFile(): Promise<Modules | undefined> {
if (await exists("./modules-lock.json")) {
const f = await Deno.readFile("./modules-lock.json");
const lock = JSON.parse(new TextDecoder().decode(f));
const err = Array<string>();
if (!isDlinkModules(lock, err)) {
throw new Error(
"lock file may be saved as invalid format: " + err.join(","),
);
}
return lock;
}
}
type DlinkOptions = {
file: string;
reload: boolean;
};
async function main() {
const args = flags.parse(Deno.args, {
alias: {
h: "help",
V: "ver",
R: "reload",
},
"--": true,
});
if (args["V"] || args["ver"]) {
console.log(VERSION);
Deno.exit(0);
}
if (args["h"] || args["help"]) {
console.log(
String(`
USAGE
Dlink (just type ${green("Dlink")})
ARGUMENTS
OPTIONS
-f, --file Custom path for module.json (Optional)
GLOBAL OPTIONS
-h, --help Display help
-V, --ver Display version
`),
);
Deno.exit(0);
}
let reload = !!(args["R"] || args["reload"]);
const opts: DlinkOptions = {
file: "./modules.json",
reload,
};
if (args["f"]) {
opts.file = args["f"];
}
if (!(await exists(opts.file))) {
console.log("./modules.json not found. Creating skeleton...");
await generateSkeletonFile();
}
const file = await Deno.readFile(opts.file);
const decoder = new TextDecoder();
const json = JSON.parse(decoder.decode(file)) as Modules;
const errors = Array<string>();
if (!isDlinkModules(json, errors)) {
console.error(`${opts.file} has syntax error: ${errors.join(",")}`);
Deno.exit(1);
}
await ensure(json, opts);
Deno.exit(0);
}
main();