Skip to content

Commit

Permalink
feat(core): "cwd" option for commands runner (#2501)
Browse files Browse the repository at this point in the history
* feat(core): add cwd option to commands runner

* docs(core): add docs for new cwd option in commands runner

* cleanup(core): remove unecesarry import in test

* fix(core): fix failing unit test

* feat(core): allow additionalProperties for run commands

* docs(core): format docs for command runner
  • Loading branch information
rarmatei committed Feb 20, 2020
1 parent eee8c18 commit c04f8af
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
6 changes: 6 additions & 0 deletions docs/angular/api-workspace/builders/run-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Type: `string`

Command to run in child process

### cwd

Type: `string`

Current working directory of the commands.

### envFile

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

Command to run in child process

### cwd

Type: `string`

Current working directory of the commands.

### envFile

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

Command to run in child process

### cwd

Type: `string`

Current working directory of the commands.

### envFile

Type: `string`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe('Command Runner Builder', () => {
await testArchitectHost.addBuilderFromPackage(join(__dirname, '../../..'));
});

// TODO re-enable test when https://github.com/angular/angular-cli/pull/14315 is merged
xit('should error when no commands are given', async () => {
it('should error when no commands are given', async () => {
try {
const run = await architect.scheduleBuilder(
'@nrwl/workspace:run-commands',
Expand All @@ -33,14 +32,14 @@ describe('Command Runner Builder', () => {
await run.output.toPromise();
fail('should throw');
} catch (e) {
expect(e).toEqual(
`ERROR: Bad builder config for @nrwl/run-command - "command" option is required`
expect(e.message).toContain(`Schema validation failed`);
expect(e.message).toContain(
`path "" should have required property 'commands'`
);
}
});

// TODO re-enable test when https://github.com/angular/angular-cli/pull/14315 is merged
xit('should error when no command is given', async () => {
it('should error when no command is given', async () => {
try {
const run = await architect.scheduleBuilder(
'@nrwl/workspace:run-commands',
Expand All @@ -51,8 +50,9 @@ describe('Command Runner Builder', () => {
await run.result;
fail('should throw');
} catch (e) {
expect(e).toEqual(
`ERROR: Bad builder config for @nrwl/run-command - "command" option is required`
expect(e.message).toContain(`Schema validation failed`);
expect(e.message).toContain(
`path ".commands[0]" should have required property 'command'`
);
}
});
Expand Down Expand Up @@ -271,6 +271,36 @@ describe('Command Runner Builder', () => {
});
});

it('should run the task in the specified working directory', async () => {
const f = fileSync().name;
let run = await architect.scheduleBuilder('@nrwl/workspace:run-commands', {
commands: [
{
command: `pwd >> ${f}`
}
]
});

let result = await run.result;

expect(result).toEqual(jasmine.objectContaining({ success: true }));
expect(readFile(f)).not.toContain('/packages');

run = await architect.scheduleBuilder('@nrwl/workspace:run-commands', {
commands: [
{
command: `pwd >> ${f}`
}
],
cwd: 'packages'
});

result = await run.result;

expect(result).toEqual(jasmine.objectContaining({ success: true }));
expect(readFile(f)).toContain('/packages');
});

describe('dotenv', () => {
beforeAll(() => {
writeFileSync('.env', 'NRWL_SITE=https://nrwl.io/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface RunCommandsBuilderOptions extends JsonObject {
color?: boolean;
parallel?: boolean;
readyWhen?: string;
cwd?: string;
args?: string;
envFile?: string;
parsedArgs?: { [key: string]: string };
Expand Down Expand Up @@ -83,7 +84,8 @@ async function runInParallel(options: RunCommandsBuilderOptions) {
c.command,
options.readyWhen,
options.parsedArgs,
options.color
options.color,
options.cwd
).then(result => ({
result,
command: c.command
Expand Down Expand Up @@ -127,7 +129,8 @@ async function runSerially(
c.command,
options.readyWhen,
options.parsedArgs,
options.color
options.color,
options.cwd
);
return !success ? c.command : null;
} else {
Expand All @@ -150,13 +153,15 @@ function createProcess(
command: string,
readyWhen: string,
parsedArgs: { [key: string]: string },
color: boolean
color: boolean,
cwd: string
): Promise<boolean> {
command = transformCommand(command, parsedArgs);
return new Promise(res => {
const childProcess = exec(command, {
maxBuffer: TEN_MEGABYTES,
env: { ...process.env, FORCE_COLOR: `${color}` }
env: { ...process.env, FORCE_COLOR: `${color}` },
cwd
});
/**
* Ensure the child process is killed when the parent exits
Expand Down
4 changes: 4 additions & 0 deletions packages/workspace/src/builders/run-commands/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
}
}
]
},
"cwd": {
"type": "string",
"description": "Current working directory of the commands."
}
},
"required": ["commands"]
Expand Down

0 comments on commit c04f8af

Please sign in to comment.