Grunt并发任务会覆盖其他任务的日志记录

时间:2014-04-24 20:22:20

标签: javascript node.js gruntjs karma-runner grunt-concurrent

我遇到了一个有趣的问题,似乎在任务期间写入终端的某些grunt任务似乎在并发运行时会相互覆盖。例如:

我尝试同时运行两个任务,一个用于单元测试,另一个用于e2e测试。两者都应该自动调用我正在处理的文件,因此我在Gruntfile.js中有以下内容(这些只是有趣的内容:

Gruntfile.js:

concurrent: {
  options: { logConcurrentOutput: true },
  server: [
    'copy:styles'
  ],
  test: [
    'copy:styles'
  ],
  dist: [
    'copy:styles',
    'imagemin',
    'svgmin'
  ],
  continuous: {
      tasks: [
                'karma:unit_auto',
                'karma:e2e_auto'
              ]
  }
},
karma: {
        unit: {
            configFile: 'karma.conf.js',
            singleRun: true
        },
        unit_auto: {
            configFile: 'karma.conf.js',
            autoWatch: true,
            singleRun: false
        },
        e2e: {
            configFile: 'karma-e2e.conf.js',
            singleRun: true
        },
        e2e_auto: {
            configFile: 'karma-e2e.conf.js',
            autoWatch: true,
            singleRun: false
        }
    }
////////// and later in the file....... ///////////
grunt.registerTask('continuous', [
    'clean:server',
    'concurrent:continuous'
]);

当我跑步时,连续不断地哼着从命令行,我看到弹出两个浏览器窗口,正如我所期待的那样。通常在第一次运行时,我从终端获得我希望看到的输出:

初次运行$ grunt continuous:

user@myMachine:/path/to/working/directory$ grunt continuous
Running "clean:server" (clean) task

Running "concurrent:continuous" (concurrent) task
Running "karma:e2e_auto" (karma) task
Running "karma:unit_auto" (karma) task
INFO [karma]: Karma v0.12.10 server started at http://localhost:9999/
WARN [karma]: Port 9998 in use
INFO [launcher]: Starting browser Chrome
WARN [karma]: Port 9999 in use
INFO [karma]: Karma v0.12.10 server started at http://localhost:10000/_e2e/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 33.0.1750 (Linux)]: Connected on socket raGtr-40ZhPP7nCFXwvB with id 67817341
INFO [Chrome 33.0.1750 (Linux)]: Connected on socket IM2e8gy38Np1dL4fXwuy with id 77648855
Chrome 33.0.1750 (Linux): Executed 4 of 4 SUCCESS (0.11 secs / 0.104 secs)
Chrome 33.0.1750 (Linux): Executed 1 of 1 SUCCESS (0.206 secs / 0.167 secs)

然而,当我更改(并保存)观看的文件时,我开始看到奇怪的结果。请注意,反复保存同一文件通常会为并发任务提供不同的输出:

一遍又一遍地保存相同的文件:

INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
Chrome 33.0.1750 (Linux): Executed 4 of 4 SUCCESS (0.114 secs / 0.103 secs)
Chrome 33.0.1750 (Linux): Executed 1 of 1 SUCCESS (0.159 secs / 0.12 secs)
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
Chrome 33.0.1750 (Linux): Executed 0 of 4 SUCCESS (0 secs / 0 secs)
Chrome 33.0.1750 (Linux): Executed 1 of 1 SUCCESS (0.181 secs / 0.148 secs)
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
Chrome 33.0.1750 (Linux): Executed 0 of 4 SUCCESS (0 secs / 0 secs)
Chrome 33.0.1750 (Linux): Executed 1 of 1 SUCCESS (0.141 secs / 0.12 secs)
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
Chrome 33.0.1750 (Linux): Executed 0 of 4 SUCCESS (0 secs / 0 secs)
Chrome 33.0.1750 (Linux): Executed 1 of 1 SUCCESS (0.241 secs / 0.219 secs)
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/app.js".
Chrome 33.0.1750 (Linux): Executed 0 of 1 SUCCESS (0 secs / 0 secs)
Chrome 33.0.1750 (Linux): Executed 1 of 1 SUCCESS (0.253 secs / 0.227 secs)
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/controllers/mmsLandingPageCtrl.js".
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/controllers/mmsLandingPageCtrl.js".
Chrome 33.0.1750 (Linux): Executed 3 of 4 SUCCESS (0 secs / 0.091 secs)
Chrome 33.0.1750 (Linux): Executed 1 of 1 SUCCESS (0.208 secs / 0.151 secs)
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/controllers/mmsLandingPageCtrl.js".
INFO [watcher]: Changed file "/var/www/web-tcad/app/scripts/controllers/mmsLandingPageCtrl.js".
Chrome 33.0.1750 (Linux): Executed 0 of 4 SUCCESS (0 secs / 0 secs)
Chrome 33.0.1750 (Linux): Executed 1 of 1 SUCCESS (0.249 secs / 0.218 secs)

我认为这里的问题源于日志记录实际写入终端的方式......即,我认为每次运行测试时,grunt任务会擦除终端的最后一行并将其替换为目前的进展。是否有任何建议您必须控制此行为,或以不同方式设置我的grunt任务以避免此问题?

干杯!

1 个答案:

答案 0 :(得分:0)

在我的业力咕噜声设置中,我实际上使用grunt-contrib-watch并从观察中运行业力任务。我实际上在构建设置中有其他步骤,因此从监视任务运行它是非常有帮助的。

你可能想要这样的东西:

karma: {
  e2e: {
    configFile: 'karma-e2e.conf.js',
    background: true
  },
  unit: {
    configFile: 'karma.conf.js',
    background: true
  }
},
watch: {
  karma_e2e: {
    files: [<<list of your files to watch for e2e>>],
    tasks: ['karma:e2e:run'],
    options: {
      atBegin: true,
      spawn: false    // necessary to preserve console output
    }
  },
  karma_unit: {
    files: [<<list of your files to watch for unit>>],
    tasks: ['karma:unit:run'],
    options: {
      atBegin: true,
      spawn: false    // necessary to preserve console output
    }
  }
}

grunt.registerTask('continuous', [
  'clean:server',
  'watch'
]);