plopfile.js功能类似于像创建一个xx.html的空模板,模板里面的有固定的初始化数据。
安装:
npm i -g plop@2.5.0 //3.x是typeScript的
根目录创建:plopfile.js
module.exports = function(plop) { // controller generator plop.setGenerator('controller', { description: '控制器', prompts: [ { //第一个操作 type: 'input', //输入一个值 name: 'name', //变量名是name message: 'controller name please' }, { //第二个操作 type: 'checkbox', //多选 name: 'blocks', //数组变量名是 blocks:['state','mutations','actions'] message: 'Blocks:', choices: [ { name: 'state', //外显示名字 value: 'state', checked: true //默认选中 }, { name: 'mutations2', value: 'mutations', checked: true }, { name: 'actions3', value: 'actions', checked: true } ], validate(value) { //验证是否必填 if (!value.includes('state') || !value.includes('mutations')) { return 'store require at least state and mutations' } return true } } ], actions(data) { console.log(data) //之前操作获取到的数据 { name: '00', blocks: [ 'state', 'mutations', 'actions' ] } const name = '{{name}}' //第一个操作 设置的变量 const actions = [ { type: 'add', path: `src/${name}.vue`, templateFile: 'plop-templates/controller.hbs' } ] return actions//返回数组 } }); };
https://plopjs.com/documentation/
官网githun:https://github.com/plopjs/plop/tree/v2.5.0
更多有关prompts参数设置可以参考:https://github.com/SBoudrias/Inquirer.js/
下载文件plop,解压把文件都放在根目录,之后执行npm run new即可。