-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-template.ts
30 lines (24 loc) · 922 Bytes
/
create-template.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
import fs from "fs";
import yargs from "yargs";
import { fetchProblemData, DataScrape } from "./leetcodeData";
let argv = yargs(process.argv).argv;
if ("_" in argv) {
// fetch title
let url: string = argv._[2] as string;
if (!url) throw new Error("No Url Found");
fetchProblemData(url).then(async (response) => {
if (!response) throw new Error("No response from problem.");
let { questionFrontendId, titleSlug, title } = response;
let fileContents = await fs.readFileSync("./leetcode/template.ts", "utf8");
fileContents = fileContents
.replace("{title}", `${questionFrontendId}. ${title}`)
.replace("{url}", url);
const fileName = `${questionFrontendId}-${titleSlug}.ts`;
try {
await fs.writeFileSync("./leetcode/" + fileName, fileContents);
console.log(fileName, "written successfully");
} catch (error) {
console.log("error", error);
}
});
}