询问者包裹多次打印的奇怪行为

问题描述 投票:0回答:1

我正在尝试创建一个 cli 工具来下载包(内部使用 npm)并创建文件。我正在使用询问器询问用户输入他们想要添加的数据库,但询问器的输出不是我预期的。

这是我的代码(我已经跳过了导入和其他样板代码):

const questions = [
  {
      type: 'list',
      name: 'option',
      message: 'Choose an option:',
      choices: [
          'MongoDB (mongoose)',
          'Firebase',
          'PostgreSQL',
          'MySQL',
          'Redis'
      ],
  }
];

argv.command({
  command: 'create-node-app',
  describe: 'Creates a basic node project with usual files',
  builder: {
    y: {
      describe: "Use default values to build the package.json",
      demandOption: false
    }
  },
  handler: async (argv) => {
    let packages = [];
    let dependencies = '';
    const execa = util.promisify(exec);

    async function setup(jsonFile) {
      // no libraries are given, add default librariesthen create the file
      if (argv._.length == 1) {
        console.log(chalk.yellow("Note: Predefined libraries will be added to the package.json"));
        
        console.log(chalk.yellow("Please select the database you'd like to connect to your application: "));
        // cliSelect({
        //   values: ["MongoDB", "PostgreSQL", "Firebase", "Redis", "MySQL"],
        //   valueRenderer: (value, selected) => {
        //     if(selected) {
        //       return chalk.blue(value)
        //     }
        //     return value;
        //   }
        // }).then(res => console.log("The result/value:", res, "\n"))
        await inquirer.prompt(questions).then((answers) => {
          console.log(chalk.green(`The package ${answers.option} will be downloaded`));
        }).catch(err => {
          console.log("There was an error");
        })

        packages.push("express", "ejs", "dotenv", "cors", "body-parser");
        dependencies = await installPackages(packages);
        renderTemplate("./bin/templates/_index.ejs", "./", jsonFile.main || "index.js", {path: "/public/index.html"});
      } else if(argv._.length > 1) {
        //get all the arguments after the first ('create-node-app') argument to get all packages
        packages = argv._.slice(1);
        dependencies = await installPackages(packages);
        createFile("./", jsonFile.main || "index.js", "//This is the server file");
      }
      createFolder("./", "public", rl);
      renderTemplate("./bin/templates/_indexhtml.ejs", "./public/", "index.html", jsonFile);
      renderTemplate("./bin/templates/_styles.ejs", "./public/", "styles.css", "");
      renderTemplate("./bin/templates/_app.ejs", "./public/", "app.js", "");
    }
    
    //if the y flag is used
    let jsonFile;
    if(argv.y) {
      (await execa("npm init -y")).stdout;
    } else {
      // await execa("npm init");
      execSync('npm init', {stdio: 'inherit'})
    }
    console.log("The package.json file has been created");
    jsonFile = JSON.parse(fs.readFileSync("./package.json"))
    setup(jsonFile);

    createFile("./", ".env", "");
    createFile("./", ".gitignore", "node_modules\n.env");
    console.log("Done");
  }
});

这就是我运行命令

create create-node-app -y
时的输出(这会运行
npm init -y
命令来创建默认的 package.json 库并安装预定义的包,但会询问要包含哪些数据库): When I run the above command

当我按箭头键选择数据库时:
enter image description here

当我按“enter”选择一个选项时:
enter image description here

如您所见,我也尝试使用

cli-select
包,但我遇到了同样的问题,所以我没有正确使用该包,但我不知道我错过了哪里?

其他函数,如

creatFolder
createFile
renderTemplate
的功能正如其名称所示。 (
renderTemplate
渲染
ejs
模板)

node.js terminal command-line-interface inquirerjs
1个回答
0
投票

对于未来面临同样问题的任何人。

我之前已经初始化过

readline
包,并且也在使用
inquirer
包。我所要做的就是删除
readline
包,因为它与
inquirer
一起使用终端,从而导致上述问题。

© www.soinside.com 2019 - 2024. All rights reserved.