@@ -12,6 +12,7 @@ import * as jsonc from 'jsonc-parser'
1212import * as tar from 'tar-fs'
1313import { exec } from 'child_process'
1414import { promisify } from 'util'
15+ import { tmpdir } from 'os'
1516
1617const ex = promisify ( exec )
1718
@@ -379,6 +380,24 @@ export function elideMiddle(s: string, len = 8) {
379380 )
380381}
381382
383+ interface NxGraph {
384+ nodes : Record < string , { data : { tags : string [ ] } } >
385+ dependencies : Record < string , { target : string } [ ] >
386+ }
387+
388+ async function readNxGraphFromFile (
389+ projectName : string ,
390+ filePath : string
391+ ) : Promise < NxGraph > {
392+ await ex ( `yarn nx graph --focus=${ projectName } --file=${ filePath } ` , {
393+ cwd : repoRoot ,
394+ } )
395+ const raw = fs . readFileSync ( filePath , 'utf8' )
396+ const graph : NxGraph = JSON . parse ( raw ) . graph
397+
398+ return graph
399+ }
400+
382401/**
383402 * Use Nx to get all dependencies of a project in the repo.
384403 */
@@ -393,15 +412,10 @@ export async function getAllNxDependencies(
393412 throw new Error ( `Project ${ projectName } does not exist.` )
394413 }
395414
396- interface NxGraph {
397- nodes : Record < string , { data : { tags : string [ ] } } >
398- dependencies : Record < string , { target : string } [ ] >
399- }
415+ const file = `${ tmpdir ( ) } /nx-graph-${ projectName . replace ( / \/ / g, '_' ) } .json`
416+ console . log ( info ( `Writing Nx graph to ${ file } ...` ) )
400417
401- const { nodes, dependencies } : NxGraph = await ex (
402- `yarn nx graph --print --focus=${ projectName } ` ,
403- { cwd : repoRoot }
404- ) . then ( ( { stdout } ) => JSON . parse ( stdout ) . graph )
418+ const { nodes, dependencies } = await readNxGraphFromFile ( projectName , file )
405419
406420 // Nx shows both child dependencies and parent (reverse) dependencies for the focused package.
407421 // Filter out reverse dependencies.
0 commit comments