Why is the terminal in an infinite loop with the prompts?

90 views Asked by At

In relation to my previous question: Why is Node.js throwing an error to my require() statement?

I added type: module to my packages.json and the code works and asks the questions. However, it keep asking the questions instead of stopping at the last question and replacing the placeholders.

#!/usr/bin/env node

import { program } from 'commander';
import { execa } from 'execa';
import inquirer from 'inquirer';
import fs from 'fs';
import path from 'path';

program
  .command('make-plugin <folder-name>')
  .description('Clone repo and change values')
  .action(async (folderName) => {
    const repoURL = 'https://github.com/wpdev95/plugin-boiler/tree/main';
    const targetFolder = path.resolve(process.cwd(), folderName);

    // Clone the repository
    await execa('git', ['clone', repoURL, folderName]);

    // Enumerate through files and replace placeholders
    const files = fs.readdirSync(targetFolder);
    const questions = [
  {
    type: 'input',
    name: 'Test Plugin',
    message: 'What is the Name of your plugin?',
  },
  {
    type: 'input',
    name: 'test-plugin',
    message: 'What is the slug of your plugin?',
  },
  {
    type: 'input',
    name: 'Plugin Author',
    message: 'What is the author\'s name?',
  },
];

    for (const file of files) {
      const filePath = path.join(targetFolder, file);
      let content = fs.readFileSync(filePath, 'utf-8');

      // Replace placeholders in the content
      const answers = await inquirer.prompt(questions);
      for (const key in answers) {
        const placeholder = `{{${key}}}`;
        const replacement = answers[key];
        content = content.replace(new RegExp(placeholder, 'g'), replacement);
      }

      // Update PHP file name
      if (file === 'plugin-boilerplate.php') {
        const newPhpFileName = `${answers['plugin_slug']}.php`;
        const newPhpFilePath = path.join(targetFolder, newPhpFileName);
        fs.renameSync(filePath, newPhpFilePath);
      }

      // Write back to the file
      fs.writeFileSync(filePath, content, 'utf-8');
    }

    console.log('Plugin boilerplate was made successfully! Happy coding! :D');
  });

program.parse(process.argv);
0

There are 0 answers