diff --git a/actions/plan/src/plan.ts b/actions/plan/src/plan.ts index 9eadcc03..495f2d2b 100644 --- a/actions/plan/src/plan.ts +++ b/actions/plan/src/plan.ts @@ -1,16 +1,13 @@ import * as core from '@actions/core' -import { FsTree } from 'nx/src/generators/tree' -import { getProjects } from 'nx/src/generators/utils/project-configuration' +import { readCachedProjectGraph } from 'nx/src/project-graph/project-graph' import { resolve } from 'path' +import { buildCommand } from './utils/build-command' import { execCommand } from './utils/exec' import { cleanLogConditions, hasOneOfRequiredTags } from './utils/has-one-of-required-tags' async function run() { try { - const nxTree = new FsTree(process.cwd(), core.isDebug()) - const projects = getProjects(nxTree) - const workingDirectory = core.getInput('workingDirectory') || '' const affectedOnly = core.getBooleanInput('affectedOnly') const targets = core.getMultilineInput('targets', { required: true, trimWhitespace: true }) @@ -18,29 +15,34 @@ async function run() { const cwd = resolve(process.cwd(), workingDirectory) // Get all the project names - const projectsNamesToPlanFor = affectedOnly - ? JSON.parse(execCommand( - 'npx nx show projects --affected --json', - { - asString: true, - silent: !core.isDebug(), - cwd - } - )).map((projectName: string) => projectName.trim()) - : Array.from(projects.keys()) + const projectsNamesToPlanFor = JSON.parse(execCommand( + buildCommand([ + 'npx nx show projects --json', + affectedOnly && '--affected' + ]), + { + asString: true, + silent: !core.isDebug(), + cwd + } + )) // Make sure to still log the project names if (!affectedOnly) { core.debug(JSON.stringify(projectsNamesToPlanFor)) } + const projectGraph = readCachedProjectGraph() + // Get all affected projects const enabledProjects = projectsNamesToPlanFor.filter((projectName: string) => { - if (!projects.has(projectName)) { + const project = projectGraph.nodes?.[projectName]?.data + + if (!project) { return false } - return !(projects.get(projectName).tags || []).includes('ci=off') + return !(project.tags || []).includes('ci=off') }) const matrixInclude = [] @@ -60,7 +62,7 @@ async function run() { core.info(`- ${target}PostTargets: ${postTargets.join(' AND ')}`) const amountOfProjectsWithTarget = enabledProjects.map((projectName: string) => { - const { targets, tags } = projects.get(projectName) + const { targets, tags } = projectGraph.nodes[projectName].data if (Object.keys(targets).includes(target)) { return hasOneOfRequiredTags(projectName, tags, tagConditions) diff --git a/actions/plan/src/utils/build-command.ts b/actions/plan/src/utils/build-command.ts new file mode 100644 index 00000000..f90c7b03 --- /dev/null +++ b/actions/plan/src/utils/build-command.ts @@ -0,0 +1,5 @@ +export type Part = string | boolean; + +export const buildCommand = (parts: Part[]): string => { + return parts.filter(Boolean).join(' ') +} diff --git a/actions/run-many/src/run-many.ts b/actions/run-many/src/run-many.ts index 3e4ac6ec..6c494565 100644 --- a/actions/run-many/src/run-many.ts +++ b/actions/run-many/src/run-many.ts @@ -1,6 +1,5 @@ import * as core from '@actions/core' -import { FsTree } from 'nx/src/generators/tree' -import { getProjects } from 'nx/src/generators/utils/project-configuration' +import { readCachedProjectGraph } from 'nx/src/project-graph/project-graph' import { resolve } from 'path' import { hideBin } from 'yargs/helpers' import yargs from 'yargs/yargs' @@ -23,9 +22,6 @@ export const argv = yargs(hideBin(process.argv)) async function run() { try { - const nxTree = new FsTree(process.cwd(), (core.isDebug() || argv.verbose)) - const projects = getProjects(nxTree) - // Get all options const tagConditions = (argv.tag ? [argv.tag] : core.getMultilineInput('tag', { trimWhitespace: true })) const target = core.getInput('target', { required: !argv.target }) || argv.target @@ -54,32 +50,34 @@ async function run() { const cwd = resolve(process.cwd(), workingDirectory) - const projectsNamesToRun = affectedOnly - ? JSON.parse(execCommand( - buildCommand([ - 'npx nx show projects --affected --json', - `-t ${target}` - ]), - { - asString: true, - silent: !(core.isDebug() || argv.verbose), - cwd - } - )) - : Array.from(projects.keys()) + const projectsNamesToRun = JSON.parse(execCommand( + buildCommand([ + 'npx nx show projects --json', + affectedOnly && '--affected', + `-t ${target}` + ]), + { + asString: true, + silent: !(core.isDebug() || argv.verbose), + cwd + } + )) // Make sure to still log the project names if (!affectedOnly) { core.debug(JSON.stringify(projectsNamesToRun)) } + const projectGraph = readCachedProjectGraph() + // Get all affected projects const projectsToRun = projectsNamesToRun.filter((projectName: string) => { - if (!projects.has(projectName)) { + const project = projectGraph.nodes?.[projectName]?.data + + if (!project) { return false } - const project = projects.get(projectName) const tags = project.tags || [] // If the project has ci=off then don't run it @@ -112,7 +110,7 @@ async function run() { const [target, targetConfig] = targetParts.split(':') await runTarget( cwd, - projects, + projectGraph.nodes, runProjects, target, targetConfig, @@ -123,7 +121,7 @@ async function run() { await runTarget( cwd, - projects, + projectGraph.nodes, runProjects, target, config, @@ -138,7 +136,7 @@ async function run() { const [target, targetConfig] = targetParts.split(':') await runTarget( cwd, - projects, + projectGraph.nodes, runProjects, target, targetConfig, diff --git a/actions/run-many/src/utils/get-projects-with-target.ts b/actions/run-many/src/utils/get-projects-with-target.ts index 81891526..46d20c4e 100644 --- a/actions/run-many/src/utils/get-projects-with-target.ts +++ b/actions/run-many/src/utils/get-projects-with-target.ts @@ -1,10 +1,10 @@ -import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json' +import type { ProjectGraphProjectNode } from 'nx/src/config/project-graph' -export function getProjectsWithTarget(projects: Map, runProjects: string[], target: string): string[] { +export function getProjectsWithTarget(projects: Record, runProjects: string[], target: string): string[] { // Filter out all projects that are not allowed - return Array.from(projects).filter(([project, config]) => { + return Object.keys(projects).filter((project) => { // Check if the project has the provided target - return Object.keys(config?.targets ?? {}).includes(target) + return Object.keys(projects[project].data?.targets ?? {}).includes(target) && runProjects.includes(project) - }).map(([project]) => project) + }).map((project) => project) } diff --git a/actions/run-many/src/utils/run-target.ts b/actions/run-many/src/utils/run-target.ts index 37f34b2b..ce86330d 100644 --- a/actions/run-many/src/utils/run-target.ts +++ b/actions/run-many/src/utils/run-target.ts @@ -1,6 +1,6 @@ import * as core from '@actions/core' -import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json' +import type { ProjectGraphProjectNode } from 'nx/src/config/project-graph' import { argv } from '../run-many' import { buildCommand } from './build-command' @@ -11,7 +11,7 @@ import { getRestArgs } from './get-rest-args' export async function runTarget( cwd: string, - projects: Map, + projects: Record, runProjects: string[], target: string, config?: string,