Skip to content

Commit

Permalink
chore(core): move relativeCwd out of workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Aug 2, 2023
1 parent 5be931c commit bb5c037
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 21 deletions.
4 changes: 3 additions & 1 deletion packages/devkit/src/executors/read-target-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ExecutorContext } from 'nx/src/config/misc-interfaces';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { combineOptionsForExecutor } from 'nx/src/utils/params';
import { requireNx } from '../../nx';
import { relative } from 'path';

const { Workspaces, getExecutorInformation, calculateDefaultProjectName } =
requireNx();
Expand All @@ -21,6 +22,7 @@ export function readTargetOptions<T = any>(
).projects[project];
const targetConfiguration = projectConfiguration.targets[target];

// TODO(v18): remove Workspaces.
const ws = new Workspaces(context.root);
const [nodeModule, executorName] = targetConfiguration.executor.split(':');
const { schema } = getExecutorInformation
Expand Down Expand Up @@ -48,6 +50,6 @@ export function readTargetOptions<T = any>(
targetConfiguration,
schema,
defaultProject,
ws.relativeCwd(context.cwd)
relative(context.cwd, context.root)
) as T;
}
1 change: 0 additions & 1 deletion packages/nx/src/adapter/angular-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
ProjectsConfigurations,
} from '../config/workspace-json-project-json';
import { renamePropertyWithStableKeys } from '../config/workspaces';
import { workspaceRoot } from '../utils/workspace-root';

export function shouldMergeAngularProjects(
root: string,
Expand Down
9 changes: 2 additions & 7 deletions packages/nx/src/command-line/generate/generate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as chalk from 'chalk';
import { prompt } from 'enquirer';
import { relative } from 'path';

import { readNxJson } from '../../config/configuration';
import { ProjectsConfigurations } from '../../config/workspace-json-project-json';
import { Workspaces } from '../../config/workspaces';
import { FileChange, flushChanges, FsTree } from '../../generators/tree';
import {
createProjectGraphAsync,
Expand Down Expand Up @@ -51,7 +51,6 @@ export function printChanges(fileChanges: FileChange[]) {

async function promptForCollection(
generatorName: string,
ws: Workspaces,
interactive: boolean,
projectsConfiguration: ProjectsConfigurations
): Promise<string> {
Expand Down Expand Up @@ -202,7 +201,6 @@ function parseGeneratorString(value: string): {

async function convertToGenerateOptions(
generatorOptions: { [p: string]: any },
ws: Workspaces,
defaultCollectionName: string,
mode: 'generate' | 'new',
projectsConfiguration?: ProjectsConfigurations
Expand All @@ -221,7 +219,6 @@ async function convertToGenerateOptions(
} else if (!defaultCollectionName) {
const generatorString = await promptForCollection(
generatorDescriptor,
ws,
interactive,
projectsConfiguration
);
Expand Down Expand Up @@ -302,7 +299,6 @@ export async function generate(cwd: string, args: { [k: string]: any }) {
}
const verbose = process.env.NX_VERBOSE_LOGGING === 'true';

const ws = new Workspaces(workspaceRoot);
const nxJsonConfiguration = readNxJson();
const projectGraph = await createProjectGraphAsync({ exitOnError: true });
const projectsConfigurations =
Expand All @@ -311,7 +307,6 @@ export async function generate(cwd: string, args: { [k: string]: any }) {
return handleErrors(verbose, async () => {
const opts = await convertToGenerateOptions(
args,
ws,
readDefaultCollection(nxJsonConfiguration),
'generate',
projectsConfigurations
Expand Down Expand Up @@ -366,7 +361,7 @@ export async function generate(cwd: string, args: { [k: string]: any }) {
projectsConfigurations,
nxJsonConfiguration
),
ws.relativeCwd(cwd),
relative(cwd, workspaceRoot),
verbose
);

Expand Down
10 changes: 2 additions & 8 deletions packages/nx/src/command-line/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import {
Schema,
} from '../../utils/params';
import { printHelp } from '../../utils/print-help';
import { Workspaces } from '../../config/workspaces';
import { NxJsonConfiguration } from '../../config/nx-json';
import { readJsonFile } from '../../utils/fileutils';
import { buildTargetFromScript, PackageJson } from '../../utils/package-json';
import { join } from 'path';
import { join, relative } from 'path';
import { existsSync } from 'fs';
import {
loadNxPlugins,
Expand Down Expand Up @@ -111,7 +110,6 @@ function createImplicitTargetConfig(
}

async function parseExecutorAndTarget(
ws: Workspaces,
{ project, target, configuration }: Target,
root: string,
projectsConfigurations: ProjectsConfigurations,
Expand Down Expand Up @@ -147,9 +145,7 @@ async function printTargetRunHelpInternal(
projectsConfigurations: ProjectsConfigurations,
nxJsonConfiguration: NxJsonConfiguration
) {
const ws = new Workspaces(root);
const { executor, nodeModule, schema } = await parseExecutorAndTarget(
ws,
{ project, target, configuration },
root,
projectsConfigurations,
Expand All @@ -176,10 +172,8 @@ async function runExecutorInternal<T extends { success: boolean }>(
): Promise<AsyncIterableIterator<T>> {
validateProject(projectsConfigurations, project);

const ws = new Workspaces(root);
const { executor, implementationFactory, nodeModule, schema, targetConfig } =
await parseExecutorAndTarget(
ws,
{ project, target, configuration },
root,
projectsConfigurations,
Expand All @@ -193,7 +187,7 @@ async function runExecutorInternal<T extends { success: boolean }>(
targetConfig,
schema,
project,
ws.relativeCwd(cwd),
relative(cwd, root),
isVerbose
);

Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/config/relative-cwd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { relative } from 'path';

export function relativeCwd(cwd: string, root: string): string | null {
return relative(root, cwd).replace(/\\/g, '/') || null;
}
4 changes: 0 additions & 4 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export class Workspaces {

constructor(private root: string) {}

relativeCwd(cwd: string) {
return path.relative(this.root, cwd).replace(/\\/g, '/') || null;
}

/**
* @deprecated
*/
Expand Down

0 comments on commit bb5c037

Please sign in to comment.