Skip to content

Commit

Permalink
cleanup(core): move mapProjectGraphFiles to target project locator
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Nov 18, 2022
1 parent 8856865 commit 8de2449
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 98 deletions.
39 changes: 0 additions & 39 deletions docs/generated/devkit/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ It only uses language primitives and immutable objects
### Utils Type aliases

- [AdditionalSharedConfig](../../devkit/index#additionalsharedconfig)
- [MappedProjectGraph](../../devkit/index#mappedprojectgraph)
- [ModuleFederationLibrary](../../devkit/index#modulefederationlibrary)
- [Remotes](../../devkit/index#remotes)
- [SharedFunction](../../devkit/index#sharedfunction)
Expand Down Expand Up @@ -162,7 +161,6 @@ It only uses language primitives and immutable objects
- [installPackagesTask](../../devkit/index#installpackagestask)
- [isStandaloneProject](../../devkit/index#isstandaloneproject)
- [joinPathFragments](../../devkit/index#joinpathfragments)
- [mapProjectGraphFiles](../../devkit/index#mapprojectgraphfiles)
- [mapRemotes](../../devkit/index#mapremotes)
- [mapRemotesForSSR](../../devkit/index#mapremotesforssr)
- [moveFilesToNewDirectory](../../devkit/index#movefilestonewdirectory)
Expand Down Expand Up @@ -560,18 +558,6 @@ A plugin for Nx

---

### MappedProjectGraph

Ƭ **MappedProjectGraph**<`T`\>: [`ProjectGraph`](../../devkit/index#projectgraph)<`T`\> & { `allFiles`: `Record`<`string`, `string`\> }

#### Type parameters

| Name | Type |
| :--- | :---- |
| `T` | `any` |

---

### ModuleFederationLibrary

Ƭ **ModuleFederationLibrary**: `Object`
Expand Down Expand Up @@ -1489,31 +1475,6 @@ Normalized path fragments and joins them

---

### mapProjectGraphFiles

**mapProjectGraphFiles**<`T`\>(`projectGraph`): [`MappedProjectGraph`](../../devkit/index#mappedprojectgraph) \| `null`

Maps the project graph to a format that makes it easier to find the project
based on the file path.

#### Type parameters

| Name |
| :--- |
| `T` |

#### Parameters

| Name | Type |
| :------------- | :------------------------------------------------------ |
| `projectGraph` | [`ProjectGraph`](../../devkit/index#projectgraph)<`T`\> |

#### Returns

[`MappedProjectGraph`](../../devkit/index#mappedprojectgraph) \| `null`

---

### mapRemotes

**mapRemotes**(`remotes`, `remoteEntryExt`, `determineRemoteUrl`): `Record`<`string`, `string`\>
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/devkit.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/angular/plugins/component-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ExecutorContext,
joinPathFragments,
logger,
mapProjectGraphFiles,
offsetFromRoot,
parseTargetString,
ProjectConfiguration,
Expand All @@ -22,6 +21,7 @@ import {
stripIndents,
workspaceRoot,
} from '@nrwl/devkit';
import { mapProjectGraphFiles } from 'nx/src/utils/target-project-locator';
import { lstatSync, mkdirSync, writeFileSync } from 'fs';
import { dirname, join, relative } from 'path';
import type { BrowserBuilderSchema } from '../src/builders/webpack-browser/webpack-browser.impl';
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ExecutorContext,
mapProjectGraphFiles,
normalizePath,
ProjectConfiguration,
ProjectGraph,
Expand All @@ -10,6 +9,7 @@ import {
workspaceRoot,
} from '@nrwl/devkit';
import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph';
import { mapProjectGraphFiles } from 'nx/src/utils/target-project-locator';
import { dirname, extname, join, relative } from 'path';
import { lstatSync } from 'fs';

Expand Down
8 changes: 0 additions & 8 deletions packages/devkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,3 @@ export { Hash, Hasher } from 'nx/src/hasher/hasher';
* @category Utils
*/
export { cacheDir } from 'nx/src/utils/cache-directory';

/**
* @category Utils
*/
export {
MappedProjectGraph,
mapProjectGraphFiles,
} from './src/utils/mapped-graph';
40 changes: 0 additions & 40 deletions packages/devkit/src/utils/mapped-graph.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { FileData, ProjectGraph } from '@nrwl/devkit';
import { DependencyType, mapProjectGraphFiles } from '@nrwl/devkit';
import { DependencyType } from '@nrwl/devkit';
import * as parser from '@typescript-eslint/parser';
import { TSESLint } from '@typescript-eslint/utils';
import { vol } from 'memfs';
import { TargetProjectLocator } from 'nx/src/utils/target-project-locator';
import {
TargetProjectLocator,
mapProjectGraphFiles,
} from 'nx/src/utils/target-project-locator';
import enforceModuleBoundaries, {
RULE_NAME as enforceModuleBoundariesRuleName,
} from './enforce-module-boundaries';
Expand Down
7 changes: 3 additions & 4 deletions packages/eslint-plugin-nx/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { readCachedProjectGraph, readNxJson } from '@nrwl/devkit';
import {
readCachedProjectGraph,
readNxJson,
MappedProjectGraph,
mapProjectGraphFiles,
} from '@nrwl/devkit';
MappedProjectGraph,
} from 'nx/src/utils/target-project-locator';
import { isTerminalRun } from './runtime-lint-utils';
import * as chalk from 'chalk';

Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { join } from 'path';
import { getPath, pathExists } from './graph-utils';
import { existsSync } from 'fs';
import { readFileIfExisting } from 'nx/src/project-graph/file-utils';
import { TargetProjectLocator } from 'nx/src/utils/target-project-locator';
import {
TargetProjectLocator,
MappedProjectGraph,
removeExt,
} from '@nrwl/devkit/src/utils/mapped-graph';
} from 'nx/src/utils/target-project-locator';

export type Deps = { [projectName: string]: ProjectGraphDependency[] };
export type DepConstraint = {
Expand Down
47 changes: 47 additions & 0 deletions packages/nx/src/utils/target-project-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isRelativePath, readJsonFile } from './fileutils';
import { dirname, join, posix } from 'path';
import { workspaceRoot } from './workspace-root';
import {
ProjectGraph,
ProjectGraphExternalNode,
ProjectGraphProjectNode,
} from '../config/project-graph';
Expand Down Expand Up @@ -198,6 +199,11 @@ function filterRootExternalDependencies(
return nodes;
}

/**
* Mapps the project root paths to the project name
* @param nodes
* @returns
*/
export function createProjectRootMappings(
nodes: Record<string, ProjectGraphProjectNode>
) {
Expand All @@ -212,6 +218,47 @@ export function createProjectRootMappings(
return projectRootMappings;
}

export type MappedProjectGraph<T = any> = ProjectGraph<T> & {
allFiles: Record<string, string>;
};

/**
* Strips the file extension from the file path
* @param file
* @returns
*/
export function removeExt(file: string): string {
return file.replace(/(?<!(^|\/))\.[^/.]+$/, '');
}

/**
* Maps the project graph to a format that makes it easier to find the project
* based on the file path.
* @param projectGraph
* @returns
*/
export function mapProjectGraphFiles<T>(
projectGraph: ProjectGraph<T>
): MappedProjectGraph | null {
if (!projectGraph) {
return null;
}
const allFiles: Record<string, string> = {};
Object.entries(
projectGraph.nodes as Record<string, ProjectGraphProjectNode>
).forEach(([name, node]) => {
node.data.files.forEach(({ file }) => {
const fileName = removeExt(file);
allFiles[fileName] = name;
});
});

return {
...projectGraph,
allFiles,
};
}

/**
* Locates a project in projectRootMap based on a file within it
* @param filePath path that is inside of projectName
Expand Down

0 comments on commit 8de2449

Please sign in to comment.