diff --git a/packages/angular/plugins/component-testing.ts b/packages/angular/plugins/component-testing.ts index bce2cbab5bc05e..f2ea4541711f44 100644 --- a/packages/angular/plugins/component-testing.ts +++ b/packages/angular/plugins/component-testing.ts @@ -21,7 +21,7 @@ import { stripIndents, workspaceRoot, } from '@nrwl/devkit'; -import { mapProjectGraphFiles } from 'nx/src/utils/target-project-locator'; +import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils'; import { lstatSync, mkdirSync, writeFileSync } from 'fs'; import { dirname, join, relative } from 'path'; import type { BrowserBuilderSchema } from '../src/builders/webpack-browser/webpack-browser.impl'; @@ -74,7 +74,7 @@ ${e.stack ? e.stack : e}` const buildTarget = getBuildableTarget(ctContext); if (!buildTarget.project && !graph.nodes?.[buildTarget.project]?.data) { - throw new Error(stripIndents`Unable to find project configuration for build target. + throw new Error(stripIndents`Unable to find project configuration for build target. Project Name? ${buildTarget.project} Has project config? ${!!graph.nodes?.[buildTarget.project]?.data}`); } diff --git a/packages/angular/src/generators/component-cypress-spec/lib/get-component-selector.ts b/packages/angular/src/generators/component-cypress-spec/lib/get-component-selector.ts index 06c94bb9abce28..669b76add19bd9 100644 --- a/packages/angular/src/generators/component-cypress-spec/lib/get-component-selector.ts +++ b/packages/angular/src/generators/component-cypress-spec/lib/get-component-selector.ts @@ -1,5 +1,5 @@ import type { Tree } from '@nrwl/devkit'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript'; import type { PropertyAssignment } from 'typescript'; import { SyntaxKind } from 'typescript'; import { diff --git a/packages/angular/src/generators/utils/storybook-ast/module-info.ts b/packages/angular/src/generators/utils/storybook-ast/module-info.ts index 9fea560a3ffe2f..c4abf3d1ef352b 100644 --- a/packages/angular/src/generators/utils/storybook-ast/module-info.ts +++ b/packages/angular/src/generators/utils/storybook-ast/module-info.ts @@ -5,7 +5,7 @@ import { stripIndents, visitNotIgnoredFiles, } from '@nrwl/devkit'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript'; import { tsquery } from '@phenomnomnominal/tsquery'; import { extname } from 'path'; import type { diff --git a/packages/angular/src/generators/utils/storybook-ast/storybook-inputs.ts b/packages/angular/src/generators/utils/storybook-ast/storybook-inputs.ts index 5c65d8bd996f67..21198856af922e 100644 --- a/packages/angular/src/generators/utils/storybook-ast/storybook-inputs.ts +++ b/packages/angular/src/generators/utils/storybook-ast/storybook-inputs.ts @@ -1,7 +1,8 @@ import type { Tree } from '@nrwl/devkit'; -import { getSourceNodes } from '@nrwl/workspace/src/utilities/typescript'; -import { findNodes } from 'nx/src/utils/typescript'; - +import { + findNodes, + getSourceNodes, +} from '@nrwl/workspace/src/utilities/typescript'; import type { PropertyDeclaration } from 'typescript'; import { SyntaxKind } from 'typescript'; import { getTsSourceFile } from '../../../utils/nx-devkit/ast-utils'; diff --git a/packages/angular/src/utils/nx-devkit/ast-utils.ts b/packages/angular/src/utils/nx-devkit/ast-utils.ts index d203031236455b..64bc6bd1bf7130 100644 --- a/packages/angular/src/utils/nx-devkit/ast-utils.ts +++ b/packages/angular/src/utils/nx-devkit/ast-utils.ts @@ -1,5 +1,5 @@ import * as ts from 'typescript'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; import { getSourceNodes } from '@nrwl/workspace/src/utilities/typescript/get-source-nodes'; import * as path from 'path'; import { names, readProjectConfiguration, Tree } from '@nrwl/devkit'; diff --git a/packages/cypress/plugins/cypress-preset.ts b/packages/cypress/plugins/cypress-preset.ts index 22375f866cbef7..3a687eac206557 100644 --- a/packages/cypress/plugins/cypress-preset.ts +++ b/packages/cypress/plugins/cypress-preset.ts @@ -8,8 +8,8 @@ import { TargetConfiguration, workspaceRoot, } from '@nrwl/devkit'; +import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils'; 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'; @@ -98,8 +98,8 @@ export function getProjectConfigByPath( !graph.nodes[componentTestingProjectName]?.data ) { throw new Error( - stripIndents`Unable to find the project configuration that includes ${normalizedPathFromWorkspaceRoot}. - Found project name? ${componentTestingProjectName}. + stripIndents`Unable to find the project configuration that includes ${normalizedPathFromWorkspaceRoot}. + Found project name? ${componentTestingProjectName}. Graph has data? ${!!graph.nodes[componentTestingProjectName]?.data}` ); } diff --git a/packages/eslint-plugin-nx/.eslintrc.json b/packages/eslint-plugin-nx/.eslintrc.json index 5b07d3871cec98..ff47ddace4d515 100644 --- a/packages/eslint-plugin-nx/.eslintrc.json +++ b/packages/eslint-plugin-nx/.eslintrc.json @@ -3,6 +3,7 @@ "rules": { "no-restricted-imports": [ "error", + "@nrwl/workspace", "@angular-devkit/core", "@angular-devkit/architect", "@angular-devkit/schematics" diff --git a/packages/eslint-plugin-nx/package.json b/packages/eslint-plugin-nx/package.json index 640a701ef85816..353455d687d799 100644 --- a/packages/eslint-plugin-nx/package.json +++ b/packages/eslint-plugin-nx/package.json @@ -34,6 +34,7 @@ }, "dependencies": { "@nrwl/devkit": "file:../devkit", + "@nrwl/workspace": "file:../workspace", "@typescript-eslint/utils": "^5.36.1", "chalk": "4.1.0", "confusing-browser-globals": "^1.0.9", diff --git a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts index 389d6fab160bce..e482b999c53711 100644 --- a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts +++ b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts @@ -1,15 +1,13 @@ import type { FileData, ProjectGraph } from '@nrwl/devkit'; import { DependencyType } from '@nrwl/devkit'; +import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils'; import * as parser from '@typescript-eslint/parser'; import { TSESLint } from '@typescript-eslint/utils'; import { vol } from 'memfs'; -import { - TargetProjectLocator, - mapProjectGraphFiles, -} from 'nx/src/utils/target-project-locator'; +import { TargetProjectLocator } from 'nx/src/utils/target-project-locator'; import enforceModuleBoundaries, { RULE_NAME as enforceModuleBoundariesRuleName, -} from './enforce-module-boundaries'; +} from '../../src/rules/enforce-module-boundaries'; jest.mock('fs', () => require('memfs').fs); diff --git a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts index 6d1c7af5d57c8b..0d067184a915e0 100644 --- a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts +++ b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts @@ -5,11 +5,11 @@ import { ProjectGraphProjectNode, workspaceRoot, } from '@nrwl/devkit'; -import { isRelativePath } from 'nx/src/utils/fileutils'; +import { isRelativePath } from '@nrwl/workspace/src/utilities/fileutils'; import { checkCircularPath, findFilesInCircularPath, -} from '../utils/graph-utils'; +} from '@nrwl/workspace/src/utils/graph-utils'; import { DepConstraint, findConstraintsFor, @@ -31,7 +31,7 @@ import { matchImportWithWildcard, onlyLoadChildren, stringifyTags, -} from '../utils/runtime-lint-utils'; +} from '@nrwl/workspace/src/utils/runtime-lint-utils'; import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; import { TargetProjectLocator } from 'nx/src/utils/target-project-locator'; import { basename, dirname, relative } from 'path'; diff --git a/packages/eslint-plugin-nx/src/rules/nx-plugin-checks.ts b/packages/eslint-plugin-nx/src/rules/nx-plugin-checks.ts index a7024277e46d5c..f3ce721757e798 100644 --- a/packages/eslint-plugin-nx/src/rules/nx-plugin-checks.ts +++ b/packages/eslint-plugin-nx/src/rules/nx-plugin-checks.ts @@ -9,7 +9,7 @@ import { import { findSourceProject, getSourceFilePath, -} from '../utils/runtime-lint-utils'; +} from '@nrwl/workspace/src/utils/runtime-lint-utils'; import { existsSync } from 'fs'; import { registerTsProject } from 'nx/src/utils/register'; import * as path from 'path'; diff --git a/packages/eslint-plugin-nx/src/utils/ast-utils.ts b/packages/eslint-plugin-nx/src/utils/ast-utils.ts index 222900b09c0668..9e4b56e5aaf60d 100644 --- a/packages/eslint-plugin-nx/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-nx/src/utils/ast-utils.ts @@ -3,7 +3,7 @@ import { ProjectGraphProjectNode, readJsonFile, } from '@nrwl/devkit'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript'; import { existsSync, readFileSync } from 'fs'; import { dirname } from 'path'; import ts = require('typescript'); diff --git a/packages/eslint-plugin-nx/src/utils/project-graph-utils.ts b/packages/eslint-plugin-nx/src/utils/project-graph-utils.ts index ad7ec951a087ca..4661ac35a81393 100644 --- a/packages/eslint-plugin-nx/src/utils/project-graph-utils.ts +++ b/packages/eslint-plugin-nx/src/utils/project-graph-utils.ts @@ -1,9 +1,9 @@ import { readCachedProjectGraph, readNxJson } from '@nrwl/devkit'; import { - mapProjectGraphFiles, + isTerminalRun, MappedProjectGraph, -} from 'nx/src/utils/target-project-locator'; -import { isTerminalRun } from './runtime-lint-utils'; + mapProjectGraphFiles, +} from '@nrwl/workspace/src/utils/runtime-lint-utils'; import * as chalk from 'chalk'; export function ensureGlobalProjectGraph(ruleName: string) { diff --git a/packages/expo/src/generators/component/lib/add-import.ts b/packages/expo/src/generators/component/lib/add-import.ts index 5e51c4bb21f736..b151ee22261a03 100644 --- a/packages/expo/src/generators/component/lib/add-import.ts +++ b/packages/expo/src/generators/component/lib/add-import.ts @@ -1,4 +1,4 @@ -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript'; import * as ts from 'typescript'; import { ChangeType, StringChange } from '@nrwl/devkit'; diff --git a/packages/next/src/executors/build/lib/create-next-config-file.ts b/packages/next/src/executors/build/lib/create-next-config-file.ts index 39d13be27882fa..a0fd9bf939bbe2 100644 --- a/packages/next/src/executors/build/lib/create-next-config-file.ts +++ b/packages/next/src/executors/build/lib/create-next-config-file.ts @@ -11,7 +11,7 @@ import { existsSync, readFileSync, writeFileSync } from 'fs'; import { join } from 'path'; import type { NextBuildBuilderOptions } from '../../../utils/types'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript'; export function createNextConfigFile( options: NextBuildBuilderOptions, diff --git a/packages/nx/src/utils/target-project-locator.ts b/packages/nx/src/utils/target-project-locator.ts index 59ded7b6c6988c..e5693a196f00a1 100644 --- a/packages/nx/src/utils/target-project-locator.ts +++ b/packages/nx/src/utils/target-project-locator.ts @@ -3,7 +3,6 @@ import { isRelativePath, readJsonFile } from './fileutils'; import { dirname, join, posix } from 'path'; import { workspaceRoot } from './workspace-root'; import { - ProjectGraph, ProjectGraphExternalNode, ProjectGraphProjectNode, } from '../config/project-graph'; @@ -199,11 +198,6 @@ function filterRootExternalDependencies( return nodes; } -/** - * Mapps the project root paths to the project name - * @param nodes - * @returns - */ export function createProjectRootMappings( nodes: Record ) { @@ -218,47 +212,6 @@ export function createProjectRootMappings( return projectRootMappings; } -export type MappedProjectGraph = ProjectGraph & { - allFiles: Record; -}; - -/** - * Strips the file extension from the file path - * @param file - * @returns - */ -export function removeExt(file: string): string { - return file.replace(/(?( - projectGraph: ProjectGraph -): MappedProjectGraph | null { - if (!projectGraph) { - return null; - } - const allFiles: Record = {}; - Object.entries( - projectGraph.nodes as Record - ).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 diff --git a/packages/nx/src/utils/typescript.ts b/packages/nx/src/utils/typescript.ts index a6e4b98537f53b..c364ccf0bb6834 100644 --- a/packages/nx/src/utils/typescript.ts +++ b/packages/nx/src/utils/typescript.ts @@ -104,38 +104,3 @@ export function getRootTsConfigPath(): string | null { return tsConfigFileName ? join(workspaceRoot, tsConfigFileName) : null; } - -export function findNodes( - node: ts.Node, - kind: ts.SyntaxKind | ts.SyntaxKind[], - max = Infinity -): ts.Node[] { - if (!node || max == 0) { - return []; - } - - const arr: ts.Node[] = []; - const hasMatch = Array.isArray(kind) - ? kind.includes(node.kind) - : node.kind === kind; - if (hasMatch) { - arr.push(node); - max--; - } - if (max > 0) { - for (const child of node.getChildren()) { - findNodes(child, kind, max).forEach((node) => { - if (max > 0) { - arr.push(node); - } - max--; - }); - - if (max <= 0) { - break; - } - } - } - - return arr; -} diff --git a/packages/react-native/src/generators/component/lib/add-import.ts b/packages/react-native/src/generators/component/lib/add-import.ts index 5e51c4bb21f736..b151ee22261a03 100644 --- a/packages/react-native/src/generators/component/lib/add-import.ts +++ b/packages/react-native/src/generators/component/lib/add-import.ts @@ -1,4 +1,4 @@ -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript'; import * as ts from 'typescript'; import { ChangeType, StringChange } from '@nrwl/devkit'; diff --git a/packages/react/src/migrations/update-13-0-0/webpack5-changes-utils.ts b/packages/react/src/migrations/update-13-0-0/webpack5-changes-utils.ts index d856735cefa592..ff3d011033b01f 100644 --- a/packages/react/src/migrations/update-13-0-0/webpack5-changes-utils.ts +++ b/packages/react/src/migrations/update-13-0-0/webpack5-changes-utils.ts @@ -7,7 +7,7 @@ import { } from '@nrwl/devkit'; import ts = require('typescript'); -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; export async function migrateToWebPack5(tree: Tree) { allReactProjectsWithStorybookConfiguration(tree).forEach((project) => { diff --git a/packages/react/src/migrations/update-14-0-0/update-react-dom-render-for-v18.ts b/packages/react/src/migrations/update-14-0-0/update-react-dom-render-for-v18.ts index ef5e3eb1c7d6a7..e8ced25914ec08 100644 --- a/packages/react/src/migrations/update-14-0-0/update-react-dom-render-for-v18.ts +++ b/packages/react/src/migrations/update-14-0-0/update-react-dom-render-for-v18.ts @@ -7,7 +7,7 @@ import { Tree, formatFiles, } from '@nrwl/devkit'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; import * as ts from 'typescript'; export async function update(tree: Tree) { diff --git a/packages/react/src/module-federation/ast-utils.ts b/packages/react/src/module-federation/ast-utils.ts index 9e25c9767bf1f7..55758ab17ba213 100644 --- a/packages/react/src/module-federation/ast-utils.ts +++ b/packages/react/src/module-federation/ast-utils.ts @@ -1,6 +1,6 @@ import * as ts from 'typescript'; import { ChangeType, StringChange } from '@nrwl/devkit'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; import { addImport, findClosestOpening, diff --git a/packages/react/src/utils/ast-utils.ts b/packages/react/src/utils/ast-utils.ts index e0c77ed3f7d6c5..cfd21f5c2cd0b5 100644 --- a/packages/react/src/utils/ast-utils.ts +++ b/packages/react/src/utils/ast-utils.ts @@ -1,5 +1,5 @@ import * as ts from 'typescript'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; import { ChangeType, logger, diff --git a/packages/storybook/src/executors/utils.ts b/packages/storybook/src/executors/utils.ts index 7bff48af67d38f..5c2fca04f6b6dd 100644 --- a/packages/storybook/src/executors/utils.ts +++ b/packages/storybook/src/executors/utils.ts @@ -1,5 +1,5 @@ import { ExecutorContext, joinPathFragments, logger } from '@nrwl/devkit'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; import 'dotenv/config'; import { existsSync, readFileSync } from 'fs'; import { join } from 'path'; @@ -58,9 +58,9 @@ function reactWebpack5Check(options: CommonNxStorybookConfig) { It looks like you use Webpack 5 but your Storybook setup is not configured to leverage that and thus falls back to Webpack 4. Make sure you upgrade your Storybook config to use Webpack 5. - + - https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#upgrade - + `); } } diff --git a/packages/storybook/src/migrations/update-12-5-0/install-addon-essentials.ts b/packages/storybook/src/migrations/update-12-5-0/install-addon-essentials.ts index 69da9e3b2c7541..f1fd549db17367 100644 --- a/packages/storybook/src/migrations/update-12-5-0/install-addon-essentials.ts +++ b/packages/storybook/src/migrations/update-12-5-0/install-addon-essentials.ts @@ -6,7 +6,7 @@ import { applyChangesToString, ChangeType, } from '@nrwl/devkit'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; import { getTsSourceFile } from '../../utils/utilities'; import ts = require('typescript'); diff --git a/packages/storybook/src/migrations/update-14-0-0/migrate-stories-to-6-2/migrate-stories-to-6-2.spec.ts b/packages/storybook/src/migrations/update-14-0-0/migrate-stories-to-6-2/migrate-stories-to-6-2.spec.ts index 68d8acd4b287fb..1b19a69330360d 100644 --- a/packages/storybook/src/migrations/update-14-0-0/migrate-stories-to-6-2/migrate-stories-to-6-2.spec.ts +++ b/packages/storybook/src/migrations/update-14-0-0/migrate-stories-to-6-2/migrate-stories-to-6-2.spec.ts @@ -6,7 +6,7 @@ import { import { joinPathFragments, writeJson } from '@nrwl/devkit'; import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing'; import { storybookVersion } from '@nrwl/storybook'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utils/ast-utils'; import * as ts from 'typescript'; import { SyntaxKind } from 'typescript'; import { nxVersion } from '../../../utils/versions'; diff --git a/packages/storybook/src/migrations/update-14-0-0/migrate-stories-to-6-2/migrate-stories-to-6-2.ts b/packages/storybook/src/migrations/update-14-0-0/migrate-stories-to-6-2/migrate-stories-to-6-2.ts index f907177590385b..45d8f0d7e0a729 100644 --- a/packages/storybook/src/migrations/update-14-0-0/migrate-stories-to-6-2/migrate-stories-to-6-2.ts +++ b/packages/storybook/src/migrations/update-14-0-0/migrate-stories-to-6-2/migrate-stories-to-6-2.ts @@ -9,7 +9,7 @@ import { visitNotIgnoredFiles, } from '@nrwl/devkit'; import { fileExists } from '@nrwl/workspace/src/utilities/fileutils'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; import { join, normalize } from 'path'; import { SyntaxKind } from 'typescript'; import { getTsSourceFile } from '../../../utils/utilities'; diff --git a/packages/storybook/src/utils/utilities.ts b/packages/storybook/src/utils/utilities.ts index 97c5e8d0533614..74659381177f18 100644 --- a/packages/storybook/src/utils/utilities.ts +++ b/packages/storybook/src/utils/utilities.ts @@ -11,7 +11,7 @@ import { StorybookConfig } from '../executors/models'; import { constants, copyFileSync, mkdtempSync, statSync } from 'fs'; import { tmpdir } from 'os'; import { basename, join, sep } from 'path'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes'; import ts = require('typescript'); export const Constants = { diff --git a/packages/workspace/index.ts b/packages/workspace/index.ts index 4120e588e51ac7..1eb2794da672c0 100644 --- a/packages/workspace/index.ts +++ b/packages/workspace/index.ts @@ -21,7 +21,6 @@ export { readPackageJson, } from 'nx/src/project-graph/file-utils'; export { ProjectGraphCache } from 'nx/src/project-graph/nx-deps-cache'; -export { findNodes } from 'nx/src/utils/typescript'; export { readJsonInTree, updateJsonInTree, @@ -35,6 +34,7 @@ export { getProjectConfig, addParameterToConstructor, createOrUpdate, + findNodes, updatePackageJsonDependencies, readWorkspace, renameSyncInTree, diff --git a/packages/workspace/src/generators/move/lib/update-imports.ts b/packages/workspace/src/generators/move/lib/update-imports.ts index c4ba29ffa44798..286a24aace67c0 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.ts @@ -12,7 +12,7 @@ import { import { getImportPath } from 'nx/src/utils/path'; import * as ts from 'typescript'; import { getRootTsConfigPathInTree } from '../../../utilities/typescript'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '../../../utilities/typescript/find-nodes'; import { NormalizedSchema } from '../schema'; import { normalizeSlashes } from './utils'; diff --git a/packages/workspace/src/utilities/ast-utils.ts b/packages/workspace/src/utilities/ast-utils.ts index 5f94ccdc4dc7db..77a29d53f9c9ed 100644 --- a/packages/workspace/src/utilities/ast-utils.ts +++ b/packages/workspace/src/utilities/ast-utils.ts @@ -1,7 +1,7 @@ import type { Tree } from '@nrwl/devkit'; import * as ts from 'typescript'; import { getSourceNodes } from './typescript'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from './typescript/find-nodes'; function nodesByPosition(first: ts.Node, second: ts.Node): number { return first.getStart() - second.getStart(); diff --git a/packages/workspace/src/utilities/typescript.ts b/packages/workspace/src/utilities/typescript.ts index 988450dc88db1f..a01f8ba1c13fb4 100644 --- a/packages/workspace/src/utilities/typescript.ts +++ b/packages/workspace/src/utilities/typescript.ts @@ -5,6 +5,7 @@ import { dirname, join } from 'path'; import type * as ts from 'typescript'; export { compileTypeScript } from './typescript/compilation'; export type { TypeScriptCompilationOptions } from './typescript/compilation'; +export { findNodes } from './typescript/find-nodes'; export { getSourceNodes } from './typescript/get-source-nodes'; const normalizedAppRoot = workspaceRoot.replace(/\\/g, '/'); diff --git a/packages/workspace/src/utilities/typescript/find-nodes.ts b/packages/workspace/src/utilities/typescript/find-nodes.ts new file mode 100644 index 00000000000000..ca4369f8014f9c --- /dev/null +++ b/packages/workspace/src/utilities/typescript/find-nodes.ts @@ -0,0 +1,36 @@ +import * as ts from 'typescript'; + +export function findNodes( + node: ts.Node, + kind: ts.SyntaxKind | ts.SyntaxKind[], + max = Infinity +): ts.Node[] { + if (!node || max == 0) { + return []; + } + + const arr: ts.Node[] = []; + const hasMatch = Array.isArray(kind) + ? kind.includes(node.kind) + : node.kind === kind; + if (hasMatch) { + arr.push(node); + max--; + } + if (max > 0) { + for (const child of node.getChildren()) { + findNodes(child, kind, max).forEach((node) => { + if (max > 0) { + arr.push(node); + } + max--; + }); + + if (max <= 0) { + break; + } + } + } + + return arr; +} diff --git a/packages/workspace/src/utils/ast-utils.ts b/packages/workspace/src/utils/ast-utils.ts index 59c774e4ecf258..e2a87d4775d770 100644 --- a/packages/workspace/src/utils/ast-utils.ts +++ b/packages/workspace/src/utils/ast-utils.ts @@ -18,12 +18,17 @@ import { Tree, } from '@angular-devkit/schematics'; import * as ts from 'typescript'; -import { parseJson, serializeJson, FileData } from '@nrwl/devkit'; +import { + parseJson, + ProjectConfiguration, + serializeJson, + FileData, +} from '@nrwl/devkit'; import { getWorkspacePath } from './cli-config-utils'; import { extname, join, normalize, Path } from '@angular-devkit/core'; -import type { NxJsonConfiguration } from '@nrwl/devkit'; +import type { NxJsonConfiguration, ProjectsConfigurations } from '@nrwl/devkit'; import { addInstallTask } from './rules/add-install-task'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes } from '../utilities/typescript/find-nodes'; import { getSourceNodes } from '../utilities/typescript/get-source-nodes'; function nodesByPosition(first: ts.Node, second: ts.Node): number { @@ -66,6 +71,7 @@ export function sortObjectByKeys(obj: unknown) { }, {}); } +export { findNodes } from '../utilities/typescript/find-nodes'; export { getSourceNodes } from '../utilities/typescript/get-source-nodes'; export interface Change { diff --git a/packages/eslint-plugin-nx/src/utils/graph-utils.spec.ts b/packages/workspace/src/utils/graph-utils.spec.ts similarity index 100% rename from packages/eslint-plugin-nx/src/utils/graph-utils.spec.ts rename to packages/workspace/src/utils/graph-utils.spec.ts diff --git a/packages/eslint-plugin-nx/src/utils/graph-utils.ts b/packages/workspace/src/utils/graph-utils.ts similarity index 100% rename from packages/eslint-plugin-nx/src/utils/graph-utils.ts rename to packages/workspace/src/utils/graph-utils.ts diff --git a/packages/workspace/src/utils/rules/rename-package-imports.ts b/packages/workspace/src/utils/rules/rename-package-imports.ts index 57405a23334b49..283c3d3d730b43 100644 --- a/packages/workspace/src/utils/rules/rename-package-imports.ts +++ b/packages/workspace/src/utils/rules/rename-package-imports.ts @@ -7,8 +7,7 @@ import { } from '@angular-devkit/schematics'; import { getWorkspace } from '../workspace'; import { visitNotIgnoredFiles } from './visit-not-ignored-files'; -import { insert, ReplaceChange } from '../ast-utils'; -import { findNodes } from 'nx/src/utils/typescript'; +import { findNodes, insert, ReplaceChange } from '../ast-utils'; import { normalize } from '@angular-devkit/core'; export interface PackageNameMapping { diff --git a/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts b/packages/workspace/src/utils/runtime-lint-utils.spec.ts similarity index 100% rename from packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts rename to packages/workspace/src/utils/runtime-lint-utils.spec.ts diff --git a/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts b/packages/workspace/src/utils/runtime-lint-utils.ts similarity index 93% rename from packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts rename to packages/workspace/src/utils/runtime-lint-utils.ts index 0bcb72f6a1c526..475108a6d9679e 100644 --- a/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts +++ b/packages/workspace/src/utils/runtime-lint-utils.ts @@ -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, - MappedProjectGraph, - removeExt, -} from 'nx/src/utils/target-project-locator'; +import { TargetProjectLocator } from 'nx/src/utils/target-project-locator'; + +export type MappedProjectGraph = ProjectGraph & { + allFiles: Record; +}; export type Deps = { [projectName: string]: ProjectGraphDependency[] }; export type DepConstraint = { @@ -70,6 +70,10 @@ function hasTag(proj: ProjectGraphProjectNode, tag: string) { return tag === '*' || (proj.data.tags || []).indexOf(tag) > -1; } +export function removeExt(file: string): string { + return file.replace(/(?( + projectGraph: ProjectGraph +): MappedProjectGraph | null { + if (!projectGraph) { + return null; + } + const allFiles: Record = {}; + Object.entries( + projectGraph.nodes as Record + ).forEach(([name, node]) => { + node.data.files.forEach(({ file }) => { + const fileName = removeExt(file); + allFiles[fileName] = name; + }); + }); + + return { + ...projectGraph, + allFiles, + }; +} + const ESLINT_REGEX = /node_modules.*[\/\\]eslint$/; const JEST_REGEX = /node_modules\/.bin\/jest$/; // when we run unit tests in jest const NRWL_CLI_REGEX = /nx[\/\\]bin[\/\\]run-executor\.js$/;