How to write a front-end toolchain for node

Through simulation, understand how node writes front-end tools.

foreword

::: note

In the days of front-end development, the basic process of using front-end tools is:

  1. Type the name of the tool
  2. answer a series of questions
  3. Tools do certain things for us, such as使用脚手架创建一个Vue工程

:::

Let's simulate it today.

tools used

SBoudrias/Inquirer.js: A collection of common interactive command line user interfaces. (github.com)

implement logic

  1. Leverage tools to get answers from user interactions
  2. do something

Implementation code

'use strict'
const inpuirer = require('inquirer')
const fs = require('fs')
/**
 * 命令行交互,根据用户的操作选择后续的操作
 * 
 */
inpuirer.prompt([
    {
    
    
        type:'input',
        name:'name',
        message:'Project name::'
    },
    {
    
    
        type:'confirm',
        name:'flag',
        message:'确定创建文件夹吗?'
    }
]).then(ans=>{
    
    
    const {
    
     name, flag } = ans
    if(!flag){
    
    
        return console.log(`用户主动取消创建${
      
      name}文件夹`)
    }
    !fs.existsSync(`./${
      
      name}`) && fs.mkdirSync(name)
})

Effect

before confirmation
insert image description here

after confirmation
insert image description here

  • You can see that there is one more folder.
    What happens if you enter n?
    No new folder will be created.
    insert image description here

::: tip

The simulation is over, thank you for your patience in reading! ! !

:::

Guess you like

Origin blog.csdn.net/qq_45704048/article/details/120997917