通用交互式命令行用户界面的集合。

    安装

    1. npm install inquirer

    问题参数

    • type:(String)表示提问的类型,包括:input, confirm, list, rawlist, expand, checkbox, password, editor;
    • name: (String)当前问题回答的变量key;
    • message:(String|Function) 问题的描述;
    • default:(String|Number|Boolean|Array|Function) 默认值;未输入任何内容时使用的默认值,或返回默认值的函数。
    • choices: (Array|Function)Choices数组或返回Choices数组的函数。如果定义为函数,则第一个参数将是当前查询程序会话答案。数组值可以是简单的数字、字符串或包含名称(显示在列表中)、值(保存在答案哈希中)和短属性(选择后显示)的对象。choices数组还可以包含分隔符。
    • validate: (Function)对用户的回答进行校验;
    • filter: (Function)对用户的回答进行过滤处理,返回处理后的值;
    • transformer: (Function)对用户回答的显示效果进行处理(如:修改回答的字体或背景颜色),但不会影响最终的答案的内容;
    • when: (Function|Boolean) 根据前面问题的回答,判断当前问题是否需要被回答;
    • pageSize: (Number) 更改使用list、rawList、expand或checkbox时要呈现的行数。
    • prefix:(String) 修改message默认前缀;
    • suffix:(String) 修改message默认后缀。
    • askAnswered: (Boolean) 如果答案已经存在,则强制提示问题。
    • loop: (Boolean) 启用列表循环。默认值:true

    使用

    1. var inquirer = require(‘inquirer');
    2. inquirer.prompt(questions).then(answers => {
    3. // TODO
    4. })

    简单例子

    1. eg1
    2. const promptList = [
    3. {
    4. type: 'input',
    5. name: 'a',
    6. message: '页面标题'
    7. }
    8. ];
    9. // 询问框问题
    10. inquirer.prompt(promptList).then(answers => {
    11. // 得到用户答案
    12. console.log(answers);
    13. })

    0EF16C83-FB4E-41E4-8DFC-1C0222689081.png

    1. eg2
    2. const promptList = [
    3. {
    4. type: 'confirm',
    5. name: 'b',
    6. message: 'Are you ok?',
    7. default: true
    8. }
    9. ];

    5430D77C-D489-4574-9D2F-FA2FBE22E875.png

    1. eg3
    2. const promptList = [
    3. {
    4. type: 'list',
    5. message: '请选择种类:',
    6. name: 'c',
    7. choices: [
    8. 'A',
    9. 'B',
    10. 'C'
    11. ],
    12. filter: function (val) {
    13. // 使用filter将回答变为小写
    14. return val.toLowerCase();
    15. }
    16. }
    17. ];

    101333B5-2729-4781-8ADF-E053823EAF0B.png

    1. eg4:
    2. const promptList = [
    3. {
    4. type: 'password',
    5. name: 'd',
    6. message: '请输入密码’,
    7. validate:function() {
    8. if (val == '') {
    9. return '请输入密码';
    10. }
    11. return true;
    12. }
    13. }
    14. ];

    5EDDF140-6CE3-4DFA-AB66-CE8176BA3EFE.png

    1. eg5:
    2. const promptList = [
    3. {
    4. type: "checkbox",
    5. message: "请选择种类:",
    6. name: "color",
    7. choices: [
    8. 'a',
    9. 'b',
    10. 'c'
    11. ]
    12. }
    13. ];

    0DAFB62F-79FA-4C3B-9ABC-6E96127E45DD.png

    npm link

    1. link:到模块目录 npm link 进行全局link
    2. 使用:到所需要的项目下,可直接使用此模块
    3. 解除linknpm unlink 模块名

    DEMO

    templates/jstemp.js

    1. layui.define(function (exports) {
    2. layui.use(['element', 'table', 'form', 'admin', 'view', 'laytpl', 'common', 'util'], function () {
    3. var element = layui.element,
    4. $ = layui.$,
    5. table = layui.table,
    6. form = layui.form,
    7. admin = layui.admin,
    8. view = layui.view,
    9. laytpl = layui.laytpl,
    10. common = layui.common,
    11. router = layui.router(),
    12. util = layui.util,
    13. setter = layui.setter;
    14. // 初始化搜索form
    15. form.render();
    16. util.event('lay-btn-operation', {
    17. })
    18. })
    19. exports('<%= fileName || "default" %>', {}); //注意,这里是模块输出的核心,模块名必须和use时的模块名一致
    20. });

    templates/htmltemp.html

    1. <div class="content-container">
    2. <div class="container-title"></div>
    3. <div class="container-title-small" id="listNum"></div>
    4. <div class="table-container">
    5. <!-- 查询区域 -->
    6. <div class="layui-form layui-form-search" lay-filter="layadmin-search-form">
    7. <form>
    8. <div class="layui-form-item">
    9. <div class="layui-inline">
    10. <label class="layui-form-label-nowidth">员工职务:</label>
    11. <div class="inline">
    12. <select name="role">
    13. <option value="">全部</option>
    14. <option value="1">列车长</option>
    15. <option value="2">副列车长</option>
    16. <option value="3">乘务员</option>
    17. <option value="4">乘警</option>
    18. </select>
    19. </div>
    20. </div>
    21. </div>
    22. <div>
    23. <input type="button" class="layui-btn" id="search-btn" lay-submit lay-filter="LAY-front-search" value="查询">
    24. <input type="reset" class="layui-btn layui-btn-primary" value="重置">
    25. </div>
    26. </form>
    27. </div>
    28. <!-- 内容主体区域 -->
    29. <div class="layui-fluid layui-card">
    30. <table id="LAY-table-list" lay-filter="filter-table-list" lay-even lay-skin="nob"></table>
    31. </div>
    32. </div>
    33. </div>
    34. <script>
    35. layui.use('<%= fileName || "default" %>', layui.factory('<%= fileName || "default" %>'));
    36. </script>

    cli.js

    1. #!/usr/bin/env node
    2. const fs = require('fs');
    3. const path = require('path');
    4. const inquirer = require('inquirer'); // 命令行交互询问
    5. const ejs = require('ejs'); // 模版引擎
    6. const promptList = [{
    7. type: 'input',
    8. name: 'fileName',
    9. message: '请输入页面文件名称:',
    10. validate: function (val) {
    11. if (val == '') {
    12. return '请输入页面文件名称';
    13. }
    14. return true;
    15. }
    16. }, {
    17. type: 'input',
    18. name: 'filePath',
    19. message: '请输入html页面文件路径:',
    20. }, {
    21. type: 'confirm',
    22. name: 'createJs',
    23. message: '是否需要所对应的js文件?',
    24. default: true
    25. }, {
    26. type: 'input',
    27. name: 'fileJsPath',
    28. message: '请输入js文件路径?',
    29. default: 'src/controller',
    30. when: function(answers) {
    31. return answers.createJs;
    32. }
    33. }];
    34. // 询问框问题
    35. inquirer.prompt(promptList).then(answers => {
    36. // 得到用户答案 根据用户回答的结果生成文件
    37. const tmplDir = path.join(__dirname, 'templates'); // 模板目录
    38. const destDir = process.cwd(); // 目标目录
    39. // 将模板下的文件全部转换到目标目录
    40. fs.readdir(tmplDir, (err, files) => {
    41. // 得到目录下的所有文件
    42. if (err) throw err;
    43. files.forEach(file => {
    44. // 通过模板引擎渲染文件
    45. let suffix = file.substr(file.lastIndexOf('.') + 1); // 文件后缀
    46. ejs.renderFile(path.join(tmplDir, file), answers, (err, res) => {
    47. if (err) throw err;
    48. // 将结果写入目标文件路径
    49. if (suffix == 'html') {
    50. let htmlPath = path.join(destDir, `src/views/${answers.filePath}`);
    51. // 生成文件夹
    52. `src/views/${answers.filePath}`.split('/').forEach(d => {
    53. basePath += '/' + d;
    54. if (!fs.existsSync(basePath)) {
    55. fs.mkdirSync(basePath);
    56. }
    57. })
    58. fs.writeFileSync(path.join(destDir, `src/views/${answers.filePath}/${answers.fileName}.html`), res);
    59. }
    60. if (suffix == 'js' && answers.createJs) {
    61. fs.writeFileSync(path.join(destDir, `${answers.fileJsPath}/${answers.fileName}.js`), res);
    62. }
    63. })
    64. })
    65. })
    66. })