如何自动回答 inquirer.js 中的问题?

How can I auto answer questions in inquirer.js?

我正在用 typescript 编写一个小的 CLI,我有一个命令基本上允许我生成一个 json 文件,其中包含默认值(就像 npm init -y),但我不'知道如何自动回答询问者的问题

这是我目前得到的:

export const initializeConfig = (project: string, ...args: boolean[]) => {
  prompt([
    {
      type: "input",
      name: "name",
      message: "What is the name of the project?",
      default: basename(cwd()),
      when: () => args.every((arg) => arg === false),
    },
    {
      type: "list",
      name: "project",
      message: "What is the type of the project?",
      choices: ["Node", "Python"],
      default: project,
      when: () => args.every((arg) => arg === false),
    },
  ])
    .then((answers: Answers) => {
      config = setConfig({ name: answers.name });
      config = setConfig({ project: answers.project });
    })
    .then(() =>
      prompt([
        {
          type: "input",
          name: "path",
          message: "Where is your project root located?",
          default: ".",
          when: () => args.every((arg) => arg === false),
        },
        {
          type: "input",
          name: "ignore",
          message: "What do you want to ignore? (comma separated)",
          default: defaultIgnores(config.project).ignore,
          when: () => args.every((arg) => arg === false),
        },
      ]).then((answers: Answers) => {
        config = setConfig(ignoreFiles(config.project, answers.ignore));
        createConfig(answers.path, config);
      })
    );
};

我认为如果我 skip/hide 带有 when() 的问题,它会使用默认值,但事实并非如此。总是 undefined.

目前在 Internet 上未找到此主题。有什么想法吗?

有点生活小窍门,但我通过创建 defaults() 函数 returns 默认值的对象,设法“自动回答”我在询问者中的问题。

如果我的答案对象是空的,我就可以使用它们,如下所示:

const defaults = (project: string) => {
  return {
    name: basename(cwd()),
    project,
    path: ".",
    ignore: defaultIgnores(project).ignore,
  };
};

export let config: any = {
  version,
};

export const initializeConfig = (project: string, ...args: boolean[]) => {
  prompt([
    {
      type: "input",
      name: "name",
      message: "What is the name of the project?",
      default: defaults(project).name,
      when: () => args.every((arg) => arg === false),
    },
    {
      type: "list",
      name: "project",
      message: "What is the type of the project?",
      choices: ["Node", "Python"],
      default: defaults(project).project,
      when: () => args.every((arg) => arg === false),
    },
  ])
    .then((answers: Answers) => {
      const { name, project: projectName } = defaults(project);

      config = setConfig({ name: answers.name || name });
      config = setConfig({ project: answers.project || projectName });
    })
    .then(() =>
      prompt([
        {
          type: "input",
          name: "path",
          message: "Where is your project root located?",
          default: defaults(project).path,
          when: () => args.every((arg) => arg === false),
        },
        {
          type: "input",
          name: "ignore",
          message: "What do you want to ignore? (comma separated)",
          default: defaults(project).ignore,
          when: () => args.every((arg) => arg === false),
        },
      ]).then((answers: Answers) => {
        const { ignore, path } = defaults(project);

        config = setConfig(
          ignoreFiles(config.project, (answers.ignore || ignore)!)
        );
        createConfig(answers.path || path, config);
      })
    );
};