yeoman不能编辑由其他子生成器创build的XML文件

我试图在一个由另一个创build的子生成器中对XML文件进行更改。

我的主生成器执行提示并确定应使用哪个子生成器。 简单的看起来像这样:

var MainGenerator = module.exports = yeoman.generators.Base.extend({ writing: function () { this.composeWith('design:setup', {}); if (this.option.get('someOption')) { this.composeWith('design:extend', {}); } } }); 

设置生成器会添加一些在devise的每个变体中使用的文件。 例如一个项目config.xml

 var SetupGenerator = module.exports = yeoman.generators.Base.extend({ default: function () { // ^ default: makes sure the vsSetup is run before the writing action // of the other sub generators this.fs.copy( this.templatePath( 'project/_config.xml' ), this.destinationPath( 'project/config.xml' ) ); }); 

现在,根据用户在提示中select的设置,执行不同的子生成器。 当他们向目的地添加新文件夹时,必须在由设置生成器创build的config.xml中进行更新。

 var xml2js = require('xml2js'); var MainGenerator = module.exports = yeoman.generators.Base.extend({ writing: function () { var xmlParser = new xml2js.Parser(); this.fs.read( 'project/config.xml', function (err, data) { console.log('read file'); console.dir(err); console.dir(data); xmlParser.parseString(data, function (err, result) { console.log('parsed xml: ' + 'project/config.xml' ); console.dir(result); console.dir(err); }); }); } }); 

从fs读取完全没有输出。 没有错误,没有什么。 任何想法我在这里做错了吗?

因为扩展生成器有不同的组合,所以我希望每个生成器都注册它需要的文件夹,而不是在原始xml文件中有一个不可维护的if else语句的地狱。

我还没有find文件系统或composeWith函数发出的任何事件的文档,但可以挂钩到end事件中,然后读取文件。

 this.composeWith('design:extend', {}) .on('end', function () { console.log(this.fs.read('path/to/file')); // do your file manipulation here }); 

这并不是最好的方法,因为它在将文件提交到磁盘修改文件,而不是在内存编辑器中进行修改,但这至less是一个很好的开始。