在 Yeoman 复制模板文件后重命名文件
Renaming files after Yeoman copies template files
我正在研究基于 Yeoman 的 generator-generator 的示例 yeoman 生成器。默认生成的writing
函数是这样的:
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
}
});
但是,我在执行第一个 move
:
的那一行得到一个断言错误
AssertionError: Trying to copy from a source that does not exist
我认为 file/directory 复制是同步的,但是当我 运行 this.fs.exists(this.destinationRoot())
在第一次调用 this.fs.move
之前 returns false
.我怀疑这与 this.fs
是 mem-fs 的实例有关,其中 directory/files 仅存在于内存中,直到 writing
函数完成。
如果我需要重命名files/directories怎么办?
如果在复制函数中使用 glob 模式是否有效?
this.fs.copyTpl(
this.templatePath('**'),
this.destinationPath(),
{ appname: this.props.appName });
mem-fs 操作是同步的,如果文件真的被复制过来,存在检查将通过。
我正在研究基于 Yeoman 的 generator-generator 的示例 yeoman 生成器。默认生成的writing
函数是这样的:
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
}
});
但是,我在执行第一个 move
:
AssertionError: Trying to copy from a source that does not exist
我认为 file/directory 复制是同步的,但是当我 运行 this.fs.exists(this.destinationRoot())
在第一次调用 this.fs.move
之前 returns false
.我怀疑这与 this.fs
是 mem-fs 的实例有关,其中 directory/files 仅存在于内存中,直到 writing
函数完成。
如果我需要重命名files/directories怎么办?
如果在复制函数中使用 glob 模式是否有效?
this.fs.copyTpl(
this.templatePath('**'),
this.destinationPath(),
{ appname: this.props.appName });
mem-fs 操作是同步的,如果文件真的被复制过来,存在检查将通过。