Javascript 复制模板文件后重命名文件
我正在基于yeoman的开发一个样本yeoman生成器。生成的默认Javascript 复制模板文件后重命名文件,javascript,visual-studio,yeoman,yeoman-generator,Javascript,Visual Studio,Yeoman,Yeoman Generator,我正在基于yeoman的开发一个样本yeoman生成器。生成的默认写入函数如下: generators.Base.extend({ writing: function () { this.fs.copyTpl( this.sourceRoot(), this.destinationRoot()); } }); 我的模板文件包含Visual Studio项目和解决方案文件,因此我希望重命名它们以匹配appName:
写入函数如下:
generators.Base.extend({
writing: function () {
this.fs.copyTpl(
this.sourceRoot(),
this.destinationRoot());
}
});
我的模板文件包含Visual Studio项目和解决方案文件,因此我希望重命名它们以匹配appName
:
generators.Base.extend({
writing: function () {
this.fs.copyTpl(
this.sourceRoot(),
this.destinationRoot(),
{ appname: this.props.appName });
// Rename directory to match 'appName'
this.fs.move(
this.destinationPath('foo'), // A directory
this.detinationPath(this.props.appName)); // From prompt when user types yo webapp
// Rename Visual Studio .sln file to match 'appName'
this.fs.move(
this.destinationPath('foo/bar.sln'),
this.destinationPath(this.props.appName + '/' + this.props.appName + '.sln');
// ...similar renames to Visual Studio .csproj files
}
});
但是,我在执行第一个移动的行中得到一个断言错误:
AssertionError: Trying to copy from a source that does not exist
我认为文件/目录复制是同步的,但在第一次调用this.fs.move
之前运行this.fs.exists(this.destinationRoot())
时,它返回false
。我怀疑这与this.fs
是一个实例有关,在writing
函数完成之前,目录/文件只存在于内存中
如果需要重命名文件/目录,该怎么办?如果在复制功能中使用glob模式,该方法是否有效
this.fs.copyTpl(
this.templatePath('**'),
this.destinationPath(),
{ appname: this.props.appName });
mem fs操作是同步的,如果文件真的被复制了,exist检查将通过。不会这一点。sourceRoot('**')
修改sourceRoot
?是的,我复制粘贴了您的代码。但是问题是混合了根函数和路径创建者。您需要使用this.templatePath()
和this.destinationPath()
听起来像您需要的吗?