Skip to content

Commit

Permalink
feat(misc): add color option to run-commands
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Jan 5, 2020
1 parent 6bcffed commit 8eea4b3
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
8 changes: 8 additions & 0 deletions docs/angular/api-workspace/builders/run-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ Type: `string`

Extra arguments. You can pass them as follows: ng run project:target --args='--wait=100'. You can them use {args.wait} syntax to interpolate them in the workspace config file.

### color

Default: `false`

Type: `boolean`

Use colors when showing output of command

### commands

Type: `array` of `object`
Expand Down
8 changes: 8 additions & 0 deletions docs/react/api-workspace/builders/run-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Type: `string`

Extra arguments. You can pass them as follows: ng run project:target --args='--wait=100'. You can them use {args.wait} syntax to interpolate them in the workspace config file.

### color

Default: `false`

Type: `boolean`

Use colors when showing output of command

### commands

Type: `array` of `object`
Expand Down
8 changes: 8 additions & 0 deletions docs/web/api-workspace/builders/run-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Type: `string`

Extra arguments. You can pass them as follows: ng run project:target --args='--wait=100'. You can them use {args.wait} syntax to interpolate them in the workspace config file.

### color

Default: `false`

Type: `boolean`

Use colors when showing output of command

### commands

Type: `array` of `object`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { readFileSync } from 'fs';
import { TestingArchitectHost } from '@angular-devkit/architect/testing';
import { Architect } from '@angular-devkit/architect';
import { join } from 'path';
import { TEN_MEGABYTES } from '@nrwl/workspace/src/core/file-utils';

function readFile(f: string) {
return readFileSync(f)
Expand Down Expand Up @@ -213,4 +214,49 @@ describe('Command Runner Builder', () => {
expect(result).toEqual(jasmine.objectContaining({ success: true }));
expect(readFile(f)).toEqual('value');
});

describe('--color', () => {
it('should set FORCE_COLOR=true', async () => {
const exec = spyOn(require('child_process'), 'exec').and.callThrough();
const run = await architect.scheduleBuilder(
'@nrwl/workspace:run-commands',
{
commands: [
{
command: `echo 'Hello World'`
}
]
}
);

await run.result;

expect(exec).toHaveBeenCalledWith(`echo 'Hello World'`, {
maxBuffer: TEN_MEGABYTES,
env: { ...process.env, FORCE_COLOR: `false` }
});
});

it('should set FORCE_COLOR=false when running with --color', async () => {
const exec = spyOn(require('child_process'), 'exec').and.callThrough();
const run = await architect.scheduleBuilder(
'@nrwl/workspace:run-commands',
{
commands: [
{
command: `echo 'Hello World'`
}
],
color: true
}
);

await run.result;

expect(exec).toHaveBeenCalledWith(`echo 'Hello World'`, {
maxBuffer: TEN_MEGABYTES,
env: { ...process.env, FORCE_COLOR: `true` }
});
});
});
});
27 changes: 18 additions & 9 deletions packages/workspace/src/builders/run-commands/run-commands.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ try {

export interface RunCommandsBuilderOptions extends JsonObject {
commands: { command: string }[];
color?: boolean;
parallel?: boolean;
readyWhen?: string;
args?: string;
Expand Down Expand Up @@ -67,12 +68,15 @@ function run(

async function runInParallel(options: RunCommandsBuilderOptions) {
const procs = options.commands.map(c =>
createProcess(c.command, options.readyWhen, options.parsedArgs).then(
result => ({
result,
command: c.command
})
)
createProcess(
c.command,
options.readyWhen,
options.parsedArgs,
options.color
).then(result => ({
result,
command: c.command
}))
);

if (options.readyWhen) {
Expand Down Expand Up @@ -111,7 +115,8 @@ async function runSerially(
const success = await createProcess(
c.command,
options.readyWhen,
options.parsedArgs
options.parsedArgs,
options.color
);
return !success ? c.command : null;
} else {
Expand All @@ -133,11 +138,15 @@ async function runSerially(
function createProcess(
command: string,
readyWhen: string,
parsedArgs: { [key: string]: string }
parsedArgs: { [key: string]: string },
color: boolean
): Promise<boolean> {
command = transformCommand(command, parsedArgs);
return new Promise(res => {
const childProcess = exec(command, { maxBuffer: TEN_MEGABYTES });
const childProcess = exec(command, {
maxBuffer: TEN_MEGABYTES,
env: { ...process.env, FORCE_COLOR: `${color}` }
});
/**
* Ensure the child process is killed when the parent exits
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/workspace/src/builders/run-commands/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"args": {
"type": "string",
"description": "Extra arguments. You can pass them as follows: ng run project:target --args='--wait=100'. You can them use {args.wait} syntax to interpolate them in the workspace config file."
},
"color": {
"type": "boolean",
"description": "Use colors when showing output of command",
"default": false
}
},
"required": ["commands"]
Expand Down

0 comments on commit 8eea4b3

Please sign in to comment.