【问题标题】:Output when watching CoffeeScript files from a cakefile task从 cakefile 任务中查看 CoffeeScript 文件时的输出
【发布时间】:2011-06-16 17:28:16
【问题描述】:

我想做一个 Cakefile 任务来查看一些 CoffeeScript 文件,就像我运行 coffee -c -w js/*.coffee 一样。

它成功地观察和重新编译它们,但是当出现编译错误时,它不会将通常的输出记录到终端,就像我只是从终端运行脚本一样。知道如何实现吗?

exec = require('child_process').exec

task 'watch','watch all files and compile them as needed', (options) ->
    exec 'coffee -c -w js/*.coffee', (err,stdout, stderr) ->
        console.log stdout

另外,如果有比运行 'exec' 更好的方法从 cakefile 调用 coffeescript 命令,请也发布。

【问题讨论】:

    标签: coffeescript


    【解决方案1】:

    我已经使用 spawn 来解决这个问题,这里是一个示例蛋糕文件:

    {spawn, exec} = require 'child_process'
    
    option '-p', '--prefix [DIR]', 'set the installation prefix for `cake install`'
    
    task 'build', 'continually build with --watch', ->
        coffee = spawn 'coffee', ['-cw', '-o', 'lib', 'src']
        coffee.stdout.on 'data', (data) -> console.log data.toString().trim()
    

    您可以在 docco 项目中看到它的实际效果: https://github.com/jashkenas/docco/blob/master/Cakefile

    【讨论】:

      【解决方案2】:

      您的原始代码的问题是exec 仅在子进程终止后调用其回调一次。 (Node 文档对此并不太清楚。)因此,您应该尝试而不是定义该回调,而是尝试

      child = exec 'coffee -c -w js/*.coffee'
      child.stdout.on 'data', (data) -> sys.print data
      

      让我知道这是否适合你。

      【讨论】:

      • sys 未定义,我使用 console.log 而不是打印:child.stdout.on 'data', (data) -> console.log data
      【解决方案3】:

      spawn 而不是exec?

      {spawn} = require 'child_process'
      
      task 'watch', -> spawn 'coffee', ['-cw', 'js'], customFds: [0..2]
      

      【讨论】:

      • customFds 已弃用:nodejs.org/api/child_process.html。丹尼尔的解决方案对我来说就像一个魅力。
      • 你也可以用stdio: 'inherit'代替customFds: [0..2]
      猜你喜欢
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      相关资源
      最近更新 更多