Skip to content

Commit be9beff

Browse files
chore: write/read nx dep graph from file (#951)
Signed-off-by: Alex Matson <[email protected]>
1 parent 9ebffd5 commit be9beff

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

scripts/src/lib/utils.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as jsonc from 'jsonc-parser'
1212
import * as tar from 'tar-fs'
1313
import { exec } from 'child_process'
1414
import { promisify } from 'util'
15+
import { tmpdir } from 'os'
1516

1617
const 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

Comments
 (0)