leetcode-fetcher-cli
Version:
A CLi Application for local fetching of leetcode problems
115 lines (114 loc) • 5.27 kB
JavaScript
;
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.RunApp = void 0;
const promises_1 = require("readline/promises");
const process_1 = require("process");
const problems_1 = require("./commands/problems");
const commands_1 = __importDefault(require("./commands"));
const constants_1 = __importDefault(require("./constants"));
const chalk_1 = __importDefault(require("chalk"));
const formats = __importStar(require("./utils/formatter"));
const generic = __importStar(require("./utils/general"));
const ExcuteCommandIfExists = async (command, state, commands) => {
// Get the command name and check that exists
const cmd_name = command.split(" ")[0];
const cmd_filter = commands.filter((value) => value.command === cmd_name);
if (cmd_filter.length < 1) {
const error_str = formats.FormatString("No Command: {0}", command);
console.error(chalk_1.default.redBright(error_str));
return state;
}
// Otherwise, get the actual command and obtain the matching groups
const app_command = cmd_filter[0];
const match = command.match(app_command.syntax);
let data = match?.slice(1, match.length) || [];
// Execute the command
state = await app_command.callback(data, state);
state.lastCommand = command;
return state;
};
const PrintEntryView = () => {
console.log(chalk_1.default.rgb(255, 128, 0)(constants_1.default.APP.TITLE));
console.log();
const title_len = constants_1.default.APP.TITLE.split('\n')[0].length;
const rem_size = (title_len - constants_1.default.APP.SUBTITLE.length) / 2 - 1;
const padding = '─'.repeat(rem_size);
const subtitle_styled = chalk_1.default.italic(chalk_1.default.gray(constants_1.default.APP.SUBTITLE));
const complete_subtitle = `${padding} ${subtitle_styled} ${padding}`;
console.log(complete_subtitle);
console.log();
const author_name = chalk_1.default.bold("Riccardo La Marca");
const author_email = chalk_1.default.italic("riccardo.lamarca98@gmail.com");
const repo_url = chalk_1.default.underline('https://github.com/lmriccardo/leetcode-fetcher.git');
console.log(`@${chalk_1.default.gray("Author")} ${author_name} <${author_email}>`);
console.log(`@${chalk_1.default.gray("Repository")} ${repo_url}`);
console.log(`@${chalk_1.default.gray("Version")} v0.1.2`);
console.log();
};
const RunApp = async () => {
PrintEntryView();
// Fetch the total number of problems for each difficulty
const nproblems = await generic.GetAllProblemsCount();
const totals = generic.ArraySum(...Object.values(nproblems));
const counts = { ...nproblems, ALL: totals };
// Initialize the app state
let app_state = {
commands: commands_1.default,
variables: constants_1.default.APP.APP_VARIABLES,
problemsCount: counts
};
app_state = await problems_1.daily_command.callback([""], app_state);
const question = chalk_1.default.magentaBright(">> (Type help for commands): ");
// Loop indefinitely up until the quit command is not given
for (;;) {
const date_now = (new Date()).toLocaleString('en-IT');
// Create the interface for reading the stdin
const rl = (0, promises_1.createInterface)({ input: process_1.stdin, output: process_1.stdout });
const answer = (await rl.question(`[${chalk_1.default.blueBright(date_now)}] ${question}`)).trim();
rl.close();
if (answer.length < 1)
continue; // Check that there is a command
// Otherwise execute the command
app_state = await ExcuteCommandIfExists(answer, app_state, commands_1.default);
if (answer === 'quit') {
break;
}
}
};
exports.RunApp = RunApp;