UNPKG

1.24 kBJavaScriptView Raw
1const path = require("path");
2const chalk = require("chalk");
3require("markdown-toc");
4const JoyCon = require("joycon");
5const toc = require("markdown-toc");
6
7const joyRead = new JoyCon({
8 // Stop reading at parent dir
9 // i.e. Only read file from process.cwd()
10 stopDir: path.dirname(process.cwd())
11});
12const { writeFile } = require("./helpers.js");
13// const projectDir = process.cwd();
14
15const generateToc = async () => {
16 const { path: filepath, data } = joyRead.loadSync(["fscripts.md"]);
17 if (!filepath) {
18 console.warn(
19 `${chalk.bold.red("You're missing the fscripts.md file!")}
20${chalk.green("Please run 'fsr generate' to get started!")}`
21 );
22 process.exit(0);
23 return null;
24 } else {
25 console.warn(`${chalk.bold.green("Located fscripts.md file!")}`);
26 let newFile = ``;
27 let tocSplit = data.split("<!-- end toc -->");
28 if (tocSplit.length === 2) {
29 newFile = toc(tocSplit[1]).content + "\n<!-- end toc -->\n\n" + tocSplit[1].trim();
30 } else {
31 newFile = toc(data).content + "\n<!-- end toc -->\n\n" + data.trim();
32 }
33 await writeFile("./fscripts.md", newFile);
34 }
35};
36
37module.exports = generateToc;