Skip to content

Commit

Permalink
fix(angular): fix imports into nx from ng-add (#13645)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Dec 5, 2022
1 parent 9453a75 commit 2c81904
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 80 deletions.
4 changes: 2 additions & 2 deletions packages/angular/plugins/component-testing.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2c81904

Please sign in to comment.