-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support hidden docs cells and TS validation in notebooks (#6259)
- Loading branch information
1 parent
ad391d5
commit 9091012
Showing
7 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters