【问题标题】:Is it possible to run a task after the watch task?是否可以在监视任务之后运行任务?
【发布时间】:2013-07-17 21:27:11
【问题描述】:

我有一个无法在grunt-php 上运行的基于 PHP 的项目。相反,我使用 grunt-exec 运行我的 MAMP 服务器进行开发。

exec: {
  serverup: {
    command: '/Applications/MAMP/bin/start.sh'
  },
  serverdown: {
    command: '/Applications/MAMP/bin/stop.sh'
  }
}

在我的自定义开发任务中,我在监视任务之前运行 MAMP 启动脚本。然后,我在退出监视任务后尝试停止 MAMP 服务器。

grunt.registerTask('default', ['jshint', 'concat', 'compass:dev', 'exec:serverup', 'watch', 'exec:serverdown']);

但是,如果我使用 Ctrl-C 退出任务,exec:serverdown 任务似乎永远不会运行。有什么办法可以使这项工作?由于服务器永远不会关闭,因此该端口会被占用,直到我手动运行停止脚本,如果我在关闭默认任务之前尝试再次运行默认任务,则会出现错误。

如果没有,还有其他方法可以完成同样的事情吗?

【问题讨论】:

  • 您介意分享一下为什么它不能与 grunt-php 一起使用吗?
  • @SindreSorhus 我不太清楚。我正在为Koken 开发一个主题。我开始尝试使用 grunt-php,但无法完成安装。我在 Koken 论坛上发帖,一位开发人员告诉我它不能与内置的 PHP 服务器一起工作,这显然是 grunt-php 使用的。他没有说为什么。

标签: gruntjs grunt-contrib-watch


【解决方案1】:

您可以在SIGINT 上收听并运行脚本:

var exec = require('child_process').exec;
process.on('SIGINT', function () {
    exec('/Applications/MAMP/bin/stop.sh', function () {
        process.exit();
    });
});

module.exports = function (grunt) {};

【讨论】:

  • 感谢您的回答。看起来很有希望。我会尽快试用并报告。
  • 我遇到了一些麻烦。我不确定这应该在我的 Gruntfile 中的确切位置。如果我将它包含在 module.exports = function (grunt) {...} 中,我将无法再使用 Ctrl-C 结束任务。如果我把它放在那个函数之外,我会得到Fatal error: grunt is not defined
  • 这让我可以退出任务,但是当一切都说完了,服务器仍然在运行。我可以手动运行grunt exec:serverdown,但服务器确实宕机了。
  • 似乎 grunt.task.run 是异步的,没有回调,这使得它在运行任务之前运行 process.exit() 。我已经用另一个解决方案更新了答案。
猜你喜欢
  • 1970-01-01
  • 2016-10-10
  • 1970-01-01
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 2019-04-17
相关资源
最近更新 更多