使用 copyTpl 复制整个文件夹时重命名单个文件名 [英] Renaming single file names while copying entire folder using copyTpl
问题描述
我的 yeoman 生成器将文件从模板复制到目标路径:
My yeoman generator copies files from template to destination path:
this.fs.copyTpl(
this.templatePath(),
this.destinationPath(), {
appName: this.props.appName
});
在项目生成过程中,我需要将this.props.appName
的值赋给一些文件名.
During project generation, I need to assign value of this.props.appName
to some of filenames.
不幸的是,我不能像在这些文件中那样这样做:
Unfortunately I can't do this that way like I could do inside this files:
<%=appName%>-project.sln
所有需要重命名的文件的名称中都有appTemplate
,所以我需要做的就是将appTemplate
替换为this.props 的值.应用名称
.
All files that need to be renamed have appTemplate
in their names, so what I need to do is simply replace appTemplate
with value of this.props.appName
.
我可以以某种方式配置 copyTpl
以在将某些文件复制到另一个目标时重命名它们吗?
Can I somehow configure copyTpl
to rename some of files while copying them to another destination?
推荐答案
好的,我找到了解决方案.根据 yeoman 文档:
OK, I found a solution. According to yeoman docs:
任何生成器作者都可以注册一个transformStream来修改文件路径和/或内容.
Any generator author can register a transformStream to modify the file path and/or the content.
使用此方法:
this.registerTransformStream();
这意味着我可以通过一些脚本来管理所有生成的文件:
What that means is I can pipe all generated files through some script:
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-rename 管道所有文件,更改 666replacethat666
更智能的东西.
This script will pipe all files through gulp-rename, changing 666replacethat666
to something more intelligent.
这篇关于使用 copyTpl 复制整个文件夹时重命名单个文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!