-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): ability to save task graph to a file when running --graph #16350
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,7 +187,12 @@ export async function generateGraph( | |
}, | ||
affectedProjects: string[] | ||
): Promise<void> { | ||
if (Array.isArray(args.targets) && args.targets.length > 1) { | ||
if ( | ||
Array.isArray(args.targets) && | ||
args.targets.length > 1 && | ||
args.file && | ||
!(args.file === 'stdout' || args.file.endsWith('.json')) | ||
) { | ||
output.warn({ | ||
title: 'Showing Multiple Targets is not supported yet', | ||
bodyLines: [ | ||
|
@@ -246,6 +251,18 @@ export async function generateGraph( | |
graph = filterGraph(graph, args.focus || null, args.exclude || []); | ||
|
||
if (args.file) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to alter this output to include Option 1: Option 2: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There is a caveat. Should we try to get accurate hashes this time, or just be ok with no hash? |
||
// stdout is a magical constant that doesn't actually write a file | ||
if (args.file === 'stdout') { | ||
console.log( | ||
JSON.stringify( | ||
createJsonOutput(graph, args.projects, args.targets), | ||
null, | ||
2 | ||
) | ||
); | ||
process.exit(0); | ||
} | ||
|
||
const workspaceFolder = workspaceRoot; | ||
const ext = extname(args.file); | ||
const fullFilePath = isAbsolute(args.file) | ||
|
@@ -294,10 +311,20 @@ export async function generateGraph( | |
} else if (ext === '.json') { | ||
ensureDirSync(dirname(fullFilePath)); | ||
|
||
writeJsonFile(fullFilePath, { | ||
graph, | ||
affectedProjects, | ||
criticalPath: affectedProjects, | ||
const json = createJsonOutput(graph, args.projects, args.targets); | ||
json.affectedProjects = affectedProjects; | ||
json.criticalPath = affectedProjects; | ||
|
||
writeJsonFile(fullFilePath, json); | ||
|
||
output.warn({ | ||
title: 'JSON output contains deprecated fields:', | ||
bodyLines: [ | ||
'- affectedProjects', | ||
'- criticalPath', | ||
'', | ||
'These fields will be removed in Nx 18. If you need to see which projects were affected, use `nx show projects --affected`.', | ||
], | ||
}); | ||
|
||
output.success({ | ||
|
@@ -685,3 +712,47 @@ function createTaskId( | |
return `${projectId}:${targetId}`; | ||
} | ||
} | ||
|
||
interface GraphJsonResponse { | ||
tasks?: TaskGraph; | ||
graph: ProjectGraph; | ||
|
||
/** | ||
* @deprecated To see affected projects, use `nx show projects --affected`. This will be removed in Nx 18. | ||
*/ | ||
affectedProjects?: string[]; | ||
|
||
/** | ||
* @deprecated To see affected projects, use `nx show projects --affected`. This will be removed in Nx 18. | ||
*/ | ||
criticalPath?: string[]; | ||
} | ||
|
||
function createJsonOutput( | ||
graph: ProjectGraph, | ||
projects: string[], | ||
targets?: string[] | ||
): GraphJsonResponse { | ||
const response: GraphJsonResponse = { | ||
graph, | ||
}; | ||
|
||
if (targets?.length) { | ||
const nxJson = readNxJson(); | ||
|
||
const defaultDependencyConfigs = mapTargetDefaultsToDependencies( | ||
nxJson.targetDefaults | ||
); | ||
|
||
response.tasks = createTaskGraph( | ||
graph, | ||
defaultDependencyConfigs, | ||
projects, | ||
targets, | ||
undefined, | ||
{} | ||
); | ||
} | ||
|
||
return response; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
followup: Why is this here?