-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathvalidate_notebook.ts
85 lines (80 loc) · 2.3 KB
/
validate_notebook.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
import * as fs from "node:fs/promises";
import * as ts from "typescript";
import { v4 as uuidv4 } from "uuid";
export async function extract(filepath: string) {
const cells = JSON.parse((await fs.readFile(filepath)).toString()).cells;
const code = cells
.map((cell: Record<string, any>) => {
if (cell.cell_type === "code") {
return cell.source.join("");
}
return "";
})
.join("\n");
return code;
}
let [pathname, ...args] = process.argv.slice(2);
if (!pathname) {
throw new Error("No pathname provided.");
}
const run = async () => {
if (pathname.startsWith("docs/core_docs/")) {
pathname = "./" + pathname.slice("docs/core_docs/".length);
}
if (!pathname.endsWith(".ipynb")) {
throw new Error("Only .ipynb files are supported.");
}
const filename = pathname
.split("/")
[pathname.split("/").length - 1].replace(".ipynb", ".mts");
const tempFilepath = `./tmp/${filename}`;
try {
const typescriptSource = await extract(pathname);
try {
await fs.access("./tmp", fs.constants.F_OK);
} catch (err) {
await fs.mkdir("./tmp");
}
await fs.writeFile(tempFilepath, typescriptSource);
const program = ts.createProgram([tempFilepath], {
module: ts.ModuleKind.NodeNext,
moduleResolution: ts.ModuleResolutionKind.NodeNext,
target: ts.ScriptTarget.ES2021,
alwaysStrict: true,
skipLibCheck: true,
});
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length === 0) {
console.log("No type errors found.");
} else {
diagnostics.forEach((diagnostic) => {
if (diagnostic.file) {
const { line, character } =
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
const message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
"\n"
);
console.log(
`${diagnostic.file.fileName} (${line + 1},${
character + 1
}): ${message}`
);
} else {
console.log(
ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")
);
}
});
}
} catch (e) {
console.log(e);
} finally {
try {
await fs.rm(tempFilepath);
} catch (e) {
// Do nothing
}
}
};
run();