11import { ExecutorContext } from '@nrwl/devkit' ;
2+ import { appRootPath } from '@nrwl/tao/src/utils/app-root' ;
23
34import { ChildProcess } from 'child_process' ;
4- import * as chockidar from 'chokidar ' ;
5+ import { resolve as pathResolve } from 'path ' ;
56
67import {
78 DotNetClient ,
@@ -10,17 +11,15 @@ import {
1011 dotnetRunOptions ,
1112} from '@nx-dotnet/dotnet' ;
1213import {
13- getDependantProjectsForNxProject ,
1414 getExecutedProjectConfiguration ,
1515 getProjectFileForNxProject ,
16+ handleChildProcessPassthrough ,
1617 rimraf ,
1718} from '@nx-dotnet/utils' ;
1819
1920import { ServeExecutorSchema } from './schema' ;
2021
21- let resolver : ( returnObject : { success : boolean } ) => void ;
2222let childProcess : ChildProcess ;
23- let timeout : NodeJS . Timeout ;
2423let projectDirectory : string ;
2524
2625export default function dotnetRunExecutor (
@@ -29,90 +28,28 @@ export default function dotnetRunExecutor(
2928 dotnetClient : DotNetClient = new DotNetClient ( dotnetFactory ( ) ) ,
3029) : Promise < { success : boolean } > {
3130 const nxProjectConfiguration = getExecutedProjectConfiguration ( context ) ;
31+ const cwd = pathResolve ( appRootPath , nxProjectConfiguration . root ) ;
32+ dotnetClient . cwd = cwd ;
3233
33- return getProjectFileForNxProject ( nxProjectConfiguration ) . then ( ( project ) => {
34- projectDirectory = nxProjectConfiguration . root ;
35-
36- return new Promise ( ( resolve ) => {
37- resolver = resolve ;
38-
39- const watcher = chockidar . watch ( nxProjectConfiguration . root ) ;
40-
41- getDependantProjectsForNxProject (
42- context . projectName as string ,
43- context . workspace ,
44- ( dependency ) => {
45- watcher . add ( dependency . root ) ;
46- } ,
47- ) ;
48-
49- watcher . on ( 'all' , ( event , path ) => {
50- if ( path . includes ( 'bin' ) || path . includes ( 'obj' ) ) {
51- return ;
52- }
53-
54- if ( timeout ) {
55- clearTimeout ( timeout ) ;
56- }
57-
58- timeout = setTimeout ( ( ) => {
59- setupDotnetRun ( dotnetClient , project , options ) ;
60- } , 1000 ) ;
61-
62- console . log ( event , path ) ;
63- } ) ;
64- } ) ;
65- } ) ;
34+ return getProjectFileForNxProject ( nxProjectConfiguration ) . then ( ( project ) =>
35+ runDotnetRun ( dotnetClient , pathResolve ( appRootPath , project ) , options ) ,
36+ ) ;
6637}
6738
68- const setupDotnetRun = (
39+ const runDotnetRun = (
6940 dotnetClient : DotNetClient ,
7041 project : string ,
7142 options : ServeExecutorSchema ,
7243) => {
73- if ( childProcess ) {
74- childProcess . kill ( 'SIGTERM' ) ;
75- }
76-
7744 const opts : dotnetRunOptions = Object . keys ( options ) . map ( ( x ) => ( {
7845 flag : x as dotnetRunFlags ,
7946 value : ( options as Record < string , string | boolean > ) [ x ] ,
8047 } ) ) ;
8148
82- childProcess = dotnetClient . run ( project , opts ) ;
83-
84- childProcess . on ( 'error' , ( err ) => {
85- console . error ( err ) ;
49+ childProcess = dotnetClient . run ( project , true , opts ) ;
50+ return handleChildProcessPassthrough ( childProcess ) . then ( async ( ) => {
51+ await rimraf ( projectDirectory + '/bin' ) ;
52+ await rimraf ( projectDirectory + '/obj' ) ;
53+ return { success : true } ;
8654 } ) ;
8755} ;
88-
89- const exitHandler = async ( options : { exit : boolean } , exitCode = 0 ) => {
90- console . log ( 'Exit Handler Called' ) ;
91-
92- await rimraf ( projectDirectory + '/bin' ) ;
93- await rimraf ( projectDirectory + '/obj' ) ;
94-
95- if ( exitCode || exitCode === 0 ) console . log ( exitCode ) ;
96-
97- if ( childProcess ) {
98- childProcess . kill ( 'SIGINT' ) ;
99- }
100- resolver ( {
101- success : true ,
102- } ) ;
103-
104- if ( options . exit ) process . exit ( ) ;
105- } ;
106-
107- //do something when app is closing
108- process . on ( 'exit' , ( ) => exitHandler ( { exit : false } ) ) ;
109-
110- //catches ctrl+c event
111- process . on ( 'SIGINT' , ( ) => exitHandler ( { exit : true } ) ) ;
112-
113- // catches "kill pid" (for example: nodemon restart)
114- process . on ( 'SIGUSR1' , ( ) => exitHandler ( { exit : true } ) ) ;
115- process . on ( 'SIGUSR2' , ( ) => exitHandler ( { exit : true } ) ) ;
116-
117- //catches uncaught exceptions
118- process . on ( 'uncaughtException' , ( ) => exitHandler ( { exit : true } ) ) ;
0 commit comments