From 85c20ee6af6aa7ca377874896edbd19cbd7778a8 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Thu, 23 May 2024 14:44:48 -0400 Subject: [PATCH] fix(core): fix buildTargetFromScript takes a long time (#25209) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes https://github.com/nrwl/nx/issues/25923 (cherry picked from commit 94707d9575fe302388eaaef6de3aa986b5f3b2cd) --- packages/nx/src/utils/package-json.spec.ts | 7 ++++++- packages/nx/src/utils/package-json.ts | 13 ++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/nx/src/utils/package-json.spec.ts b/packages/nx/src/utils/package-json.spec.ts index 5508fd2a42c977..f5b00a0b7f1d5a 100644 --- a/packages/nx/src/utils/package-json.spec.ts +++ b/packages/nx/src/utils/package-json.spec.ts @@ -7,10 +7,15 @@ import { readModulePackageJson, readTargetsFromPackageJson, } from './package-json'; +import { getPackageManagerCommand } from './package-manager'; describe('buildTargetFromScript', () => { it('should use nx:run-script', () => { - const target = buildTargetFromScript('build'); + const target = buildTargetFromScript( + 'build', + {}, + getPackageManagerCommand() + ); expect(target.executor).toEqual('nx:run-script'); }); }); diff --git a/packages/nx/src/utils/package-json.ts b/packages/nx/src/utils/package-json.ts index 14f4e2fdea65ee..3ac14df43caa7e 100644 --- a/packages/nx/src/utils/package-json.ts +++ b/packages/nx/src/utils/package-json.ts @@ -7,7 +7,10 @@ import { import { mergeTargetConfigurations } from '../project-graph/utils/project-configuration-utils'; import { readJsonFile } from './fileutils'; import { getNxRequirePaths } from './installation-directory'; -import { getPackageManagerCommand } from './package-manager'; +import { + PackageManagerCommands, + getPackageManagerCommand, +} from './package-manager'; export interface NxProjectPackageJsonConfiguration { implicitDependencies?: string[]; @@ -118,9 +121,9 @@ export function readNxMigrateConfig( export function buildTargetFromScript( script: string, - scripts: Record = {} + scripts: Record = {}, + packageManagerCommand: PackageManagerCommands ): TargetConfiguration { - const packageManagerCommand = getPackageManagerCommand(); return { executor: 'nx:run-script', options: { @@ -137,9 +140,9 @@ export function readTargetsFromPackageJson(packageJson: PackageJson) { const { scripts, nx, private: isPrivate } = packageJson ?? {}; const res: Record = {}; const includedScripts = nx?.includedScripts || Object.keys(scripts ?? {}); - // + const packageManagerCommand = getPackageManagerCommand(); for (const script of includedScripts) { - res[script] = buildTargetFromScript(script, scripts); + res[script] = buildTargetFromScript(script, scripts, packageManagerCommand); } for (const targetName in nx?.targets) { res[targetName] = mergeTargetConfigurations(