From caacc47e72f51303acff9398d5eb33e931ed3906 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:42 +0100 Subject: [PATCH] feat(euler): allow the challenge name to be passed into the generate command - --- challenges/euler/scripts/generate.js | 49 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/challenges/euler/scripts/generate.js b/challenges/euler/scripts/generate.js index 964636d..e173922 100644 --- a/challenges/euler/scripts/generate.js +++ b/challenges/euler/scripts/generate.js @@ -5,6 +5,9 @@ const { root, src, thoughts: thoughtsDir } = require('../constants'); const axios = require('axios'); const cheerio = require('cheerio'); +process.argv.shift(); +process.argv.shift(); + const readmeContent = fs.readFileSync(path.join(root, 'readme.md')).toString(); const problems = readmeContent.match(/^- \[.] (?:\[(.*)\]|(.*))/gm).map(res => { @@ -12,28 +15,33 @@ const problems = readmeContent.match(/^- \[.] (?:\[(.*)\]|(.*))/gm).map(res => return sanitised.match(/[0-9]* - (.*)/)[1]; }); +const questions = []; + +if (isNaN(process.argv[0])) + questions.push({ + name: 'problemNumber', + message: 'Which problem would you like to solve?', + type: 'number', + validate: input => { + input = parseInt(input); + + if (input > 100) return 'Please make sure you choose a number between 1 and 100!'; + else { + let alreadyGenerated = false; + fs.readdirSync(src).forEach(file => { + if (file.startsWith(input)) alreadyGenerated = true; + }); + + if (alreadyGenerated) + return 'Please choose a problem you have not already completed!'; + else return true; + } + } + }); + inquirer .prompt([ - { - name: 'problemNumber', - message: 'Which problem would you like to solve?', - type: 'number', - validate: input => { - input = parseInt(input); - - if (input > 100) return 'Please make sure you choose a number between 1 and 100!'; - else { - let alreadyGenerated = false; - fs.readdirSync(src).forEach(file => { - if (file.startsWith(input)) alreadyGenerated = true; - }); - - if (alreadyGenerated) - return 'Please choose a problem you have not already completed!'; - else return true; - } - } - }, + ...questions, { name: 'thoughts', message: 'Should I generate a thoughts document for you?', @@ -42,6 +50,7 @@ inquirer } ]) .then(({ problemNumber, thoughts }) => { + if (!problemNumber) problemNumber = parseInt(process.argv[0]); const fileName = `${problemNumber} - ${problems[problemNumber - 1]}`; // Fetch the problem data off of projecteuler.net