如何在我的Yeoman发电机完成安装后运行Grunt任务? [英] How to run a Grunt task after my Yeoman generator finishes installing?

查看:87
本文介绍了如何在我的Yeoman发电机完成安装后运行Grunt任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个定制的Yeoman生成器,它安装了许多预处理的语言编译器,如CoffeeScript,LESS和Jade。在我的生成器创建的Gruntfile中,我有一个编译任务的编译任务。但是,在构建任务至少运行一次之前,已编译的HTML,CSS和Javascript文件不存在,如果我尝试在新建脚手架后运行grunt watch / connect服务器,这可能会造成混淆。



让我的发电机在安装结束时运行Grunt构建步骤的最佳方式是什么?已经用于调用 this.installDependencies end 事件似乎是正确的地方,但应该怎么做我与Grunt沟通?

解决方案

如果您遵循堆栈, this.installDependencies 最终会按照 https:// github .com / yeoman / generator / blob / 45258c0a48edfb917ecf915e842b091a26d17f3e / lib / actions / install.js#L36

  this .spawnCommand(installer,args,cb)
.on('error',cb)
.on('exit',this.emit.bind(this,installer +'Install:end',paths ))
.on('exit',function(err){
if(err === 127){
this.log.error('Could not find'+ installer +' 。请用'+
'`npm install -g'+安装程序+'`。');
}
cb(err);
} .bind(this));

进一步追踪这个 this.spawnCommand 来自 https://github.com/yeoman/generator/blob /master/lib/actions/spawn_command.js

  var spawn = require('child_process')。spawn ; 
var win32 = process.platform ==='win32';

/ **
*跨操作系统规范一个命令并产生它。
*
* @param {String}命令
* @param {Array}参数
* /

module.exports = function spawnCommand(command, args){
var winCommand = win32? 'cmd':命令;
var winArgs = win32? ['/c'].concat(command,args):args;

返回spawn(winCommand,winArgs,{stdio:'inherit'});
};

换句话说,在您的Generator的代码中,您可以调用 this.spawnCommand ,然后将希望终端运行的参数传递给它。如 this.spawnCommand('grunt',['build'])



问题在于你把它放在哪里?线性思考,你只能相信 grunt build 在所有依赖关系安装完毕后才能工作。

From https://github.com/yeoman/generator/blob/45258c0a48edfb917ecf915e842b091a26d17f3e/lib/actions/install.js#L67-69
this.installDependencies 接受回调,所以你的代码可能如下所示:

  this.on('end' ,函数(){
this.installDependencies({
skipInstall:this.options ['skip-install'],
callback:function(){
this.spawnCommand(' grunt',['build']);
} .bind(this)//将回调绑定到父范围
});
});

试试吧!如果一切顺利,您应该在新的 this.spawnCommand 调用之上添加一些错误处理以保证安全。


I'm building a custom Yeoman generator that installs a lot of pre-processed language compilers like CoffeeScript, LESS and Jade. In the Gruntfile that my generator creates I have a build task which compiles everything. However, until that build task is run at least once, the compiled HTML, CSS and Javascript files don't exist, which can be confusing if I try to run the grunt watch/connect server after freshly scaffolding.

What is the best way to have my generator run that Grunt build step at the end of the installation? The end event that's already being used to call this.installDependencies seems like the right place to do that, but how should I communicate with Grunt?

解决方案

If you follow the stack, this.installDependencies eventually works its way down to https://github.com/yeoman/generator/blob/45258c0a48edfb917ecf915e842b091a26d17f3e/lib/actions/install.js#L36:

this.spawnCommand(installer, args, cb)
  .on('error', cb)
  .on('exit', this.emit.bind(this, installer + 'Install:end', paths))
  .on('exit', function (err) {
    if (err === 127) {
      this.log.error('Could not find ' + installer + '. Please install with ' +
                          '`npm install -g ' + installer + '`.');
    }
    cb(err);
  }.bind(this));

Chasing this down further, this.spawnCommand comes from https://github.com/yeoman/generator/blob/master/lib/actions/spawn_command.js:

var spawn = require('child_process').spawn;
var win32 = process.platform === 'win32';

/**
 * Normalize a command across OS and spawn it.
 *
 * @param {String} command
 * @param {Array} args
 */

module.exports = function spawnCommand(command, args) {
  var winCommand = win32 ? 'cmd' : command;
  var winArgs = win32 ? ['/c'].concat(command, args) : args;

  return spawn(winCommand, winArgs, { stdio: 'inherit' });
};

In other words, in your Generator's code, you can call this.spawnCommand anytime, and pass it the arguments you wish the terminal to run. As in, this.spawnCommand('grunt', ['build']).

So then the next question is where do you put that? Thinking linearly, you can only trust that grunt build will work after all of your dependencies have been installed.

From https://github.com/yeoman/generator/blob/45258c0a48edfb917ecf915e842b091a26d17f3e/lib/actions/install.js#L67-69, this.installDependencies accepts a callback, so your code might look like this:

this.on('end', function () {
  this.installDependencies({
    skipInstall: this.options['skip-install'],
    callback: function () {
      this.spawnCommand('grunt', ['build']);
    }.bind(this) // bind the callback to the parent scope
  });
});

Give it a shot! If all goes well, you should add some error handling on top of that new this.spawnCommand call to be safe.

这篇关于如何在我的Yeoman发电机完成安装后运行Grunt任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆