gpt4 book ai didi

javascript - 在 Yeoman 复制模板文件后重命名文件

转载 作者:行者123 更新时间:2023-11-29 10:36:38 25 4
gpt4 key购买 nike

我正在开发一个基于 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

我认为文件/目录复制是同步的,但是当我运行 this.fs.exists(this.destinationRoot()) 时在第一次调用 this.fs.move 之前它返回 false .我怀疑这与 this.fs 有关作为 mem-fs 的实例目录/文件只存在于内存中,直到 writing功能完成。

如果我需要重命名文件/目录,我该怎么办?

最佳答案

如果在复制函数中使用 glob 模式是否有效?

this.fs.copyTpl(
this.templatePath('**'),
this.destinationPath(),
{ appname: this.props.appName });

mem-fs 操作是同步的,如果文件真的被复制过来,存在检查将通过。

关于javascript - 在 Yeoman 复制模板文件后重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35484645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com