UNPKG

leetcode-fetcher-cli

Version:

A CLi Application for local fetching of leetcode problems

192 lines (191 loc) 7.87 kB
"use strict"; /** * @author Riccardo La Marca * * @brief Submission-releated Commands: * - watch [Cache the given problem id for future tests or submissions] * - test [Run the tests for specified or cached question identifier] * - submit [Submit the code of specified or cached question] */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.submit_command = exports.test_command = exports.watch_command = void 0; const problems_1 = require("./problems"); const general_1 = require("../utils/general"); const printer_1 = require("../utils/printer"); const prompt_1 = __importDefault(require("prompt")); const chalk_1 = __importDefault(require("chalk")); const constants_1 = __importDefault(require("../constants")); const lc = __importStar(require("../leetcode")); const AskForCreation = async (state) => { prompt_1.default.message = ''; prompt_1.default.delimiter = ''; try { const { answer } = await prompt_1.default.get(constants_1.default.PROMPT.CREATE_QUESTION); if (answer === 'Y') return state; console.error(chalk_1.default.redBright("[ERROR] Watch command exits.")); } catch (error) { return null; } return null; }; const WatchCommand = async (data, state) => { if (data.length < 1) { console.error(chalk_1.default.redBright("[ERROR] At least one input must be given.")); return state; } const is_daily = data[0] === 'daily'; const instances = await (0, general_1.GetExistingProblems)(state.variables["FOLDER"].value); // If the input is given, we need to check that the provided IDX // belongs to the set of already created instances. let problem_id = 0; if (is_daily) { if (!state.dailyQuestion) { console.error(chalk_1.default.redBright("[ERROR] No daily question available.")); return state; } problem_id = Number.parseInt(state.dailyQuestion.questionFrontendId); // Check that there exists an instance of the given problem ID if (!instances.includes(Number.parseInt(problem_id.toString()))) { console.warn(chalk_1.default.yellowBright("[WARNING] No local instance found.")); const result = await AskForCreation(state); if (!result) return state; state = await problems_1.daily_command.callback(["create"], state); } state.watchQuestionId = problem_id; state.watchQuestion = state.dailyQuestion; } else { // First wee need to check that there are locally fetched problems if (!state.fetchedProblems) { console.error(chalk_1.default.redBright("[ERROR] No locally fetched problems available.")); return state; } // Otherwise take the corresponding problem and see // if a corresponding instance exists const problem_idx = Number.parseInt(data[0]); if (problem_idx >= state.fetchedProblems.count) { console.error(chalk_1.default.redBright("[ERROR] Input index exceeds fetched problems")); return state; } const problem = state.fetchedProblems.questions[problem_idx]; problem_id = Number.parseInt(problem.questionFrontendId); if (!instances.includes(Number.parseInt(problem_id.toString()))) { console.warn(chalk_1.default.yellowBright("[WARNING] No local instance found.")); const result = await AskForCreation(state); if (!result) return state; state = await problems_1.create_command.callback([data[0]], state); } state.watchQuestionId = problem_id; state.watchQuestion = problem; } console.log(chalk_1.default.greenBright(`[INFO] Watching Problem ID ${problem_id}`)); return state; }; exports.watch_command = { group: 'Submission', name: 'Watch Command', command: 'watch', syntax: /^watch\s+(\d+|daily)$/, callback: WatchCommand, help: 'watch <question-idx|daily> - Cache the given problem for future tests\n' + 'or submissions. An instance of the given problem must exists.\n' + 'Moreover the provided problem must also be locally fetched.' }; const TestCommand = async (_, state) => { // First we need to check if a session is currently available if (!state.cookies) { console.error(chalk_1.default.redBright('[ERROR] No current in a session.' + ' Please login or load a valid session!')); return state; } // Then we check for watching selected problems if (!state.watchQuestionId) { console.error(chalk_1.default.redBright('[ERROR] No problem current being watched.')); return state; } const result = await lc.TestSolution(state); if (!result) { console.error(chalk_1.default.redBright("[ERROR] Something went wrong!")); return state; } (0, printer_1.PrintTestDetails)(state.watchQuestion, result); return state; }; // Test Command - test the solution implementation exports.test_command = { group: 'Submission', name: 'Test Command', command: 'test', syntax: /^test$/, callback: TestCommand, help: 'test - Tests the selected problem in leetcode.' }; const SubmitCommand = async (_, state) => { // First we need to check if a session is currently available if (!state.cookies) { console.error(chalk_1.default.redBright('[ERROR] No current in a session.' + ' Please login or load a valid session!')); return state; } // Then we check for watching selected problems if (!state.watchQuestionId) { console.error(chalk_1.default.redBright('[ERROR] No problem current being watched.')); return state; } const result = await lc.SubmitSolution(state); if (!result) { console.error(chalk_1.default.redBright("[ERROR] Something went wrong!")); return state; } (0, printer_1.PrintSubmissionResults)(state.watchQuestion, result); state.profile = await lc.GetUserData(state.selectedUser, state); return state; }; exports.submit_command = { group: 'Submission', name: 'Submit Command', command: 'submit', syntax: /^submit$/, callback: SubmitCommand, help: 'submit - Submit the selected problem solution in leetcode.' };