leetcode/.scripts/addnew

55 lines
1.4 KiB
Plaintext
Executable File

#!/usr/bin/env zx
import Turndown from "turndown";
import * as cheerio from "cheerio";
import { client, gql } from "./graphql.mjs";
const langExt = { python3: "py", nodejs: "js", cpp: "cpp", sql: "sql" };
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 $ch = cheerio.load(q.content);
$ch("pre").wrapInner("<code></code>");
const td = new Turndown({});
const mdBody = td.turndown($ch("body").html()) + `\n\n${link}`;
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!");