Skip to content

Commit

Permalink
Add indent option
Browse files Browse the repository at this point in the history
  • Loading branch information
Shingyx committed Jun 15, 2019
1 parent 468aec3 commit 332a75e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
23 changes: 23 additions & 0 deletions gruntfile.js
Expand Up @@ -30,6 +30,25 @@ module.exports = grunt => {
},
colors: [
'colorcheck'
],
indentTrue: {
options: {
indent: true
},
tasks: [
'testIndent'
]
},
indentFalse: {
options: {
indent: false
},
tasks: [
'testIndent'
]
},
indentDefault: [
'testIndent'
]
},
simplemocha: {
Expand Down Expand Up @@ -121,6 +140,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',
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -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`<br>
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.
21 changes: 17 additions & 4 deletions tasks/concurrent.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Expand Up @@ -71,4 +71,30 @@ 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('indents output by default', done => {
exec('grunt concurrent:indentDefault', (error, stdout) => {
assert.ok(stdout.split('\n').includes(indentedTestOutput));
done();
});
});
});
});

0 comments on commit 332a75e

Please sign in to comment.