从 yeoman 生成器本身将目录更改为新创建的 yeoman 项目
change dir to newly created yeoman project from yeoman generator itself
我想将一个项目搭建到同名目录中,在生成器完成其工作后,我希望将使用者重定向到该文件夹。
所以我接受项目名称作为参数:this.argument('name', { type: String, required: false });
然后创建该文件夹并将 destinationRoot
更改为它:
if (this.name) {
mkdirp(this.name);
this.destinationRoot(this.destinationPath(this.name));
}
毕竟我想在 runContext cycle 的最后阶段制作有点 cd this.name
。我尝试将 destinationRoot
更改为父目录,然后将 cd
更改为它,它应该可以工作,但它没有:
end: function () {
if (this.name) {
console.log('BEFORE', ls('.'))
this.destinationRoot('..');
console.log('AFTER', ls('.'))
cd(this.name);
}
},
这是日志:
BEFORE [ 'README.md', 'index.js', 'package.json', 'test.js' ]
AFTER [ 'meow' ]
但是它不起作用,因为一旦 yeoman 完成它的工作,我仍然在当前文件夹中,而不是在 meow
文件夹中。有人知道如何解决吗?
Yeoman 不会为用户更改目录。
您可以使用 process.chdir()
手动完成
我想将一个项目搭建到同名目录中,在生成器完成其工作后,我希望将使用者重定向到该文件夹。
所以我接受项目名称作为参数:this.argument('name', { type: String, required: false });
然后创建该文件夹并将 destinationRoot
更改为它:
if (this.name) {
mkdirp(this.name);
this.destinationRoot(this.destinationPath(this.name));
}
毕竟我想在 runContext cycle 的最后阶段制作有点 cd this.name
。我尝试将 destinationRoot
更改为父目录,然后将 cd
更改为它,它应该可以工作,但它没有:
end: function () {
if (this.name) {
console.log('BEFORE', ls('.'))
this.destinationRoot('..');
console.log('AFTER', ls('.'))
cd(this.name);
}
},
这是日志:
BEFORE [ 'README.md', 'index.js', 'package.json', 'test.js' ]
AFTER [ 'meow' ]
但是它不起作用,因为一旦 yeoman 完成它的工作,我仍然在当前文件夹中,而不是在 meow
文件夹中。有人知道如何解决吗?
Yeoman 不会为用户更改目录。
您可以使用 process.chdir()