Skip to content

Commit

Permalink
feat(nx): add flag to toggle the restart of a node process
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAifam5 authored and vsavkin committed Nov 8, 2019
1 parent ae93a87 commit 58dba00
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
8 changes: 8 additions & 0 deletions docs/angular/api-node/builders/execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ The port to inspect the process on. Setting port to 0 will assign random free po
Type: `array`

The targets to run to before starting the node app

### watch

Default: `true`

Type: `boolean`

Run build when files change
8 changes: 8 additions & 0 deletions docs/react/api-node/builders/execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ The port to inspect the process on. Setting port to 0 will assign random free po
Type: `array`

The targets to run to before starting the node app

### watch

Default: `true`

Type: `boolean`

Run build when files change
8 changes: 8 additions & 0 deletions docs/web/api-node/builders/execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ The port to inspect the process on. Setting port to 0 will assign random free po
Type: `array`

The targets to run to before starting the node app

### watch

Default: `true`

Type: `boolean`

Run build when files change
3 changes: 2 additions & 1 deletion packages/node/src/builders/execute/execute.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ describe('NodeExecuteBuilder', () => {
buildTarget: 'nodeapp:build',
port: 9229,
waitUntilTargets: [],
host: 'localhost'
host: 'localhost',
watch: true
};
scheduleTargetAndForget = spyOn(
devkitArchitect,
Expand Down
37 changes: 17 additions & 20 deletions packages/node/src/builders/execute/execute.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { ChildProcess, fork } from 'child_process';
import * as treeKill from 'tree-kill';

import { Observable, bindCallback, of, zip, from } from 'rxjs';
import { Observable, bindCallback, of, zip, from, iif } from 'rxjs';
import { concatMap, tap, mapTo, first, map, filter } from 'rxjs/operators';

import { NodeBuildEvent } from '../build/build.impl';
Expand All @@ -31,13 +31,14 @@ export interface NodeExecuteBuilderOptions extends JsonObject {
buildTarget: string;
host: string;
port: number;
watch: boolean;
}

export default createBuilder<NodeExecuteBuilderOptions>(
nodeExecuteBuilderHandler
);

let subProcess: ChildProcess;
let subProcess: ChildProcess = null;

export function nodeExecuteBuilderHandler(
options: NodeExecuteBuilderOptions,
Expand All @@ -53,28 +54,24 @@ export function nodeExecuteBuilderHandler(
}
return startBuild(options, context).pipe(
concatMap((event: NodeBuildEvent) => {
if (event.success) {
return restartProcess(event.outfile, options, context).pipe(
mapTo(event)
);
} else {
if (!event.success) {
context.logger.error(
'There was an error with the build. See above.'
);
context.logger.info(`${event.outfile} was not restarted.`);
return of(event);
}

return handleBuildEvent(event, options, context).pipe(mapTo(event));
})
);
})
);
}

function runProcess(file: string, options: NodeExecuteBuilderOptions) {
if (subProcess) {
throw new Error('Already running');
}
subProcess = fork(file, options.args, {
function runProcess(event: NodeBuildEvent, options: NodeExecuteBuilderOptions) {
if (subProcess || !event.success) return;

subProcess = fork(event.outfile, options.args, {
execArgv: getExecArgv(options)
});
}
Expand All @@ -93,16 +90,16 @@ function getExecArgv(options: NodeExecuteBuilderOptions) {
return args;
}

function restartProcess(
file: string,
function handleBuildEvent(
event: NodeBuildEvent,
options: NodeExecuteBuilderOptions,
context: BuilderContext
) {
return killProcess(context).pipe(
tap(() => {
runProcess(file, options);
})
);
return iif(
() => !event.success || options.watch,
killProcess(context),
of(undefined)
).pipe(tap(() => runProcess(event, options)));
}

function killProcess(context: BuilderContext): Observable<void | Error> {
Expand Down
5 changes: 5 additions & 0 deletions packages/node/src/builders/execute/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"default": 7777,
"description": "The port to inspect the process on. Setting port to 0 will assign random free ports to all forked processes."
},
"watch": {
"type": "boolean",
"description": "Run build when files change",
"default": true
},
"inspect": {
"oneOf": [
{
Expand Down

0 comments on commit 58dba00

Please sign in to comment.