葵花宝典

vuePress-theme-reco 前端小菜-贺俊兰    2021
葵花宝典 葵花宝典

Choose mode

  • 关灯
  • 自动
  • 开灯
主页
分类
  • LeetCode
  • JavaScript
  • Node
  • 其他
  • VUE
标签
时间轴
author-avatar

前端小菜-贺俊兰

33

文章

7

标签

主页
分类
  • LeetCode
  • JavaScript
  • Node
  • 其他
  • VUE
标签
时间轴
  • node

    • node交互式
    • 根据模版生成文件
    • 每日暖心邮件

node交互式

vuePress-theme-reco 前端小菜-贺俊兰    2021

node交互式

前端小菜-贺俊兰 2020-12-18 Node

# 介绍

类似vue cli创建项目的时候的效果,主要是自己之前一只比较好奇,今天发现直接用npm库就能解决,觉得不是那么高大上了,不过还是记录一下

# 完整demo

其实这个结合自动生成代码可以组成一个非常不错的自动化的工程,懒得去弄这一套了,就弄一部分记录一部分了,直接上代码

    /**
     * @Author: junlan.he
     * @date: 2020/12/18
     * @desc: 交互式node
     */

    const inquirer = require("inquirer");//交互式命令
    const chalk = require("chalk"); //添加颜色背景
    const figlet = require("figlet"); //字体转换
    const init = () => {
        console.log(
            chalk.blue(
                figlet.textSync("HEJUNLAN", {
                    // font: "Ghost",
                    horizontalLayout: "default",
                    verticalLayout: "default",
                    width: 80,
                    whitespaceBreak: true
                })
            )
        );
    };
    const askQuestions = () => {
        const questions = [
            {
                name: "fileName",
                type: "input",
                message: "请输入文件名!"
            },
            {
                type: "list",
                name: "extension",
                message: "请选择文件类型",
                choices: [".vue", ".js"],
                filter: function(val) {
                    return val.split(".")[1];
                }
            }
        ];
        return inquirer.prompt(questions);
    };
    const createFile = (filename, extension) => {
        const filePath = `${process.cwd()}/${filename}.${extension}`
        return filePath;
    };
    const success = filepath => {
        console.log(
            chalk.white(`Done! File created at ${filepath}`)
        );
    };
    const run = async () => {
        init();
        const answers = await askQuestions();
        const { fileName, extension } = answers;
        const filePath = createFile(fileName, extension);
        success(filePath);
    };
    run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
欢迎来到 葵花宝典
看板娘