|
| 1 | +const fs = require('node:fs').promises; |
| 2 | +const path = require('node:path'); |
| 3 | +const process = require('node:process'); |
| 4 | +const { program } = require('commander'); |
| 5 | +const { number } = require('@inquirer/prompts'); |
| 6 | + |
| 7 | +async function readFiles(dir) { |
| 8 | + const files = await fs.readdir(dir); |
| 9 | + return Promise.all( |
| 10 | + files.map(async (file) => { |
| 11 | + const content = await fs.readFile(path.join(dir, file), 'utf8'); |
| 12 | + return { name: file, content }; |
| 13 | + }), |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +async function generatePrompt(numQuestions) { |
| 18 | + try { |
| 19 | + const questionsDir = path.join(__dirname, 'src', 'questions'); |
| 20 | + const existingQuestions = await readFiles(questionsDir); |
| 21 | + const exampleQuestion = await fs.readFile(path.join(__dirname, 'example-question.md'), 'utf8'); |
| 22 | + |
| 23 | + return ` |
| 24 | +You are an expert quiz creator for the CodeQuest project. Your task is to create ${numQuestions} new question(s) for our fun, engaging and playful quiz game. Here are the guidelines: |
| 25 | +
|
| 26 | +1. Each question should be in Markdown format, similar to the example provided below. |
| 27 | +2. Questions should increase in difficulty as they progress. |
| 28 | +3. The title and description should be fun, descriptive and engaging. Use alliterations and wordplay to make them stand out. |
| 29 | +3. Use the following features where appropriate: |
| 30 | + - Markdown formatting (bold, italics, code blocks, lists etc.) |
| 31 | + - Multiple choice answers |
| 32 | + - Time limits (optional) |
| 33 | + - Difficulty levels (Beginner, Intermediate, Expert) (optional) |
| 34 | + - Hints |
| 35 | + - Explanations |
| 36 | +
|
| 37 | +4. The structure should be exactly as follows: |
| 38 | + --- |
| 39 | + title: [Question Title] |
| 40 | + description: [Brief description] |
| 41 | + level: [number, determines order] |
| 42 | + correctAnswer: [number, 1-based index of correct answer] |
| 43 | + difficulty: [Beginner/Intermediate/Expert] |
| 44 | + timeLimit: [optional, in seconds] |
| 45 | + --- |
| 46 | +
|
| 47 | + ## Context |
| 48 | +
|
| 49 | + ### Introduction |
| 50 | + [Optional: Only include if necessary to understand the question] |
| 51 | +
|
| 52 | + ### Question |
| 53 | + [The actual question goes here] |
| 54 | +
|
| 55 | + ### Outro |
| 56 | + [Optional: Include only to provide additional resources or information] |
| 57 | +
|
| 58 | + ## Answers |
| 59 | + - [Answer option 1] |
| 60 | + - [Answer option 2] |
| 61 | + - [Answer option 3] |
| 62 | + - [Answer option 4] |
| 63 | +
|
| 64 | + ## Explanation |
| 65 | + [Detailed explanation of the correct answer] |
| 66 | +
|
| 67 | + ## Hint |
| 68 | + [Optional hint for the question] |
| 69 | +
|
| 70 | +5. Prefer short answer options, but use code fences for longer code snippets if necessary. |
| 71 | +6. Ensure questions are relevant to coding, technology, or related fields. |
| 72 | +7. Be creative and make the questions engaging! |
| 73 | +
|
| 74 | +Here's an example question for reference: |
| 75 | +
|
| 76 | +${exampleQuestion} |
| 77 | +
|
| 78 | +Now, based on the existing questions and this example, please create ${numQuestions} new question(s). Ensure they fit well with the existing set and increase in difficulty appropriately. |
| 79 | +
|
| 80 | +Existing questions for context: |
| 81 | +${existingQuestions.map(q => q.content).join('\n\n')} |
| 82 | +`; |
| 83 | + } |
| 84 | + catch (error) { |
| 85 | + console.error('Error generating prompt:', error); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +async function main() { |
| 90 | + program |
| 91 | + .option('-o, --output <file>', 'output file') |
| 92 | + .helpOption('-h, --help', 'display help for command') |
| 93 | + .parse(process.argv); |
| 94 | + |
| 95 | + const options = program.opts(); |
| 96 | + |
| 97 | + if (options.help) { |
| 98 | + program.outputHelp(); |
| 99 | + process.exit(0); |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + const numQuestions = await number({ message: 'How many questions do you want to generate?' }, { min: 1, max: 5 }); |
| 104 | + const prompt = await generatePrompt(numQuestions); |
| 105 | + |
| 106 | + if (options.output) { |
| 107 | + await fs.writeFile(options.output, prompt); |
| 108 | + // eslint-disable-next-line no-console |
| 109 | + console.log(`Prompt written to ${options.output}`); |
| 110 | + } |
| 111 | + else { |
| 112 | + process.stdout.write(prompt); |
| 113 | + } |
| 114 | + } |
| 115 | + catch (error) { |
| 116 | + console.error('An error occurred:', error); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +main(); |
0 commit comments