plop 方便的micro-generator framework

plop是一个micro-generator framework,可以用来方便的生成代码

参考使用

  • 安装
    pacakge.json
 
{
  "name": "appdemo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "plop": "^2.7.4"
  },
  "scripts": {
    "app":"plop"
  }
}

plopfile.js

module.exports = function (plop) {
    // controller generator
    plop.setGenerator('controller', {
        description: 'application controller logic',
        prompts: [{
            type: 'input',
            name: 'name',
            message: 'controller name please'
        }],
        actions: [{
            type: 'add',
            path: 'src/{{name}}.js',
            templateFile: 'plop-templates/controller.hbs'
        }]
    });
};
  • 模板
    plop-templates/controller.hbs
 
module.exports = class {{name}} {
    constrcut(){
        this.name={{name}};
    }
    get name(){
      return this.name;
    }
}
  • 运行效果
yarn app
 

 

 

 

 

参考资料

https://www.npmjs.com/package/plop
https://github.com/plopjs/plop
https://plopjs.com/
https://github.com/handlebars-lang/handlebars.js

posted on 2021-02-19 12:25  荣锋亮  阅读(129)  评论(0编辑  收藏  举报

导航