上一篇讲了如何开始搭建脚手架,接下来讲说函数定义的规则和要点。npm
以前说了,若是将函数直接写,脚手架在构建的时候就会自动的调用,有时候咱们只是须要一个私有函数。有三个不一样的方法能够实现。函数
_method
。放在构造函数里。this
generators.Base.extend({ init: function () { this.helperMethod = function () { console.log('won\'t be called automatically'); }; } });
用继承的方法。命令行
var MyBase = generators.Base.extend({ helper: function () { console.log('won\'t be called automatically'); } }); module.exports = MyBase.extend({ exec: function () { this.helper(); } });
官方提到了能够这样进行合并。code
generators.Base.extend({ priorityName: { method: function () {}, method2: function () {} } });
对于那些会自动执行的函数,他们是有一个优先顺序的,下面这些函数是安顺序的一个一个执行的。继承
initializing
- 你的初始化函数,就是构造函数,主要就是检查一下参数什么的prompting
- 给用户展现你的菜单,选点东西什么的configuring
- 保存配置信息,建立相似.editorconfig
的文件default
- 就是默认,只要不在这个列表里的函数都在这个位置执行writing
- 建立模板文件conflicts
- 处理异常和冲突install
- 装npm
和bower
依赖什么的end
- 打个命令行祝贺使用者成功了这节有点短,但单独领出来感受仍是颇有必要,理顺了大的流程。欢迎勘误和提问。bower