From bf3bf03f5387745e7ac0b5e3b644fce50630af32 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Thu, 20 Jul 2023 12:59:07 -0400 Subject: [PATCH] chore(core): move relativeCwd out of workspaces --- packages/devkit/src/executors/read-target-options.ts | 5 ++--- packages/nx/src/command-line/generate/generate.ts | 3 ++- packages/nx/src/command-line/run/run.ts | 3 ++- packages/nx/src/config/relative-cwd.ts | 5 +++++ packages/nx/src/devkit-internals.ts | 1 + 5 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 packages/nx/src/config/relative-cwd.ts diff --git a/packages/devkit/src/executors/read-target-options.ts b/packages/devkit/src/executors/read-target-options.ts index a83d7193e720e0..e4d8f21e887a5a 100644 --- a/packages/devkit/src/executors/read-target-options.ts +++ b/packages/devkit/src/executors/read-target-options.ts @@ -4,7 +4,7 @@ import type { ExecutorContext } from 'nx/src/config/misc-interfaces'; import { combineOptionsForExecutor } from 'nx/src/utils/params'; import { requireNx } from '../../nx'; -const { Workspaces, getExecutorInformation, calculateDefaultProjectName } = +const { getExecutorInformation, calculateDefaultProjectName, relativeCwd } = requireNx(); /** @@ -21,7 +21,6 @@ export function readTargetOptions( ).projects[project]; const targetConfiguration = projectConfiguration.targets[target]; - const ws = new Workspaces(context.root); const [nodeModule, executorName] = targetConfiguration.executor.split(':'); const { schema } = getExecutorInformation( nodeModule, @@ -42,6 +41,6 @@ export function readTargetOptions( targetConfiguration, schema, defaultProject, - ws.relativeCwd(context.cwd) + relativeCwd(context.cwd, context.root) ) as T; } diff --git a/packages/nx/src/command-line/generate/generate.ts b/packages/nx/src/command-line/generate/generate.ts index 57b4c901f3830f..f895581d398898 100644 --- a/packages/nx/src/command-line/generate/generate.ts +++ b/packages/nx/src/command-line/generate/generate.ts @@ -20,6 +20,7 @@ import { getLocalWorkspacePlugins } from '../../utils/plugins/local-plugins'; import { printHelp } from '../../utils/print-help'; import { workspaceRoot } from '../../utils/workspace-root'; import { NxJsonConfiguration } from '../../config/nx-json'; +import { relativeCwd } from '../../config/relative-cwd'; import { calculateDefaultProjectName } from '../../config/calculate-default-project-name'; import { findInstalledPlugins } from '../../utils/plugins/installed-plugins'; import type { Arguments } from 'yargs'; @@ -366,7 +367,7 @@ export async function generate(cwd: string, args: { [k: string]: any }) { projectsConfigurations, nxJsonConfiguration ), - ws.relativeCwd(cwd), + relativeCwd(cwd, workspaceRoot), verbose ); diff --git a/packages/nx/src/command-line/run/run.ts b/packages/nx/src/command-line/run/run.ts index e6fd53a211b5b5..9714d3e57c07a1 100644 --- a/packages/nx/src/command-line/run/run.ts +++ b/packages/nx/src/command-line/run/run.ts @@ -28,6 +28,7 @@ import { } from '../../project-graph/project-graph'; import { ProjectGraph } from '../../config/project-graph'; import { readNxJson } from '../../config/configuration'; +import { relativeCwd } from '../../config/relative-cwd'; import { getLastValueFromAsyncIterableIterator, isAsyncIterator, @@ -193,7 +194,7 @@ async function runExecutorInternal( targetConfig, schema, project, - ws.relativeCwd(cwd), + relativeCwd(cwd, root), isVerbose ); diff --git a/packages/nx/src/config/relative-cwd.ts b/packages/nx/src/config/relative-cwd.ts new file mode 100644 index 00000000000000..c8d196bfe3d09b --- /dev/null +++ b/packages/nx/src/config/relative-cwd.ts @@ -0,0 +1,5 @@ +import { relative } from 'path'; + +export function relativeCwd(cwd: string, root: string): string | null { + return relative(this.root, cwd).replace(/\\/g, '/') || null; +} diff --git a/packages/nx/src/devkit-internals.ts b/packages/nx/src/devkit-internals.ts index f5448d0381cf65..936cca8404df65 100644 --- a/packages/nx/src/devkit-internals.ts +++ b/packages/nx/src/devkit-internals.ts @@ -6,3 +6,4 @@ export { createTempNpmDirectory } from './utils/package-manager'; export { getExecutorInformation } from './command-line/run/executor-utils'; export { calculateDefaultProjectName } from './config/calculate-default-project-name'; +export { relativeCwd } from './config/relative-cwd';