我的 Yeoman 生成器将文件从模板复制到目标路径:
this.fs.copyTpl(
this.templatePath(),
this.destinationPath(), {
appName: this.props.appName
});
在项目生成过程中,我需要分配this.props.appName
到一些文件名。
不幸的是,我不能像在这个文件中那样做:
<%=appName%>-project.sln
所有需要重命名的文件都有appTemplate
在他们的名字中,所以我需要做的就是简单地替换appTemplate
值为this.props.appName
.
我可以以某种方式配置吗copyTpl
在将某些文件复制到另一个目的地时重命名它们?
好的,我找到了解决方案。根据自耕农文档:
任何生成器作者都可以注册一个transformStream来修改文件路径和/或内容。
使用此方法:
this.registerTransformStream();
这意味着我可以通过某个脚本传输所有生成的文件:
var rename = require("gulp-rename");
//other dependecies...
module.exports = yeoman.Base.extend({
//some other things generator do...
writing: function() {
var THAT = this;
this.registerTransformStream(rename(function(path) {
path.basename = path.basename.replace(/(666replacethat666)/g, THAT.props.appName);
path.dirname = path.dirname.replace(/(666replacethat666)/g, THAT.props.appName);
}));
this.fs.copyTpl(
this.templatePath(),
this.destinationPath(), {
appName: this.props.appName
});
}
});
该脚本将通过管道传输所有文件gulp重命名,改变666replacethat666
到更智能的东西。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)