我正在尝试创建一个简单的生成器来标准化节点项目的创建。在我的生成器中,我调用了copyTpl,如:
generator.fs.copyTpl(
sourcePath,
destinationPath
context,
//ignore ES6 constructs in our templates
{
interpolate: /<%=([\s\S]+?)%>/g
}
);
只要目标目录中不存在任何文件,这就很有效。不幸的是,如果文件存在并且我选择覆盖它,我会收到以下错误:
Error: no writecb in Transform class
at afterTransform (/Users/gerrard00/projects/generator-mine/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:95:33)
at TransformState.afterTransform (/Users/gerrard00/projects/generator-mine/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:79:12)
at Object.callback (/Users/gerrard00/projects/generator-mine/node_modules/yeoman-generator/lib/base.js:787:7)
at /Users/gerrard00/projects/generator-mine/node_modules/yeoman-generator/lib/util/conflicter.js:79:18
at Conflicter.<anonymous> (/Users/gerrard00/projects/generator-mine/node_modules/yeoman-generator/lib/util/conflicter.js:193:12)
at PromptUI.onCompletion (/usr/local/opt/nvm/versions/node/v5.0.0/lib/node_modules/yo/node_modules/inquirer/lib/ui/prompt.js:57:10)
at AnonymousObserver.Rx.AnonymousObserver.AnonymousObserver.completed (/usr/local/opt/nvm/versions/node/v5.0.0/lib/node_modules/yo/node_modules/rx-lite/rx.lite.js:1550:12)
at AnonymousObserver.Rx.internals.AbstractObserver.AbstractObserver.onCompleted (/usr/local/opt/nvm/versions/node/v5.0.0/lib/node_modules/yo/node_modules/rx-lite/rx.lite.js:1489:14)
at Subject.Rx.Subject.addProperties.onCompleted (/usr/local/opt/nvm/versions/node/v5.0.0/lib/node_modules/yo/node_modules/rx-lite/rx.lite.js:5871:19)
at Subject.tryCatcher (/usr/local/opt/nvm/versions/node/v5.0.0/lib/node_modules/yo/node_modules/rx-lite/rx.lite.js:63:31)
这意味着我的生成器仅适用于空文件夹。我已经重新阅读了“创建一个生成器”文档大约二十次,但我无法弄清楚我错过了什么。我需要添加到我的生成器以防止此错误?
更新:尽管出现错误,文件仍会被覆盖。
答案 0 :(得分:1)
事实证明问题在于我的代码使用node-walk包异步地遍历目录。调用方法使用生成器异步方法,但同步调用完成回调。我修改它以在walk end事件处理程序中调用done回调,错误就消失了。