Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): fix imports into nx from ng-add #13645

Merged
merged 1 commit into from Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/angular/plugins/component-testing.ts
@@ -1,10 +1,10 @@
import {
createExecutorContext,
getProjectConfigByPath,
nxBaseCypressPreset,
NxComponentTestingOptions,
} from '@nrwl/cypress/plugins/cypress-preset';
import {
createExecutorContext,
getProjectConfigByPath,
getTempTailwindPath,
isCtProjectUsingBuildProject,
} from '@nrwl/cypress/src/utils/ct-helpers';
Expand Down
74 changes: 2 additions & 72 deletions packages/cypress/plugins/cypress-preset.ts
@@ -1,20 +1,6 @@
import {
ExecutorContext,
normalizePath,
ProjectConfiguration,
ProjectGraph,
readNxJson,
stripIndents,
TargetConfiguration,
workspaceRoot,
} from '@nrwl/devkit';
import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph';
import { dirname, extname, join, relative } from 'path';
import { workspaceRoot } from '@nrwl/devkit';
import { dirname, join, relative } from 'path';
import { lstatSync } from 'fs';
import {
createProjectRootMappings,
findProjectForPath,
} from 'nx/src/project-graph/utils/find-project-for-path';

interface BaseCypressPreset {
videosFolder: string;
Expand Down Expand Up @@ -81,59 +67,3 @@ export function nxE2EPreset(pathToConfig: string) {
fixturesFolder: 'src/fixtures',
};
}

export function getProjectConfigByPath(
graph: ProjectGraph,
configPath: string
): ProjectConfiguration {
const configFileFromWorkspaceRoot = relative(workspaceRoot, configPath);
const normalizedPathFromWorkspaceRoot = normalizePath(
lstatSync(configPath).isFile()
? configFileFromWorkspaceRoot.replace(extname(configPath), '')
: configFileFromWorkspaceRoot
);

const projectRootMappings = createProjectRootMappings(graph.nodes);
const componentTestingProjectName = findProjectForPath(
normalizedPathFromWorkspaceRoot,
projectRootMappings
);
if (
!componentTestingProjectName ||
!graph.nodes[componentTestingProjectName]?.data
) {
throw new Error(
stripIndents`Unable to find the project configuration that includes ${normalizedPathFromWorkspaceRoot}.
Found project name? ${componentTestingProjectName}.
Graph has data? ${!!graph.nodes[componentTestingProjectName]?.data}`
);
}
// make sure name is set since it can be undefined
graph.nodes[componentTestingProjectName].data.name ??=
componentTestingProjectName;
return graph.nodes[componentTestingProjectName].data;
}

export function createExecutorContext(
graph: ProjectGraph,
targets: Record<string, TargetConfiguration>,
projectName: string,
targetName: string,
configurationName: string
): ExecutorContext {
const projectConfigs = readProjectsConfigurationFromProjectGraph(graph);
return {
cwd: process.cwd(),
projectGraph: graph,
target: targets[targetName],
targetName,
configurationName,
root: workspaceRoot,
isVerbose: false,
projectName,
workspace: {
...readNxJson(),
...projectConfigs,
},
};
}
75 changes: 72 additions & 3 deletions packages/cypress/src/utils/ct-helpers.ts
@@ -1,6 +1,19 @@
import type { ExecutorContext } from 'nx/src/config/misc-interfaces';
import { ProjectGraph } from 'nx/src/config/project-graph';
import { join } from 'path';
import type { ExecutorContext, ProjectGraph } from '@nrwl/devkit';
import {
normalizePath,
ProjectConfiguration,
readNxJson,
stripIndents,
TargetConfiguration,
workspaceRoot,
} from '@nrwl/devkit';
import { extname, join, relative } from 'path';
import { lstatSync } from 'fs';
import {
createProjectRootMappings,
findProjectForPath,
} from 'nx/src/project-graph/utils/find-project-for-path';
import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph';

export const CY_FILE_MATCHER = new RegExp(/\.cy\.[tj]sx?$/);
/**
Expand Down Expand Up @@ -41,3 +54,59 @@ export function isCtProjectUsingBuildProject(
)
);
}

export function getProjectConfigByPath(
graph: ProjectGraph,
configPath: string
): ProjectConfiguration {
const configFileFromWorkspaceRoot = relative(workspaceRoot, configPath);
const normalizedPathFromWorkspaceRoot = normalizePath(
lstatSync(configPath).isFile()
? configFileFromWorkspaceRoot.replace(extname(configPath), '')
: configFileFromWorkspaceRoot
);

const projectRootMappings = createProjectRootMappings(graph.nodes);
const componentTestingProjectName = findProjectForPath(
normalizedPathFromWorkspaceRoot,
projectRootMappings
);
if (
!componentTestingProjectName ||
!graph.nodes[componentTestingProjectName]?.data
) {
throw new Error(
stripIndents`Unable to find the project configuration that includes ${normalizedPathFromWorkspaceRoot}.
Found project name? ${componentTestingProjectName}.
Graph has data? ${!!graph.nodes[componentTestingProjectName]?.data}`
);
}
// make sure name is set since it can be undefined
graph.nodes[componentTestingProjectName].data.name ??=
componentTestingProjectName;
return graph.nodes[componentTestingProjectName].data;
}

export function createExecutorContext(
graph: ProjectGraph,
targets: Record<string, TargetConfiguration>,
projectName: string,
targetName: string,
configurationName: string
): ExecutorContext {
const projectConfigs = readProjectsConfigurationFromProjectGraph(graph);
return {
cwd: process.cwd(),
projectGraph: graph,
target: targets[targetName],
targetName,
configurationName,
root: workspaceRoot,
isVerbose: false,
projectName,
workspace: {
...readNxJson(),
...projectConfigs,
},
};
}
1 change: 0 additions & 1 deletion packages/linter/package.json
Expand Up @@ -35,7 +35,6 @@
"dependencies": {
"@nrwl/devkit": "file:../devkit",
"@phenomnomnominal/tsquery": "4.1.1",
"nx": "file:../nx",
"tmp": "~0.2.1",
"tslib": "^2.3.0"
},
Expand Down
6 changes: 4 additions & 2 deletions packages/react/plugins/component-testing/index.ts
@@ -1,6 +1,4 @@
import {
createExecutorContext,
getProjectConfigByPath,
nxBaseCypressPreset,
NxComponentTestingOptions,
} from '@nrwl/cypress/plugins/cypress-preset';
Expand All @@ -21,6 +19,10 @@ import { normalizeOptions } from '@nrwl/webpack/src/executors/webpack/lib/normal
import { getWebpackConfig } from '@nrwl/webpack/src/executors/webpack/lib/get-webpack-config';
import { resolveCustomWebpackConfig } from '@nrwl/webpack/src/utils/webpack/custom-webpack';
import { buildBaseWebpackConfig } from './webpack-fallback';
import {
createExecutorContext,
getProjectConfigByPath,
} from '@nrwl/cypress/src/utils/ct-helpers';

/**
* React nx preset for Cypress Component Testing
Expand Down