-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathquarto-build.js
53 lines (48 loc) · 1.55 KB
/
quarto-build.js
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
const fs = require("node:fs");
const { glob } = require("glob");
const { execSync } = require("node:child_process");
const IGNORED_CELL_REGEX = /```\w*?\n\/\/ ?@ls-docs-hide-cell\n[\s\S]*?```/g;
async function main() {
const allIpynb = await glob("./docs/**/*.ipynb");
const allRenames = allIpynb.flatMap((filename) => [
filename.replace(".ipynb", ".md"),
filename.replace(".ipynb", ".mdx"),
]);
const pathToRootGitignore = ".gitignore";
let gitignore = fs.readFileSync(pathToRootGitignore, "utf-8");
gitignore = gitignore.split("# AUTO_GENERATED_DOCS")[0];
gitignore += "# AUTO_GENERATED_DOCS\n";
gitignore += allRenames.join("\n");
fs.writeFileSync(pathToRootGitignore, gitignore);
for (const renamedFilepath of allRenames) {
if (fs.existsSync(renamedFilepath)) {
let content = fs.readFileSync(renamedFilepath).toString();
if (content.match(IGNORED_CELL_REGEX)) {
content = content.replace(IGNORED_CELL_REGEX, "");
fs.writeFileSync(renamedFilepath, content);
}
}
}
try {
/**
* Run Prettier on all generated .ipynb -> .mdx because we don't
* currently have another way to format code written in notebooks.
*/
const command = `yarn prettier --write ${allRenames
.filter((filename) => fs.existsSync(filename))
.join(" ")}`;
execSync(command);
} catch (error) {
console.error(
{
error,
stdout: error?.stderr?.toString(),
},
"Failed to format notebooks"
);
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});