From 1422e2e1d2e9e0feff3aade469116725a5c047cf Mon Sep 17 00:00:00 2001 From: Su-Shing Chen Date: Sat, 15 Jun 2019 13:00:33 +1200 Subject: [PATCH] Add indent option --- gruntfile.js | 32 ++++++++++++++++++++++++++++++++ readme.md | 7 +++++++ tasks/concurrent.js | 21 +++++++++++++++++---- test/test.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/gruntfile.js b/gruntfile.js index 2a99108..ffb19bd 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -30,6 +30,34 @@ module.exports = grunt => { }, colors: [ 'colorcheck' + ], + indentTrue: { + options: { + indent: true + }, + tasks: [ + 'testIndent' + ] + }, + indentFalse: { + options: { + indent: false + }, + tasks: [ + 'testIndent' + ] + }, + indentFalseConcurrentOutput: { + options: { + logConcurrentOutput: true, + indent: false + }, + tasks: [ + 'testIndent' + ] + }, + indentDefault: [ + 'testIndent' ] }, simplemocha: { @@ -121,6 +149,10 @@ module.exports = grunt => { grunt.file.write('test/tmp/colors', supports); }); + grunt.registerTask('testIndent', () => { + console.log('indent test output'); + }); + grunt.registerTask('default', [ 'clean', 'concurrent:test', diff --git a/readme.md b/readme.md index b649462..89f34e3 100644 --- a/readme.md +++ b/readme.md @@ -76,3 +76,10 @@ grunt.registerTask('default', ['concurrent:target']); ``` *The output will be messy when combining certain tasks. This option is best used with tasks that don't exit like `watch` and `nodemon` to monitor the output of long-running concurrent tasks.* + +### indent + +Type: `boolean`
+Default: `true` + +You can optionally skip indenting the log output of your concurrent tasks by specifying the `indent` option as `false`. This can be useful for running tasks in parallel for a stdout parser which expects no indentation, e.g. TeamCity tests. diff --git a/tasks/concurrent.js b/tasks/concurrent.js index 7feaaf8..52afd5d 100644 --- a/tasks/concurrent.js +++ b/tasks/concurrent.js @@ -12,7 +12,8 @@ module.exports = grunt => { const done = this.async(); const options = this.options({ - limit: Math.max((os.cpus().length || 1) * 2, 2) + limit: Math.max((os.cpus().length || 1) * 2, 2), + indent: true }); const tasks = this.data.tasks || this.data; @@ -47,15 +48,27 @@ module.exports = grunt => { } }, (error, result) => { if (!options.logConcurrentOutput) { - grunt.log.writeln('\n' + indentString(result.stdout + result.stderr, 4)); + let output = result.stdout + result.stderr; + if (options.indent) { + output = indentString(output, 4); + } + + grunt.log.writeln('\n' + output); } next(error); }); if (options.logConcurrentOutput) { - subprocess.stdout.pipe(padStream(4)).pipe(process.stdout); - subprocess.stderr.pipe(padStream(4)).pipe(process.stderr); + let subStdout = subprocess.stdout; + let subStderr = subprocess.stderr; + if (options.indent) { + subStdout = subStdout.pipe(padStream(4)); + subStderr = subStderr.pipe(padStream(4)); + } + + subStdout.pipe(process.stdout); + subStderr.pipe(process.stderr); } subprocesses.push(subprocess); diff --git a/test/test.js b/test/test.js index efd0b3a..7875d42 100644 --- a/test/test.js +++ b/test/test.js @@ -71,4 +71,37 @@ describe('concurrent', () => { }); }); }); + + describe('`indent` option', () => { + const testOutput = 'indent test output'; + const indentedTestOutput = ' ' + testOutput; + + it('indents output when true', done => { + exec('grunt concurrent:indentTrue', (error, stdout) => { + assert.ok(stdout.split('\n').includes(indentedTestOutput)); + done(); + }); + }); + + it('does not indent output when false', done => { + exec('grunt concurrent:indentFalse', (error, stdout) => { + assert.ok(stdout.split('\n').includes(testOutput)); + done(); + }); + }); + + it('does not indent output when false and logConcurrentOutput is true', done => { + exec('grunt concurrent:indentFalseConcurrentOutput', (error, stdout) => { + assert.ok(stdout.split('\n').includes(testOutput)); + done(); + }); + }); + + it('indents output by default', done => { + exec('grunt concurrent:indentDefault', (error, stdout) => { + assert.ok(stdout.split('\n').includes(indentedTestOutput)); + done(); + }); + }); + }); });