Commander.js-demo 发表于 2020-07-24 | 分类于 Project Base 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354#!/usr/bin/env nodeconst commander = require('commander');const inquirer = require('inquirer');const ora = require('ora');const chalk = require('chalk');const symbols = require('log-symbols');const packageJson = require('./package.json');const spinner = ora('正在下载模板...');// 命令const program = new commander.Command(packageJson.name) .version(packageJson.version) .arguments('<name>') .action((name) => { // 问题 inquirer .prompt([ { type: 'input', name: 'description', message: '请输入项目描述', }, { type: 'input', name: 'author', message: '请输入作者名称', }, ]) .then((answers) => { // 显示下载中 spinner.start(); setTimeout(() => { spinner.succeed(); console.log(symbols.success, name + '下载完成'); console.log('问题答案:', symbols.info, answers); console.log('--verbose 参数:', program.verbose); console.log('--info 参数:', program.info); console.log('--use-npm 参数', program.useNpm); }, 5000); }); }) .option('--verbose', 'print additional logs') .option('--info', 'print environment debug info') .option('--use-npm') .allowUnknownOption() .usage(`${chalk.green('<name>')} [options]`) .on('--help', () => { console.log( ` If you have any problems, do not hesitate to file an issue.` ); }) .parse(process.argv);