leetcode/.scripts/addnew

56 lines
1.4 KiB
Plaintext
Executable File

#!/usr/bin/env zx
import Turndown from "turndown";
import { client, gql } from "./graphql.mjs";
const langExt = { python3: "py", nodejs: "js", cpp: "cpp" };
const link = await question("Enter leetcode problem link: ");
const titleSlug = path.basename(new URL(link).pathname);
const language = await question("Enter language: ", {
choices: Object.keys(langExt),
});
const { question: q } = await client.request(
gql`
query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
questionFrontendId
boundTopicId
title
titleSlug
content
}
}
`,
{
titleSlug,
}
);
const questionIdPadded = q.questionId.padStart(4, "0");
const questionDir = `${questionIdPadded}_${q.titleSlug}`;
const solutionDir = `${questionDir}/${language}`;
const solutionFilePath = `${solutionDir}/solution.${langExt[language]}`;
const td = new Turndown({});
td.addRule("pre-code", {
filter: "pre",
replacement(content) {
return "```\n" + content + "```";
},
});
const mdBody = td.turndown(q.content);
await $`mkdir -p ${solutionDir}`;
await $`touch ${solutionFilePath}`;
fs.writeFileSync(`${questionDir}/README.md`, mdBody, { encoding: "utf-8" });
// TODO: Wait for solution to be pasted and saved
// TODO: If above is successful (prompt for a key), then git add . and git commit
console.log("Done!");