我正在构建一个自定义的 yeoman 生成器,所以当需要创建文件时,它们会在我当前位置上方的一个目录中创建,或者在 ..
中创建,例如,如果我运行:
yo koala
从 /home/diegoaguilar/koala
文件将在 /home/diegoaguilar
创建。我应该如何告诉生成器复制文件的路径?我真的认为那将是 process.cwd()
或者只是运行 yeoman 生成器的地方。
这是我得到的文件生成代码:
writing: {
app: function () {
this.fs.copyTpl(
this.templatePath('_package.json'),
this.destinationPath('package.json'),
{appname: this.appname}
);
this.fs.copy(
this.templatePath('_bower.json'),
this.destinationPath('bower.json')
);
},
projectfiles: function () {
this.fs.copy(
this.templatePath('editorconfig'),
this.destinationPath('.editorconfig')
);
this.fs.copy(
this.templatePath('jshintrc'),
this.destinationPath('.jshintrc')
);
}
},
最佳答案
首先,我发现使用 yeoman
的 this.template()
比使用 this.fs.copy() 更容易
/this.fs.copyTpl()
来自 mem-fs-editor
的实例, 但是 YMMV
无论如何,您需要设置 this.sourceRoot('rel/path/to/source/root')
和 this.destinationRoot('rel/path/to/dest/root')
在尝试编写之前在生成器中添加,以确保您设置了正确的模板和目标上下文。 See yeoman's getting started guide on interacting with the files system from more information . this.destinationRoot()
应该相对于当前项目的根目录定义(我在下面解释),而 this.sourceRoot()
应该相对定义到生成器文件的根目录。
您还必须考虑到,yeoman 会尝试找出您当前在命令行中使用的任何应用程序的根目录。它通过向上导航(即 /home/diegoaguilar/koala
-> /home/diegoaguilar/
)直到找到 .yo-rc.json
文件。然后,Yeoman 将最近的 .yo-rc.json
目录作为您的项目的预期根目录。
在您的情况下,您可能想要删除/移动/重命名 /home/diegoaguilar/.yo-rc.json
(如果存在)。然后你可以创建你希望项目所在的目录并在那里运行生成器。这看起来像
/home/diegoaguilar/ $> mkdir koala
/home/diegoaguilar/ $> cd koala
/home/diegoaguilar/koala/ $> yo koala
如果你想或需要将你的 /home/diegoaguilar/.yo-rc.json
留在那里,你应该设置 this.destinationRoot()
在相对于 /home/diegoaguilar/
的生成器中,因此要写入 /home/diegoaguilar/koala/
您将使用 this.destinationRoot('koala')
。
关于javascript - 如何为自定义 yeoman 生成器文件设置目标路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33504316/