Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indent option #101

Merged
merged 3 commits into from Jun 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions gruntfile.js
Expand Up @@ -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: {
Expand Down Expand Up @@ -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',
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 `false`. This can be useful for running tasks in parallel for a stdout parser which expects no indentation, for example, 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
33 changes: 33 additions & 0 deletions test/test.js
Expand Up @@ -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();
});
});
});
});