我遇到了一个有趣的问题,在任务期间写入终端的某些 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”时,我看到弹出两个浏览器窗口,正如我所料。通常在第一次运行时,我会从终端获得想要查看的输出:
在 $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 任务都会擦除终端的最后一行并将其替换为当前进度。有什么建议你们都必须控制这种行为,或者以不同的方式设置我的繁重任务以避免问题?
干杯!