@roeeyn/challenge-generator
Version:
Fetches a code challenge from the backend, and creates the necessary files to run locally.
111 lines (110 loc) • 4.57 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cli = void 0;
const questions_1 = require("./questions");
const utils_1 = require("./utils");
const api_1 = require("./api");
const templates_1 = require("./templates");
const models_1 = require("./models");
const cli = async () => {
(0, utils_1.showTitleAndBanner)();
const cliOptions = (0, utils_1.getCliOptions)();
global.IS_VERBOSE = cliOptions.verbose;
const skipConfirmation = cliOptions.skipConfirmation || false;
const author = (await (0, questions_1.generateQuestion)(cliOptions.author, skipConfirmation, {
name: "author",
message: "Search for author regex? (e.g. '^M')",
type: questions_1.QuestionType.INPUT,
})).author;
const authorId = (await (0, questions_1.generateQuestion)(cliOptions.authorId, skipConfirmation, {
name: "authorId",
message: "Search for author edabit id? (e.g. 'BkPgkDQGHm66X4Qai')",
type: questions_1.QuestionType.INPUT,
})).authorId;
const title = (await (0, questions_1.generateQuestion)(cliOptions.title, skipConfirmation, {
name: "title",
message: "Search for title regex? (e.g. 'ort$')",
type: questions_1.QuestionType.INPUT,
})).title;
const edabitId = (await (0, questions_1.generateQuestion)(cliOptions.edabitId, skipConfirmation, {
name: "edabitId",
message: "Search for edabitId? (e.g. '6vSZmN66xhMRDX8YT')",
type: questions_1.QuestionType.INPUT,
})).edabitId;
const minDifficulty = parseFloat((await (0, questions_1.generateQuestion)(cliOptions.minDifficulty, skipConfirmation, {
name: "minDifficulty",
message: "Search for min difficulty (from 0 to 5)? (e.g. '2.5')",
type: questions_1.QuestionType.NUMBER,
})).minDifficulty);
const minQuality = parseFloat((await (0, questions_1.generateQuestion)(cliOptions.minQuality, skipConfirmation, {
name: "minQuality",
message: "Search for min quality (from 0 to 5)? (e.g. '2.5')",
type: questions_1.QuestionType.NUMBER,
})).minQuality);
const programmingLanguage = (await (0, questions_1.generateQuestion)(cliOptions.programmingLanguage, skipConfirmation, {
name: "programmingLanguage",
message: "Filter by programming language?",
type: questions_1.QuestionType.LIST,
choices: [
{
name: "JavaScript",
value: models_1.ProgrammingLanguage.JAVASCRIPT,
},
{
name: "Java",
value: models_1.ProgrammingLanguage.JAVA,
},
{
name: "Python",
value: models_1.ProgrammingLanguage.PYTHON,
},
],
})).programmingLanguage;
const tags = (await (0, questions_1.generateQuestion)(cliOptions.tags, skipConfirmation, {
name: "tags",
message: "Search for tags? (e.g. 'strings, algorithms, sorting')",
type: questions_1.QuestionType.INPUT,
})).tags;
const resultParams = (0, utils_1.cleanCliOptions)({
title,
edabitId,
author,
authorId,
tags,
minDifficulty,
minQuality,
programmingLanguage,
});
if (skipConfirmation) {
(0, utils_1.showInfo)("Requesting challenge with params:");
(0, utils_1.showInfo)(`${JSON.stringify(resultParams, null, 2)}`);
try {
const challenge = await (0, api_1.getChallengeFromApi)(resultParams);
await (0, templates_1.createFilesFromChallenge)(challenge);
}
catch (error) {
(0, utils_1.showError)(error.message);
process.exit(1);
}
(0, utils_1.showSuccess)("Challenge created successfully!");
}
else {
const { isConfirmed } = await (0, questions_1.confirmParamsQuestion)(resultParams);
if (isConfirmed === true) {
(0, utils_1.showInfo)("Fetching challenge based on the provided parameters...");
try {
const challenge = await (0, api_1.getChallengeFromApi)(resultParams);
await (0, templates_1.createFilesFromChallenge)(challenge);
}
catch (error) {
(0, utils_1.showError)(error.message);
process.exit(1);
}
(0, utils_1.showSuccess)("Challenge created successfully!");
}
else {
(0, utils_1.showError)("Aborted! No action was taken.");
}
}
};
exports.cli = cli;