是否可以在安装yeoman发生器后运行grunt命令?

Jav*_*eva 1 node.js gruntjs yeoman yeoman-generator

基本上我想grunt在我的生成器完成安装依赖项后运行,我发现你可以callbackinstallDependencies方法之后添加一个函数,以便在安装完所有内容之后运行:

this.on('end', function () {
    this.installDependencies({
        skipInstall: options['skip-install'],
        callback: function () {
            console.log('All done!');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

但是我不确定如何运行grunt任务(如去往终端并运行"grunt")

小智 5

在this.on('end')之后添加这一行

// Now you can bind to the dependencies installed event
this.on('dependenciesInstalled', function() {
    this.spawnCommand('grunt', ['build']);
});
Run Code Online (Sandbox Code Playgroud)

查看此主题以获取更多详细信息

但是,如果你正在使用最新的yeomen更新,你需要这样做

this.on('end', function () {
  if (!this.options['skip-install']) {
    this.npmInstall();
    this.spawnCommand('grunt', ['prepare']); // change 'prepare' with your task.
  }
});
Run Code Online (Sandbox Code Playgroud)