1

I'm writing a small CLI in typescript and I have a command which basically allows me to generate a json file with default values in it (just like npm init -y), but I don't know how to auto answer the questions in inquirer.

This is what I've got so far:

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);
      })
    );
};

I thought that if I'd skip/hide the questions with when(), it would use the default values, but it doesn't. It's always undefined.

Didn't find this topic on the internet so far. Any ideas?

4

1 回答 1

1

一种生活技巧,但我设法通过创建一个defaults()返回默认值对象的函数来“自动回答”我在查询器中的问题。

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

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);
      })
    );
};
于 2020-12-04T13:48:08.807 回答