Skip to content

Commit

Permalink
chore(core): move calculateDefaultProjectName out of workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jul 20, 2023
1 parent b7fb3ea commit 5f17f63
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 92 deletions.
6 changes: 4 additions & 2 deletions packages/devkit/src/executors/read-target-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { ExecutorContext } from 'nx/src/config/misc-interfaces';
import { combineOptionsForExecutor } from 'nx/src/utils/params';
import { requireNx } from '../../nx';

const { Workspaces, getExecutorInformation } = requireNx();
const { Workspaces, getExecutorInformation, calculateDefaultProjectName } =
requireNx();

/**
* Reads and combines options for a given target.
Expand All @@ -28,8 +29,9 @@ export function readTargetOptions<T = any>(
context.root
);

const defaultProject = ws.calculateDefaultProjectName(
const defaultProject = calculateDefaultProjectName(
context.cwd,
context.root,
{ version: 2, projects: context.projectsConfigurations.projects },
context.nxJsonConfiguration
);
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/command-line/exec/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { output } from '../../utils/output';
import { PackageJson } from '../../utils/package-json';
import { getPackageManagerCommand } from '../../utils/package-manager';
import { workspaceRoot } from '../../utils/workspace-root';
import { calculateDefaultProjectName } from '../run/run-one';
import { calculateDefaultProjectName } from '../../config/calculate-default-project-name';

export async function nxExecCommand(
args: Record<string, string[]>
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/command-line/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { calculateDefaultProjectName } from '../../config/calculate-default-project-name';
import { findInstalledPlugins } from '../../utils/plugins/installed-plugins';
import type { Arguments } from 'yargs';
import { output } from '../../utils/output';
Expand Down Expand Up @@ -359,8 +360,9 @@ export async function generate(cwd: string, args: { [k: string]: any }) {
nxJsonConfiguration,
schema,
opts.interactive,
ws.calculateDefaultProjectName(
calculateDefaultProjectName(
cwd,
workspaceRoot,
projectsConfigurations,
nxJsonConfiguration
),
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/command-line/generate/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from '../../config/schema-utils';
import { readJsonFile } from '../../utils/fileutils';
import { readPluginPackageJson } from '../../utils/nx-plugin';
import { getNxRequirePaths } from '../../utils/installation-directory';

export function getGeneratorInformation(
collectionName: string,
Expand Down
42 changes: 2 additions & 40 deletions packages/nx/src/command-line/run/run-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import { NxJsonConfiguration } from '../../config/nx-json';
import { workspaceRoot } from '../../utils/workspace-root';
import { splitTarget } from '../../utils/split-target';
import { output } from '../../utils/output';
import {
ProjectsConfigurations,
TargetDependencyConfig,
} from '../../config/workspace-json-project-json';
import { TargetDependencyConfig } from '../../config/workspace-json-project-json';
import { readNxJson } from '../../config/configuration';
import { calculateDefaultProjectName } from '../../config/calculate-default-project-name';
import { workspaceConfigurationCheck } from '../../utils/workspace-configuration-check';
import { generateGraph } from '../graph/graph';

Expand Down Expand Up @@ -175,39 +173,3 @@ function parseRunOneOptions(

return res;
}

export function calculateDefaultProjectName(
cwd: string,
root: string,
projectsConfigurations: ProjectsConfigurations,
nxJsonConfiguration: NxJsonConfiguration
) {
if (cwd && /^[A-Z]:/.test(cwd)) {
cwd = cwd.charAt(0).toLowerCase() + cwd.slice(1);
}

if (root && /^[A-Z]:/.test(root)) {
root = root.charAt(0).toLowerCase() + root.slice(1);
}

let relativeCwd = cwd.replace(/\\/g, '/').split(root.replace(/\\/g, '/'))[1];

relativeCwd = relativeCwd.startsWith('/')
? relativeCwd.substring(1)
: relativeCwd;
const matchingProject = Object.keys(projectsConfigurations.projects).find(
(p) => {
const projectRoot = projectsConfigurations.projects[p].root;
return (
relativeCwd == projectRoot ||
(relativeCwd == '' && projectRoot == '.') ||
relativeCwd.startsWith(`${projectRoot}/`)
);
}
);
if (matchingProject) return matchingProject;
return (
(nxJsonConfiguration.cli as { defaultProjectName: string })
?.defaultProjectName || nxJsonConfiguration.defaultProject
);
}
57 changes: 57 additions & 0 deletions packages/nx/src/config/calculate-default-project-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { NxJsonConfiguration } from './nx-json';
import {
ProjectConfiguration,
ProjectsConfigurations,
} from './workspace-json-project-json';
import {
findProjectForPath,
normalizeProjectRoot,
} from '../project-graph/utils/find-project-for-path';
import { relative } from 'path';

export function calculateDefaultProjectName(
cwd: string,
root: string,
{ projects }: ProjectsConfigurations,
nxJson: NxJsonConfiguration
) {
const relativeCwd = relative(root, cwd).replace(/\\/g, '/') || null;
if (relativeCwd) {
const matchingProject = findMatchingProjectInCwd(projects, relativeCwd);
// We have found a project
if (matchingProject) {
// That is not at the root
if (
projects[matchingProject].root !== '.' &&
projects[matchingProject].root !== ''
) {
return matchingProject;
// But its at the root, and NX_DEFAULT_PROJECT is set
} else if (process.env.NX_DEFAULT_PROJECT) {
return process.env.NX_DEFAULT_PROJECT;
// Its root, and NX_DEFAULT_PROJECT is not set
} else {
return matchingProject;
}
}
}
// There was no matching project in cwd.
return (
process.env.NX_DEFAULT_PROJECT ??
(nxJson.cli as { defaultProjectName: string })?.defaultProjectName ??
nxJson?.defaultProject
);
}

function findMatchingProjectInCwd(
projects: { [projectName: string]: ProjectConfiguration },
relativeCwd: string
) {
const projectRootMappings = new Map<string, string>();
for (const projectName of Object.keys(projects)) {
const { root } = projects[projectName];
projectRootMappings.set(normalizeProjectRoot(root), projectName);
}
const matchingProject = findProjectForPath(relativeCwd, projectRootMappings);
return matchingProject;
}
47 changes: 0 additions & 47 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ import {
} from '../adapter/angular-json';
import { getNxRequirePaths } from '../utils/installation-directory';
import { getIgnoredGlobs } from '../utils/ignore';
import {
findProjectForPath,
normalizeProjectRoot,
} from '../project-graph/utils/find-project-for-path';

export class Workspaces {
private cachedProjectsConfig: ProjectsConfigurations;

Expand All @@ -37,35 +32,6 @@ export class Workspaces {
return path.relative(this.root, cwd).replace(/\\/g, '/') || null;
}

calculateDefaultProjectName(
cwd: string,
{ projects }: ProjectsConfigurations,
nxJson: NxJsonConfiguration
) {
const relativeCwd = this.relativeCwd(cwd);
if (relativeCwd) {
const matchingProject = findMatchingProjectInCwd(projects, relativeCwd);
// We have found a project
if (matchingProject) {
// That is not at the root
if (
projects[matchingProject].root !== '.' &&
projects[matchingProject].root !== ''
) {
return matchingProject;
// But its at the root, and NX_DEFAULT_PROJECT is set
} else if (process.env.NX_DEFAULT_PROJECT) {
return process.env.NX_DEFAULT_PROJECT;
// Its root, and NX_DEFAULT_PROJECT is not set
} else {
return matchingProject;
}
}
}
// There was no matching project in cwd.
return process.env.NX_DEFAULT_PROJECT ?? nxJson?.defaultProject;
}

/**
* @deprecated
*/
Expand Down Expand Up @@ -191,19 +157,6 @@ export class Workspaces {
}
}

function findMatchingProjectInCwd(
projects: { [projectName: string]: ProjectConfiguration },
relativeCwd: string
) {
const projectRootMappings = new Map<string, string>();
for (const projectName of Object.keys(projects)) {
const { root } = projects[projectName];
projectRootMappings.set(normalizeProjectRoot(root), projectName);
}
const matchingProject = findProjectForPath(relativeCwd, projectRootMappings);
return matchingProject;
}

/**
* Pulled from toFileName in names from @nx/devkit.
* Todo: Should refactor, not duplicate.
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/devkit-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*/
export { createTempNpmDirectory } from './utils/package-manager';
export { getExecutorInformation } from './command-line/run/executor-utils';
export { calculateDefaultProjectName } from './config/calculate-default-project-name';

0 comments on commit 5f17f63

Please sign in to comment.