将 DIR 更改为从 Yeoman 生成器本身新创建的 Yeoman 项目

change dir to newly created yeoman project from yeoman generator itself

本文关键字:Yeoman 新创建 创建 项目 DIR      更新时间:2023-09-26

我想将一个项目脚手架到同一个命名目录中,在生成器完成其工作后,我希望消费者被重定向到该文件夹。

所以我接受项目名称作为参数:this.argument('name', { type: String, required: false });,然后创建该文件夹并更改destinationRoot

if (this.name) {
  mkdirp(this.name);
  this.destinationRoot(this.destinationPath(this.name));
}

毕竟,我想在runContext循环的最后阶段做一些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 完成它的工作,我仍然在 currrent 文件夹中,而不是在 meow 文件夹中。有人知道如何解决这个问题吗?

>Yeoman 不会更改用户的目录。

您可以使用process.chdir()手动执行此操作