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:
- Type the name of the tool
- answer a series of questions
- 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
- Leverage tools to get answers from user interactions
- 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
after confirmation
- You can see that there is one more folder.
What happens if you enter n?
No new folder will be created.
::: tip
The simulation is over, thank you for your patience in reading! ! !
:::