-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
deno-doc-reader.ts
78 lines (63 loc) · 1.81 KB
/
deno-doc-reader.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
import { DenoDoc } from "./deno-doc.model.ts";
const decoder = new TextDecoder();
const GlobalFilesSet = new Set();
// TODO Implement this with Deno,doc
// issue https://github.com/denoland/deno/issues/4531
export async function getDenoDoc(
path?: string,
): Promise<DenoDoc.RootDef[] | any> {
if (GlobalFilesSet.has(path)) return undefined;
GlobalFilesSet.add(path);
const option = {
cmd: [
Deno.execPath(),
"doc",
"--json",
"--reload",
],
stdout: "piped",
stderr: "piped",
};
if (path) {
option.cmd.push(path);
}
const process: Deno.Process<Deno.RunOptions> = Deno.run(option as any);
let killed = false;
// // Zeit timeout is 60 seconds for pro tier: https://zeit.co/docs/v2/platform/limits
// const timer = setTimeout(() => {
// killed = true;
// pids.delete(process.pid);
// console.log(process.pid)
//
// // process.kill(process.pid);
// // process.kill(Deno.Signal.SIGINT);
// // process.kill("2");
// process.close();
// (process.stdout as any)?.close();
// }, 4000);
const [out, errOut] = await Promise.all([
process.output(),
process.stderrOutput(),
]);
const status = await process.status();
// clearTimeout(timer);
process.close();
if (!status.success) {
if (killed) throw new Error("Parse timed out");
throw new Error(decoder.decode(errOut));
}
const result = JSON.parse(decoder.decode(out));
for (let i = 0; i < result.length; i++) {
const object = result[i];
if (object.kind === "import" && !GlobalFilesSet.has(object.importDef.src)) {
const src: string = object.importDef.src;
// skip declare
if (src.startsWith("http")) {
GlobalFilesSet.add(src);
} else {
object.importDef.def = await getDenoDoc(src);
}
}
}
return result;
}