All files / src/install-from-boilerplate select-template.js

0% Statements 0/27
0% Branches 0/9
0% Functions 0/4
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                                       
const readline = require('node:readline');
const { templates } = require('./config-templates');
const Logger = require('../utils/logger');
 
/**
 * Prompts the user to select a content template interactively.
 * If the SPEC_UP_T_TEMPLATE environment variable is set, uses that value
 * without prompting (for non-interactive use, e.g., GitHubUi or CI).
 *
 * @returns {Promise<Object>} The selected template object from config-templates.js
 */
function selectTemplate() {
    // Check environment variable first (non-interactive use)
    const envTemplate = process.env.SPEC_UP_T_TEMPLATE;
    if (envTemplate) {
        const found = templates.find(t => t.id === envTemplate);
        if (found) {
            Logger.info(`Using content template from environment: ${found.name}`);
            return Promise.resolve(found);
        }
        Logger.warn(`Unknown template "${envTemplate}", falling back to default`);
        return Promise.resolve(templates[0]);
    }
 
    return new Promise((resolve) => {
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });
 
        console.log('\nšŸ“„ Available content templates:\n');
        for (const [i, t] of templates.entries()) {
            console.log(`  [${i}] ${t.name}`);
            console.log(`      ${t.description}\n`);
        }
 
        rl.question('Select a content template (number) [0]: ', (answer) => {
            rl.close();
            const index = Number.parseInt(answer, 10);
            if (!Number.isNaN(index) && index >= 0 && index < templates.length) {
                Logger.info(`Selected template: ${templates[index].name}`);
                resolve(templates[index]);
            } else {
                Logger.info('Using default template');
                resolve(templates[0]);
            }
        });
    });
}
 
module.exports = { selectTemplate, templates };