@@ -363,6 +363,53 @@ const startAll = async ({ options }) => {
363363
364364}
365365
366+ // /**
367+ // * Starts services with nodemon in development mode by default, otherwise with PM2.
368+ // * @param {Object } options - Environment settings for running the services.
369+ // * @param {string[] } options.serviceDirectories - List of service directories under `options.microservicesDir`.
370+ // * @param {string } options.microservicesDir - The root directory of the services.
371+ // * @param {string } [options.mode='dev'] - The environment mode for running the services. Defaults to 'dev'.
372+ // * @returns {void } Starts the services and logs their startup status.
373+ // */
374+ // const spinVanillaServices = async ({ serviceDirectories, microservicesDir, mode = 'dev' }) => {
375+ // const spinner = ora('Starting all services in ' + mode + ' mode...').start();
376+
377+ // try {
378+ // // Simulate delay before starting services
379+ // await delay(1);
380+
381+ // await Promise.all(serviceDirectories.map(async (dir) => {
382+ // const serviceSpinner = ora('Starting service concurrently in: ' + dir).start();
383+ // const processes = await exec(`yarn ${mode}`, { cwd: join(microservicesDir, dir) }, async (error, stdout, stderr) => {
384+ // if (error) {
385+ // const errorMessage = getErrorMessage(error, dir, microservicesDir);
386+ // serviceSpinner.fail(errorMessage);
387+ // } else {
388+ // serviceSpinner.succeed(`Service in directory ${dir} started successfully`);
389+ // }
390+ // });
391+ // processes.stdout.on('data', data => {
392+ // const output = data.toString();
393+ // // Check if the output contains the "yarn run" message
394+ // if (!output.includes('yarn run')) {
395+ // // Stop the spinner before printing the output
396+ // serviceSpinner.stop();
397+ // spinner.succeed(output);
398+ // // Restart the spinner after printing the output
399+ // // serviceSpinner.start();
400+ // }
401+ // });
402+ // }));
403+
404+ // spinner.succeed(`service${serviceDirectories.length > 0 ? 's' : ''} started successfully: ${serviceDirectories}`);
405+ // } catch (error) {
406+ // spinner.fail('An error occurred while starting services');
407+ // console.error(error);
408+ // exit(1);
409+ // }
410+ // };
411+
412+
366413/**
367414 * Starts services with nodemon in development mode by default, otherwise with PM2.
368415 * @param {Object } options - Environment settings for running the services.
@@ -375,42 +422,49 @@ const spinVanillaServices = async ({ serviceDirectories, microservicesDir, mode
375422 const spinner = ora ( 'Starting all services in ' + mode + ' mode...' ) . start ( ) ;
376423
377424 try {
378- // Simulate delay before starting services
379- await delay ( 1 ) ;
380-
381425 await Promise . all ( serviceDirectories . map ( async ( dir ) => {
382- const serviceSpinner = ora ( 'Starting service concurrently in: ' + dir ) . start ( ) ;
383- const processes = await exec ( `yarn ${ mode } ` , { cwd : join ( microservicesDir , dir ) } , async ( error , stdout , stderr ) => {
384- if ( error ) {
385- const errorMessage = getErrorMessage ( error , dir , microservicesDir ) ;
386- serviceSpinner . fail ( errorMessage ) ;
387- } else {
388- serviceSpinner . succeed ( `Service in directory ${ dir } started successfully` ) ;
389- }
390- } ) ;
391- processes . stdout . on ( 'data' , data => {
426+ const servicePath = join ( microservicesDir , dir ) ;
427+ // TODO: check if the yarn.cmd works in windows really
428+ const command = process . platform === 'win32' ? 'yarn.cmd' : 'yarn' ;
429+ const args = [ mode ] ;
430+
431+ const child = spawn ( command , args , { cwd : servicePath , shell : true } ) ;
432+
433+ child . stdout . on ( 'data' , ( data ) => {
392434 const output = data . toString ( ) ;
393435 // Check if the output contains the "yarn run" message
394- if ( ! output . includes ( 'yarn run' ) ) {
436+ if ( ! output . includes ( 'yarn run' ) && ! output . includes ( 'NODE_ENV' ) ) {
395437 // Stop the spinner before printing the output
396- serviceSpinner . stop ( ) ;
397- spinner . succeed ( output ) ;
438+ console . log ( output . trim ( ) ) ;
398439 // Restart the spinner after printing the output
399- // serviceSpinner.start();
440+ }
441+ } ) ;
442+
443+ child . stderr . on ( 'data' , ( data ) => {
444+ const output = data . toString ( ) ;
445+ // Handle stderr output
446+ spinner . fail ( `Error in service ${ dir } : ${ output . trim ( ) } ` ) ;
447+ } ) ;
448+
449+ child . on ( 'close' , ( code ) => {
450+ if ( code !== 0 ) {
451+ spinner . fail ( `Service in directory ${ dir } exited with code ${ code } ` ) ;
452+ } else {
453+ spinner . succeed ( `Service in directory ${ dir } started successfully` ) ;
454+
400455 }
401456 } ) ;
402457 } ) ) ;
403458
404- spinner . succeed ( `service${ serviceDirectories . length > 0 ? 's' : '' } started successfully: ${ serviceDirectories } ` ) ;
459+ spinner . succeed ( `Service${ serviceDirectories . length > 1 ? 's' : '' } started successfully: ${ serviceDirectories . join ( ', ' ) } ` ) ;
460+ console . log ( '\n' )
405461 } catch ( error ) {
406462 spinner . fail ( 'An error occurred while starting services' ) ;
407463 console . error ( error ) ;
408- exit ( 1 ) ;
464+ process . exit ( 1 ) ;
409465 }
410466} ;
411467
412-
413-
414468const getErrorMessage = ( error , dir , microservicesDir ) => {
415469 const errorMessageParts = error . message . split ( '\n' ) ;
416470 let errorMessage = '' ;
@@ -862,7 +916,7 @@ const addPackageJson = async ({ project_root, answers }) => {
862916 "winston" ,
863917 "mongoose"
864918 ] ;
865- const devDependencies = [ "nodemon" , "jest" ] ;
919+ const devDependencies = [ "nodemon" , "jest" ] ;
866920 const configDependencies = [ 'dotenv' , 'joi' , 'morgan' , 'winston' ]
867921 const utilitiesDependencies = [ 'joi' ]
868922 // Join dependencies into a single string for the command
@@ -1196,12 +1250,11 @@ const generateMCSHelper = ({ project_root, answers }) => {
11961250 // Correct the file extension based on directory
11971251 const mcsPath = `${ project_root } /microservices/${ answers . service_name } /src/${ mcs } `
11981252 mkdirSync ( mcsPath , { recursive : true } )
1199- const fileExtension = mcs === 'models' ? 'model .js' : mcs === 'routes' ? 'routes.js' : 'controllers.js' ;
1253+ const fileExtension = ` ${ mcs } .js` ;
12001254
12011255 // Write main file content
1202- const mainContent = mcs === 'models' ? assets . modelContent ( ) : mcs === 'routes' ? assets . routesContent ( ) : mcs === 'controllers' ? assets . controllersContent ( { answers } ) : assets . servicesContent ( ) ;
1256+ const mainContent = mcs === 'models' ? assets . modelContent ( { answers } ) : mcs === 'routes' ? assets . routesContent ( { answers } ) : mcs === 'controllers' ? assets . controllersContent ( { answers } ) : assets . servicesContent ( { answers } ) ;
12031257 writeFileSync ( join ( mcsPath , `${ fileExtension } ` ) , mainContent ) ;
1204-
12051258 // Write index file content
12061259 const indexContent = mcs === 'models' ? assets . modelIndexContent ( ) : mcs === 'routes' ? assets . routesIndexContent ( ) : mcs === 'controllers' ? assets . controllersIndexContent ( ) : assets . servicesIndexContent ( ) ;
12071260 writeFileSync ( join ( mcsPath , 'index.js' ) , indexContent ) ;
@@ -1223,17 +1276,17 @@ const releasePackage = async ({ package }) => {
12231276 const { workspace_name } = retrieveWorkSpaceName ( { package_json_path } ) ;
12241277 if ( package ) {
12251278 logInfo ( { message : `Looking for package: ${ workspace_name } /${ package } ` } ) ;
1226- await executeCommand ( 'yarn' , [ 'workspace' , `${ workspace_name } /${ package } ` , 'release' ] , { stdio :'inherit' , shell :true } ) ;
1279+ await executeCommand ( 'yarn' , [ 'workspace' , `${ workspace_name } /${ package } ` , 'release' ] , { stdio : 'inherit' , shell : true } ) ;
12271280 } else {
1228- await executeCommand ( 'yarn' , [ 'generate:release' ] , { cwd : cwd ( ) , stdio :'inherit' , shell :true } ) ;
1281+ await executeCommand ( 'yarn' , [ 'generate:release' ] , { cwd : cwd ( ) , stdio : 'inherit' , shell : true } ) ;
12291282 }
12301283 } catch ( error ) {
1231- ora ( ) . fail ( 'Command failed to run' ) ;
1284+ ora ( ) . fail ( 'Command failed to run' ) ;
12321285 }
12331286}
12341287
1235- const executeCommand = ( command , args , options ) => {
1236- return new Promise ( async ( resolve , reject ) => {
1288+ const executeCommand = ( command , args , options ) => {
1289+ return new Promise ( async ( resolve , reject ) => {
12371290 const child = await spawn ( command , args , options ) ;
12381291 child . on ( 'close' , ( code ) => {
12391292 if ( code !== 0 ) {
0 commit comments