@clack/prompts: 一个命令行工具

一个命令行的控件工具
更新于: 2024-02-20 10:31:59
一图胜千言

安装

yarn add @clack/prompts

cheatsheet

功能代码
spinner
import { spinner } from '@clack/prompts';

const s = spinner();
s.start('Installing via npm');
await new Promise((resolve) => setTimeout(resolve, 2000));
s.stop('Installed via npm');
text(input): 单行文本
import { text } from '@clack/prompts';

const meaning = await text({
  message: 'What is the meaning of life?',
  placeholder: 'Not sure',
  initialValue: '42',
  validate(value) {
    if (value.length === 0) return `Value is required!`;
  },
});
confirm(boolean)
import { confirm } from '@clack/prompts';

const shouldContinue = await confirm({
  message: 'Do you want to continue?',
});
select(single/radio)
import { select } from '@clack/prompts';

const projectType = await select({
  message: 'Pick a project type.',
  options: [
    { value: 'ts', label: 'TypeScript' },
    { value: 'js', label: 'JavaScript' },
    { value: 'coffee', label: 'CoffeeScript', hint: 'oh no' },
  ],
});
select(multiple)
import { multiselect } from '@clack/prompts';

const additionalTools = await multiselect({
  message: 'Select additional tools.',
  options: [
    { value: 'eslint', label: 'ESLint', hint: 'recommended' },
    { value: 'prettier', label: 'Prettier' },
    { value: 'gh-action', label: 'GitHub Action' },
  ],
  required: false,
});

参考