You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is you need to wait for storybook's server to be listening by running nx-storybook before running nx test-storybook
Like it'd be great if there was a comand nx storybook-all project which is basically what my script does but also I added the affected iteration myself explicitly since I could not figure out a way to make the script work from using an executor inside project.json.
Right now the js script gets called in my pre commit hooks, and it's working fine and I'm sure there's room for improvement but I just wanted to get something working for proof of concept. I tried both approaches of using an executor with nx:run-commands and nx:run-script but nx:run-commands was not respecting my exit codes (I could not figure out how to get it to fail, only wait forever if I never console.logged out the configured "readyWhen") And nx:run-script as far as I'm aware there was no way to pass in args or anyway to indicate which lib/app is running so the script can look for the configured storybook ports. So basically I had to write this which is doing what nx affected would normally iterate over if trying to call storybook.
const{ exec }=require('child_process');constfs=require('fs').promises;constpath=require('path');constwaitOn=require('wait-on');constnxAffectedLibsCommand=`nx show projects --affected --type lib -t storybook`;constnxAffectedAppsCommand=`nx show projects --affected --type app -t storybook`;// Function to run a command and return a promisefunctionrunCommand(command){returnnewPromise((resolve,reject)=>{constprocess=exec(command);constoutput=[];process.stdout.on('data',data=>{output.push(data);console.log(data);});process.stderr.on('data',data=>console.error(data));process.on('close',code=>{if(code!==0){reject(newError(`Command failed: ${command}`));}else{resolve(output);}});});}// Function to wait for a URL to be availablefunctionwaitForUrl(url){returnnewPromise((resolve,reject)=>{waitOn({resources: [url]},err=>{if(err){console.log(`Failed to wait for URL: ${url}`);reject(`Failed to wait for URL: ${url}`);}else{console.log("Storybook is available at: ",url);resolve();}});});}// Function to get the port number from the project.json fileasyncfunctiongetPortForProject(rootFolder,project){constprojectJsonPath=path.join(rootFolder,project,'project.json');try{constprojectJson=awaitfs.readFile(projectJsonPath,'utf8');constprojectConfig=JSON.parse(projectJson);returnprojectConfig.targets.storybook.options.port;}catch(error){thrownewError(`Failed to read port for project ${project}: ${error.message}`);}}// Main function to run the commands for each affected projectasyncfunctionmain(){try{console.log('Getting list of affected projects...');constaffectedProjectLibs=awaitrunCommand(nxAffectedLibsCommand);constaffectedProjectApps=awaitrunCommand(nxAffectedAppsCommand);constprojects=[{array: affectedProjectLibs,type: 'libs'},{array: affectedProjectApps,type: 'apps'}];for(leti=0;i<projects.length;i++){const{ array, type }=projects[i];for(letprojectofarray){project=project.trim().replace('\\n','');console.log(`Processing project: ${project}`);constport=awaitgetPortForProject(type,project);console.log(`Port for project ${project} is: ${port}`);conststorybookCommand=`nx storybook ${project}`;consttestCommand=`nx test-storybook ${project}`;conststorybookUrl=`http://localhost:${port}`;console.log(`Starting Storybook for project ${project} on port ${port}: ${storybookCommand}`);runCommand(storybookCommand);console.log(`Waiting for Storybook to be available at: ${storybookUrl}`);awaitwaitForUrl(storybookUrl);awaitrunCommand(testCommand);}}process.exit(0);}catch(error){console.log('exiting');console.error('Error:',error.message);process.exit(1);}}main();
Optimally I'd like for nx to support a command like this out of the box but the next best thing would to be able to figured out a way to use nx:run-commands and have it fail if a test fails because then I can take out my explicit affected iteration and leverage nx's.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The problem is you need to wait for storybook's server to be listening by running
nx-storybook
before runningnx test-storybook
Like it'd be great if there was a comand
nx storybook-all project
which is basically what my script does but also I added the affected iteration myself explicitly since I could not figure out a way to make the script work from using an executor inside project.json.Right now the js script gets called in my pre commit hooks, and it's working fine and I'm sure there's room for improvement but I just wanted to get something working for proof of concept. I tried both approaches of using an executor with
nx:run-commands
andnx:run-script
butnx:run-commands
was not respecting my exit codes (I could not figure out how to get it to fail, only wait forever if I never console.logged out the configured "readyWhen") Andnx:run-script
as far as I'm aware there was no way to pass in args or anyway to indicate which lib/app is running so the script can look for the configured storybook ports. So basically I had to write this which is doing what nx affected would normally iterate over if trying to call storybook.Optimally I'd like for nx to support a command like this out of the box but the next best thing would to be able to figured out a way to use
nx:run-commands
and have it fail if a test fails because then I can take out my explicit affected iteration and leverage nx's.Beta Was this translation helpful? Give feedback.
All reactions