我正在尝试更改一个子生成器中的XML文件,该生成器是由另一个子生成器创建的。
我的主生成器执行提示,并确定应该使用哪些子生成器。简化后的代码如下所示:
var MainGenerator = module.exports = yeoman.generators.Base.extend({
writing: function () {
this.composeWith('design:setup', {});
if (this.option.get('someOption')) {
this.composeWith('design:extend', {});
}
}
});设置生成器添加了一些在设计的每个变体中使用的文件。例如,项目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' )
);
});现在,根据用户在提示中选择的设置,将执行不同的子生成器。当他们每次向目标添加一个新文件夹时,必须在由设置生成器创建的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);
});
});
}
});文件系统读取根本没有输出。没有错误,什么都没有。你知道我哪里做错了吗?
因为扩展生成器有不同的组合,所以我想让每个生成器注册它需要的文件夹,而不是让原始if else文件中的xml语句变成难以维护的地狱。
发布于 2016-04-19 02:36:30
我还没有找到太多关于文件系统或composeWith函数发出的事件的文档,但是您可以挂接到end事件并读取文件。
this.composeWith('design:extend', {})
.on('end', function () {
console.log(this.fs.read('path/to/file'));
// do your file manipulation here
});这不是最好的方法,因为它是在文件提交到磁盘之后修改的,而不是在内存编辑器中,但这至少是一个好的开始。
https://stackoverflow.com/questions/33285405
复制相似问题