From de2824a4500168e4ac99bd8f57945758ad89b8ef Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Wed, 13 Sep 2023 17:39:45 -0400 Subject: [PATCH] feat(devkit): add method for tree-aware glob search (#19128) --- docs/generated/devkit/README.md | 1 + docs/generated/devkit/glob.md | 20 ++++++ .../packages/devkit/documents/nx_devkit.md | 1 + packages/devkit/internal-testing-utils.ts | 7 +- .../update-16-0-0-add-nx-packages.spec.ts | 2 +- .../src/rules/dependency-checks.spec.ts | 2 +- .../rules/enforce-module-boundaries.spec.ts | 2 +- .../src/utils/runtime-lint-utils.spec.ts | 2 +- .../src/utils/find-npm-dependencies.spec.ts | 2 +- .../package-json/update-package-json.spec.ts | 2 +- packages/nx/migrations.spec.ts | 2 +- .../plugins/package-json-workspaces.spec.ts | 2 +- packages/nx/src/config/workspaces.spec.ts | 4 +- packages/nx/src/devkit-exports.ts | 10 ++- packages/nx/src/generators/utils/glob.spec.ts | 68 +++++++++++++++++++ packages/nx/src/generators/utils/glob.ts | 38 +++++++++++ .../utils/project-configuration.spec.ts | 2 +- packages/nx/src/hasher/task-hasher.spec.ts | 16 ++--- .../assert-valid-migrations.ts | 5 +- .../mock-fs.ts | 0 .../run-migration-against-this-workspace.ts | 2 +- .../temp-fs.ts | 11 ++- .../with-environment.ts | 0 .../update-14-0-6/remove-roots.spec.ts | 2 +- .../update-14-2-0/add-json-schema.spec.ts | 2 +- .../remove-default-collection.spec.ts | 2 +- .../create-target-defaults.spec.ts | 2 +- .../update-15-0-0/migrate-to-inputs.spec.ts | 2 +- .../update-depends-on-to-tokens.spec.ts | 2 +- .../remove-run-commands-output-path.spec.ts | 2 +- packages/nx/src/native/index.d.ts | 2 +- packages/nx/src/native/tests/watcher.spec.ts | 3 +- .../src/native/tests/workspace_files.spec.ts | 2 +- .../nx/src/native/workspace/config_files.rs | 4 +- packages/nx/src/native/workspace/context.rs | 4 +- ...explicit-package-json-dependencies.spec.ts | 2 +- .../explicit-project-dependencies.spec.ts | 2 +- .../build-nodes/project-json.spec.ts | 2 +- .../utils/retrieve-workspace-files.spec.ts | 2 +- .../utils/retrieve-workspace-files.ts | 9 +-- .../nx/src/utils/command-line-utils.spec.ts | 2 +- packages/nx/src/utils/workspace-context.ts | 4 +- packages/nx/tsconfig.lib.json | 2 +- packages/nx/tsconfig.spec.json | 2 +- 44 files changed, 196 insertions(+), 61 deletions(-) create mode 100644 docs/generated/devkit/glob.md create mode 100644 packages/nx/src/generators/utils/glob.spec.ts create mode 100644 packages/nx/src/generators/utils/glob.ts rename packages/nx/{ => src}/internal-testing-utils/assert-valid-migrations.ts (90%) rename packages/nx/src/{utils/testing => internal-testing-utils}/mock-fs.ts (100%) rename packages/nx/{ => src}/internal-testing-utils/run-migration-against-this-workspace.ts (89%) rename packages/nx/src/{utils/testing => internal-testing-utils}/temp-fs.ts (87%) rename packages/nx/{ => src}/internal-testing-utils/with-environment.ts (100%) diff --git a/docs/generated/devkit/README.md b/docs/generated/devkit/README.md index 8cb71263358b4..0bea288f58861 100644 --- a/docs/generated/devkit/README.md +++ b/docs/generated/devkit/README.md @@ -116,6 +116,7 @@ It only uses language primitives and immutable objects - [getProjects](../../devkit/documents/getProjects) - [getWorkspaceLayout](../../devkit/documents/getWorkspaceLayout) - [getWorkspacePath](../../devkit/documents/getWorkspacePath) +- [glob](../../devkit/documents/glob) - [hashArray](../../devkit/documents/hashArray) - [installPackagesTask](../../devkit/documents/installPackagesTask) - [isStandaloneProject](../../devkit/documents/isStandaloneProject) diff --git a/docs/generated/devkit/glob.md b/docs/generated/devkit/glob.md new file mode 100644 index 0000000000000..08a29fcafbec7 --- /dev/null +++ b/docs/generated/devkit/glob.md @@ -0,0 +1,20 @@ +# Function: glob + +▸ **glob**(`tree`, `patterns`): `string`[] + +Performs a tree-aware glob search on the files in a workspace. Able to find newly +created files and hides deleted files before the updates are committed to disk. +Paths should be unix-style with forward slashes. + +#### Parameters + +| Name | Type | Description | +| :--------- | :------------------------------------ | :---------------------- | +| `tree` | [`Tree`](../../devkit/documents/Tree) | The file system tree | +| `patterns` | `string`[] | A list of glob patterns | + +#### Returns + +`string`[] + +Normalized paths in the workspace that match the provided glob patterns. diff --git a/docs/generated/packages/devkit/documents/nx_devkit.md b/docs/generated/packages/devkit/documents/nx_devkit.md index 8cb71263358b4..0bea288f58861 100644 --- a/docs/generated/packages/devkit/documents/nx_devkit.md +++ b/docs/generated/packages/devkit/documents/nx_devkit.md @@ -116,6 +116,7 @@ It only uses language primitives and immutable objects - [getProjects](../../devkit/documents/getProjects) - [getWorkspaceLayout](../../devkit/documents/getWorkspaceLayout) - [getWorkspacePath](../../devkit/documents/getWorkspacePath) +- [glob](../../devkit/documents/glob) - [hashArray](../../devkit/documents/hashArray) - [installPackagesTask](../../devkit/documents/installPackagesTask) - [isStandaloneProject](../../devkit/documents/isStandaloneProject) diff --git a/packages/devkit/internal-testing-utils.ts b/packages/devkit/internal-testing-utils.ts index 2a887f34e7744..edef8ec37b65e 100644 --- a/packages/devkit/internal-testing-utils.ts +++ b/packages/devkit/internal-testing-utils.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/no-restricted-imports */ -export * from 'nx/internal-testing-utils/assert-valid-migrations'; -export * from 'nx/internal-testing-utils/run-migration-against-this-workspace'; -export * from 'nx/internal-testing-utils/with-environment'; +export * from 'nx/src/internal-testing-utils/assert-valid-migrations'; +export * from 'nx/src/internal-testing-utils/run-migration-against-this-workspace'; +export * from 'nx/src/internal-testing-utils/with-environment'; +export * from 'nx/src/internal-testing-utils/temp-fs'; diff --git a/packages/devkit/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts b/packages/devkit/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts index e162167a66ba1..545633098521d 100644 --- a/packages/devkit/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts +++ b/packages/devkit/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts @@ -1,4 +1,4 @@ -import { assertRunsAgainstNxRepo } from 'nx/internal-testing-utils/run-migration-against-this-workspace'; +import { assertRunsAgainstNxRepo } from 'nx/src/internal-testing-utils/run-migration-against-this-workspace'; import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace'; import { Tree } from 'nx/src/generators/tree'; import { readJson, updateJson } from 'nx/src/generators/utils/json'; diff --git a/packages/eslint-plugin/src/rules/dependency-checks.spec.ts b/packages/eslint-plugin/src/rules/dependency-checks.spec.ts index 122b5c680b403..fad6069fcc4ac 100644 --- a/packages/eslint-plugin/src/rules/dependency-checks.spec.ts +++ b/packages/eslint-plugin/src/rules/dependency-checks.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/utils/testing/mock-fs'; +import 'nx/src/internal-testing-utils/mock-fs'; import dependencyChecks, { Options, diff --git a/packages/eslint-plugin/src/rules/enforce-module-boundaries.spec.ts b/packages/eslint-plugin/src/rules/enforce-module-boundaries.spec.ts index c4b39f10a501d..f07e863b1e60d 100644 --- a/packages/eslint-plugin/src/rules/enforce-module-boundaries.spec.ts +++ b/packages/eslint-plugin/src/rules/enforce-module-boundaries.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/utils/testing/mock-fs'; +import 'nx/src/internal-testing-utils/mock-fs'; import type { FileData, ProjectFileMap, ProjectGraph } from '@nx/devkit'; import { DependencyType } from '@nx/devkit'; diff --git a/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts b/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts index 273d7887bd34d..3f85b03d9f79f 100644 --- a/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts +++ b/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/utils/testing/mock-fs'; +import 'nx/src/internal-testing-utils/mock-fs'; import { ProjectGraph, diff --git a/packages/js/src/utils/find-npm-dependencies.spec.ts b/packages/js/src/utils/find-npm-dependencies.spec.ts index 9eebff3ce114b..08088ee72419d 100644 --- a/packages/js/src/utils/find-npm-dependencies.spec.ts +++ b/packages/js/src/utils/find-npm-dependencies.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/utils/testing/mock-fs'; +import 'nx/src/internal-testing-utils/mock-fs'; import { vol } from 'memfs'; import { findNpmDependencies } from './find-npm-dependencies'; diff --git a/packages/js/src/utils/package-json/update-package-json.spec.ts b/packages/js/src/utils/package-json/update-package-json.spec.ts index 41ed5ee394b93..0ee856a8817f7 100644 --- a/packages/js/src/utils/package-json/update-package-json.spec.ts +++ b/packages/js/src/utils/package-json/update-package-json.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/utils/testing/mock-fs'; +import 'nx/src/internal-testing-utils/mock-fs'; import { getUpdatedPackageJsonContent, diff --git a/packages/nx/migrations.spec.ts b/packages/nx/migrations.spec.ts index 787661d5daee8..1a3e7484146d3 100644 --- a/packages/nx/migrations.spec.ts +++ b/packages/nx/migrations.spec.ts @@ -1,6 +1,6 @@ import json = require('./migrations.json'); -import { assertValidMigrationPaths } from './internal-testing-utils/assert-valid-migrations'; +import { assertValidMigrationPaths } from './src/internal-testing-utils/assert-valid-migrations'; import { MigrationsJson } from './src/config/misc-interfaces'; describe('nx migrations', () => { diff --git a/packages/nx/plugins/package-json-workspaces.spec.ts b/packages/nx/plugins/package-json-workspaces.spec.ts index df30a43d652f7..651438f72d7e2 100644 --- a/packages/nx/plugins/package-json-workspaces.spec.ts +++ b/packages/nx/plugins/package-json-workspaces.spec.ts @@ -1,6 +1,6 @@ import * as memfs from 'memfs'; -import '../src/utils/testing/mock-fs'; +import '../src/internal-testing-utils/mock-fs'; import { createNodeFromPackageJson } from './package-json-workspaces'; describe('nx package.json workspaces plugin', () => { diff --git a/packages/nx/src/config/workspaces.spec.ts b/packages/nx/src/config/workspaces.spec.ts index 51c4db200e056..e20a1dcea9b22 100644 --- a/packages/nx/src/config/workspaces.spec.ts +++ b/packages/nx/src/config/workspaces.spec.ts @@ -1,6 +1,6 @@ import { toProjectName, Workspaces } from './workspaces'; -import { TempFs } from '../utils/testing/temp-fs'; -import { withEnvironmentVariables } from '../../internal-testing-utils/with-environment'; +import { TempFs } from '../internal-testing-utils/temp-fs'; +import { withEnvironmentVariables } from '../internal-testing-utils/with-environment'; import { retrieveProjectConfigurations } from '../project-graph/utils/retrieve-workspace-files'; import { readNxJson } from './configuration'; diff --git a/packages/nx/src/devkit-exports.ts b/packages/nx/src/devkit-exports.ts index e1b81a185e30d..59094ed2d5ba8 100644 --- a/packages/nx/src/devkit-exports.ts +++ b/packages/nx/src/devkit-exports.ts @@ -116,6 +116,11 @@ export { getProjects, } from './generators/utils/project-configuration'; +/** + * @category Generators + */ +export { glob } from './generators/utils/glob'; + /** * @category Generators */ @@ -127,6 +132,9 @@ export { getWorkspacePath, } from './generators/utils/deprecated'; +/** + * @category Generators + */ export { readNxJson, updateNxJson, @@ -164,7 +172,7 @@ export { } from './project-graph/project-graph-builder'; /** - * @category Utils + * @category Generators */ export { readJson, writeJson, updateJson } from './generators/utils/json'; diff --git a/packages/nx/src/generators/utils/glob.spec.ts b/packages/nx/src/generators/utils/glob.spec.ts new file mode 100644 index 0000000000000..d44fc42b2c65a --- /dev/null +++ b/packages/nx/src/generators/utils/glob.spec.ts @@ -0,0 +1,68 @@ +import { TempFs } from '../../internal-testing-utils/temp-fs'; +import { FsTree, Tree } from '../tree'; +import { glob } from './glob'; + +describe('glob', () => { + let fs: TempFs; + let tree: Tree; + + beforeEach(() => { + fs = new TempFs('glob', true); + tree = new FsTree(fs.tempDir, false); + }); + + afterEach(() => { + fs.cleanup(); + }); + + it('should find files on disk', async () => { + fs.writeFile('1.txt', '1'); + fs.writeFile('2.txt', '2'); + fs.writeFile('3.txt', '3'); + fs.writeFile('4.md', '4'); + + const results = glob(tree, ['*.txt']).sort(); + + expect(results).toMatchInlineSnapshot(` + [ + "1.txt", + "2.txt", + "3.txt", + ] + `); + }); + + it('should add files from tree', async () => { + fs.writeFile('1.txt', '1'); + fs.writeFile('2.txt', '2'); + tree.write('3.txt', '3'); + fs.writeFile('4.md', '4'); + + const withTree = glob(tree, ['*.txt']).sort(); + + expect(withTree).toMatchInlineSnapshot(` + [ + "1.txt", + "2.txt", + "3.txt", + ] + `); + }); + + it('should hide files deleted on tree', async () => { + fs.writeFile('1.txt', '1'); + fs.writeFile('2.txt', '2'); + fs.writeFile('3.txt', '3'); + tree.delete('3.txt'); + fs.writeFile('4.md', '4'); + + const withTree = glob(tree, ['*.txt']).sort(); + + expect(withTree).toMatchInlineSnapshot(` + [ + "1.txt", + "2.txt", + ] + `); + }); +}); diff --git a/packages/nx/src/generators/utils/glob.ts b/packages/nx/src/generators/utils/glob.ts new file mode 100644 index 0000000000000..3c3f5f366da4e --- /dev/null +++ b/packages/nx/src/generators/utils/glob.ts @@ -0,0 +1,38 @@ +import { Tree } from '../tree'; +import { combineGlobPatterns } from '../../utils/globs'; +import { workspaceRoot } from '../../utils/workspace-root'; +import { globWithWorkspaceContext } from '../../utils/workspace-context'; + +import minimatch = require('minimatch'); + +/** + * Performs a tree-aware glob search on the files in a workspace. Able to find newly + * created files and hides deleted files before the updates are committed to disk. + * Paths should be unix-style with forward slashes. + * + * @param tree The file system tree + * @param patterns A list of glob patterns + * @returns Normalized paths in the workspace that match the provided glob patterns. + */ +export function glob(tree: Tree, patterns: string[]): string[] { + const matches = new Set(globWithWorkspaceContext(tree.root, patterns)); + + const combinedGlob = combineGlobPatterns(patterns); + const matcher = minimatch.makeRe(combinedGlob); + + if (!matcher) { + throw new Error('Invalid glob pattern: ' + combinedGlob); + } + + for (const change of tree.listChanges()) { + if (change.type !== 'UPDATE' && matcher.test(change.path)) { + if (change.type === 'CREATE') { + matches.add(change.path); + } else if (change.type === 'DELETE') { + matches.delete(change.path); + } + } + } + + return Array.from(matches); +} diff --git a/packages/nx/src/generators/utils/project-configuration.spec.ts b/packages/nx/src/generators/utils/project-configuration.spec.ts index 1f9b95910ce15..90e2908904dc6 100644 --- a/packages/nx/src/generators/utils/project-configuration.spec.ts +++ b/packages/nx/src/generators/utils/project-configuration.spec.ts @@ -4,7 +4,7 @@ import { ProjectConfiguration } from '../../config/workspace-json-project-json'; import { createTree } from '../testing-utils/create-tree'; import { createTreeWithEmptyWorkspace } from '../testing-utils/create-tree-with-empty-workspace'; -import { readJson, updateJson, writeJson } from '../utils/json'; +import { readJson, writeJson } from '../utils/json'; import { addProjectConfiguration, getProjects, diff --git a/packages/nx/src/hasher/task-hasher.spec.ts b/packages/nx/src/hasher/task-hasher.spec.ts index 0c972efe3561b..9f2bd7d594f16 100644 --- a/packages/nx/src/hasher/task-hasher.spec.ts +++ b/packages/nx/src/hasher/task-hasher.spec.ts @@ -1,20 +1,14 @@ // This must come before the Hasher import -import { TempFs } from '../utils/testing/temp-fs'; +import { TempFs } from '../internal-testing-utils/temp-fs'; let tempFs = new TempFs('TaskHasher'); + import { DependencyType } from '../config/project-graph'; import { expandNamedInput, filterUsingGlobPatterns, - Hash, InProcessTaskHasher, } from './task-hasher'; -import { withEnvironmentVariables } from '../../internal-testing-utils/with-environment'; - -jest.mock('../utils/workspace-root', () => { - return { - workspaceRoot: tempFs.tempDir, - }; -}); +import { withEnvironmentVariables } from '../internal-testing-utils/with-environment'; describe('TaskHasher', () => { const packageJson = { @@ -53,6 +47,10 @@ describe('TaskHasher', () => { tempFs.reset(); }); + afterAll(() => { + tempFs.cleanup(); + }); + it('should create task hash', () => withEnvironmentVariables({ TESTENV: 'env123' }, async () => { const hasher = new InProcessTaskHasher( diff --git a/packages/nx/internal-testing-utils/assert-valid-migrations.ts b/packages/nx/src/internal-testing-utils/assert-valid-migrations.ts similarity index 90% rename from packages/nx/internal-testing-utils/assert-valid-migrations.ts rename to packages/nx/src/internal-testing-utils/assert-valid-migrations.ts index e0b325e5d527d..4902ca6a04edb 100644 --- a/packages/nx/internal-testing-utils/assert-valid-migrations.ts +++ b/packages/nx/src/internal-testing-utils/assert-valid-migrations.ts @@ -1,7 +1,4 @@ -import { - MigrationsJson, - MigrationsJsonEntry, -} from '../src/config/misc-interfaces'; +import { MigrationsJson, MigrationsJsonEntry } from '../config/misc-interfaces'; import * as path from 'path'; export function assertValidMigrationPaths(json: MigrationsJson, root: string) { diff --git a/packages/nx/src/utils/testing/mock-fs.ts b/packages/nx/src/internal-testing-utils/mock-fs.ts similarity index 100% rename from packages/nx/src/utils/testing/mock-fs.ts rename to packages/nx/src/internal-testing-utils/mock-fs.ts diff --git a/packages/nx/internal-testing-utils/run-migration-against-this-workspace.ts b/packages/nx/src/internal-testing-utils/run-migration-against-this-workspace.ts similarity index 89% rename from packages/nx/internal-testing-utils/run-migration-against-this-workspace.ts rename to packages/nx/src/internal-testing-utils/run-migration-against-this-workspace.ts index 4e958b34f0666..0d8056a2e8d83 100644 --- a/packages/nx/internal-testing-utils/run-migration-against-this-workspace.ts +++ b/packages/nx/src/internal-testing-utils/run-migration-against-this-workspace.ts @@ -1,4 +1,4 @@ -import { FsTree, Tree } from '../src/generators/tree'; +import { FsTree, Tree } from '../generators/tree'; import { join } from 'path'; export function assertRunsAgainstNxRepo( diff --git a/packages/nx/src/utils/testing/temp-fs.ts b/packages/nx/src/internal-testing-utils/temp-fs.ts similarity index 87% rename from packages/nx/src/utils/testing/temp-fs.ts rename to packages/nx/src/internal-testing-utils/temp-fs.ts index 47048dfc28ef8..0d48a5f58f1ee 100644 --- a/packages/nx/src/utils/testing/temp-fs.ts +++ b/packages/nx/src/internal-testing-utils/temp-fs.ts @@ -10,8 +10,9 @@ import { rmSync, unlinkSync, } from 'fs-extra'; -import { joinPathFragments } from '../path'; +import { joinPathFragments } from '../utils/path'; import { appendFileSync, existsSync, renameSync, writeFileSync } from 'fs'; +import { setWorkspaceRoot, workspaceRoot } from '../utils/workspace-root'; type NestedFiles = { [fileName: string]: string; @@ -19,10 +20,15 @@ type NestedFiles = { export class TempFs { readonly tempDir: string; + + private previousWorkspaceRoot: string; + constructor(private dirname: string, overrideWorkspaceRoot = true) { this.tempDir = realpathSync(mkdtempSync(join(tmpdir(), this.dirname))); + this.previousWorkspaceRoot = workspaceRoot; + if (overrideWorkspaceRoot) { - process.env.NX_WORKSPACE_ROOT_PATH = this.tempDir; + setWorkspaceRoot(this.tempDir); } } @@ -76,6 +82,7 @@ export class TempFs { cleanup() { rmSync(this.tempDir, { recursive: true }); + setWorkspaceRoot(this.previousWorkspaceRoot); } reset() { diff --git a/packages/nx/internal-testing-utils/with-environment.ts b/packages/nx/src/internal-testing-utils/with-environment.ts similarity index 100% rename from packages/nx/internal-testing-utils/with-environment.ts rename to packages/nx/src/internal-testing-utils/with-environment.ts diff --git a/packages/nx/src/migrations/update-14-0-6/remove-roots.spec.ts b/packages/nx/src/migrations/update-14-0-6/remove-roots.spec.ts index 8d63ad4ae87c8..21759e2a7f0ce 100644 --- a/packages/nx/src/migrations/update-14-0-6/remove-roots.spec.ts +++ b/packages/nx/src/migrations/update-14-0-6/remove-roots.spec.ts @@ -3,7 +3,7 @@ import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/cre import { addProjectConfiguration } from '../../generators/utils/project-configuration'; import { readJson, updateJson, writeJson } from '../../generators/utils/json'; import removeRoots from './remove-roots'; -import { assertRunsAgainstNxRepo } from '../../../internal-testing-utils/run-migration-against-this-workspace'; +import { assertRunsAgainstNxRepo } from '../../internal-testing-utils/run-migration-against-this-workspace'; describe('remove-roots >', () => { let tree: Tree; diff --git a/packages/nx/src/migrations/update-14-2-0/add-json-schema.spec.ts b/packages/nx/src/migrations/update-14-2-0/add-json-schema.spec.ts index 58b973362d7bf..c55b52742b3ee 100644 --- a/packages/nx/src/migrations/update-14-2-0/add-json-schema.spec.ts +++ b/packages/nx/src/migrations/update-14-2-0/add-json-schema.spec.ts @@ -1,4 +1,4 @@ -import { assertRunsAgainstNxRepo } from '../../../internal-testing-utils/run-migration-against-this-workspace'; +import { assertRunsAgainstNxRepo } from '../../internal-testing-utils/run-migration-against-this-workspace'; import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; import type { Tree } from '../../generators/tree'; import { readJson, writeJson } from '../../generators/utils/json'; diff --git a/packages/nx/src/migrations/update-14-2-0/remove-default-collection.spec.ts b/packages/nx/src/migrations/update-14-2-0/remove-default-collection.spec.ts index 4dda965350bd5..cb01bdcdb216e 100644 --- a/packages/nx/src/migrations/update-14-2-0/remove-default-collection.spec.ts +++ b/packages/nx/src/migrations/update-14-2-0/remove-default-collection.spec.ts @@ -4,7 +4,7 @@ import { readNxJson, updateNxJson, } from '../../generators/utils/project-configuration'; -import { assertRunsAgainstNxRepo } from '../../../internal-testing-utils/run-migration-against-this-workspace'; +import { assertRunsAgainstNxRepo } from '../../internal-testing-utils/run-migration-against-this-workspace'; import removeDefaultCollection from './remove-default-collection'; describe('remove-default-collection', () => { diff --git a/packages/nx/src/migrations/update-14-3-4/create-target-defaults.spec.ts b/packages/nx/src/migrations/update-14-3-4/create-target-defaults.spec.ts index b79a95b72201c..4207ae6ea9914 100644 --- a/packages/nx/src/migrations/update-14-3-4/create-target-defaults.spec.ts +++ b/packages/nx/src/migrations/update-14-3-4/create-target-defaults.spec.ts @@ -1,4 +1,4 @@ -import { assertRunsAgainstNxRepo } from '../../../internal-testing-utils/run-migration-against-this-workspace'; +import { assertRunsAgainstNxRepo } from '../../internal-testing-utils/run-migration-against-this-workspace'; import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; import type { Tree } from '../../generators/tree'; import { diff --git a/packages/nx/src/migrations/update-15-0-0/migrate-to-inputs.spec.ts b/packages/nx/src/migrations/update-15-0-0/migrate-to-inputs.spec.ts index 74f861657df3e..903f8c452de4a 100644 --- a/packages/nx/src/migrations/update-15-0-0/migrate-to-inputs.spec.ts +++ b/packages/nx/src/migrations/update-15-0-0/migrate-to-inputs.spec.ts @@ -1,4 +1,4 @@ -import { assertRunsAgainstNxRepo } from '../../../internal-testing-utils/run-migration-against-this-workspace'; +import { assertRunsAgainstNxRepo } from '../../internal-testing-utils/run-migration-against-this-workspace'; import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; import type { Tree } from '../../generators/tree'; import { diff --git a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts index 64715aee193b6..fed29c7aa59fb 100644 --- a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts +++ b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts @@ -7,7 +7,7 @@ import { import update from './update-depends-on-to-tokens'; import { updateJson, writeJson } from '../../generators/utils/json'; import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; -import { assertRunsAgainstNxRepo } from '../../../internal-testing-utils/run-migration-against-this-workspace'; +import { assertRunsAgainstNxRepo } from '../../internal-testing-utils/run-migration-against-this-workspace'; describe('update-depends-on-to-tokens', () => { it('should update nx.json', async () => { diff --git a/packages/nx/src/migrations/update-16-2-0/remove-run-commands-output-path.spec.ts b/packages/nx/src/migrations/update-16-2-0/remove-run-commands-output-path.spec.ts index e935d1db65d1c..f0d48d98fe2e3 100644 --- a/packages/nx/src/migrations/update-16-2-0/remove-run-commands-output-path.spec.ts +++ b/packages/nx/src/migrations/update-16-2-0/remove-run-commands-output-path.spec.ts @@ -5,7 +5,7 @@ import { addProjectConfiguration, readProjectConfiguration, } from '../../generators/utils/project-configuration'; -import { assertRunsAgainstNxRepo } from '../../../internal-testing-utils/run-migration-against-this-workspace'; +import { assertRunsAgainstNxRepo } from '../../internal-testing-utils/run-migration-against-this-workspace'; import removeRunCommandsOutputPath from './remove-run-commands-output-path'; describe('removeRunCommandsOutputPath', () => { diff --git a/packages/nx/src/native/index.d.ts b/packages/nx/src/native/index.d.ts index eafc88cf0fe8d..11cf9ee1975c6 100644 --- a/packages/nx/src/native/index.d.ts +++ b/packages/nx/src/native/index.d.ts @@ -113,7 +113,7 @@ export class WorkspaceContext { workspaceRoot: string constructor(workspaceRoot: string) getWorkspaceFiles(globs: Array, parseConfigurations: (arg0: Array) => ConfigurationParserResult): NxWorkspaceFiles - getProjectConfigurationFiles(globs: Array): Array + glob(globs: Array): Array getProjectConfigurations(globs: Array, parseConfigurations: (arg0: Array) => ConfigurationParserResult): ConfigurationParserResult incrementalUpdate(updatedFiles: Array, deletedFiles: Array): Record allFileData(): Array diff --git a/packages/nx/src/native/tests/watcher.spec.ts b/packages/nx/src/native/tests/watcher.spec.ts index f5d822bf1461c..954b593facdee 100644 --- a/packages/nx/src/native/tests/watcher.spec.ts +++ b/packages/nx/src/native/tests/watcher.spec.ts @@ -1,6 +1,5 @@ -import { TempFs } from '../../utils/testing/temp-fs'; +import { TempFs } from '../../internal-testing-utils/temp-fs'; import { Watcher } from '../index'; -import { realpathSync } from 'fs-extra'; describe('watcher', () => { let temp: TempFs; diff --git a/packages/nx/src/native/tests/workspace_files.spec.ts b/packages/nx/src/native/tests/workspace_files.spec.ts index 60a1b34e4c01a..cd5b716b3d00c 100644 --- a/packages/nx/src/native/tests/workspace_files.spec.ts +++ b/packages/nx/src/native/tests/workspace_files.spec.ts @@ -1,5 +1,5 @@ import { WorkspaceContext } from '../index'; -import { TempFs } from '../../utils/testing/temp-fs'; +import { TempFs } from '../../internal-testing-utils/temp-fs'; import { NxJsonConfiguration } from '../../config/nx-json'; import { dirname, join } from 'path'; import { readJsonFile } from '../../utils/fileutils'; diff --git a/packages/nx/src/native/workspace/config_files.rs b/packages/nx/src/native/workspace/config_files.rs index 2adf3fec71a7e..230498d67abe8 100644 --- a/packages/nx/src/native/workspace/config_files.rs +++ b/packages/nx/src/native/workspace/config_files.rs @@ -7,7 +7,7 @@ use rayon::prelude::*; use std::path::PathBuf; /// Get workspace config files based on provided globs -pub(super) fn get_project_configuration_files( +pub(super) fn glob_files( globs: Vec, files: Option<&[(PathBuf, String)]>, ) -> napi::Result, WorkspaceErrors> { @@ -34,7 +34,7 @@ where ConfigurationParser: Fn(Vec) -> napi::Result, { let config_paths = - get_project_configuration_files(globs, files).map_err(anyhow::Error::from)?; + glob_files(globs, files).map_err(anyhow::Error::from)?; parse_configurations(config_paths) } diff --git a/packages/nx/src/native/workspace/context.rs b/packages/nx/src/native/workspace/context.rs index 3bd8989d0c989..0fe836ced56dc 100644 --- a/packages/nx/src/native/workspace/context.rs +++ b/packages/nx/src/native/workspace/context.rs @@ -166,11 +166,11 @@ impl WorkspaceContext { } #[napi] - pub fn get_project_configuration_files( + pub fn glob( &self, globs: Vec, ) -> napi::Result, WorkspaceErrors> { - config_files::get_project_configuration_files( + config_files::glob_files( globs, self.files_worker .get_files() diff --git a/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts b/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts index 6e384c43a46a7..2b063652095d7 100644 --- a/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts +++ b/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts @@ -1,4 +1,4 @@ -import { TempFs } from '../../../../utils/testing/temp-fs'; +import { TempFs } from '../../../../internal-testing-utils/temp-fs'; const tempFs = new TempFs('explicit-package-json'); import { buildExplicitPackageJsonDependencies } from './explicit-package-json-dependencies'; diff --git a/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts b/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts index 48fcdb4966fa0..ab0f3522a9834 100644 --- a/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts +++ b/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts @@ -1,4 +1,4 @@ -import { TempFs } from '../../../../utils/testing/temp-fs'; +import { TempFs } from '../../../../internal-testing-utils/temp-fs'; const tempFs = new TempFs('explicit-project-deps'); import { ProjectGraphBuilder } from '../../../../project-graph/project-graph-builder'; diff --git a/packages/nx/src/plugins/project-json/build-nodes/project-json.spec.ts b/packages/nx/src/plugins/project-json/build-nodes/project-json.spec.ts index 6631f068edf65..674ec15196879 100644 --- a/packages/nx/src/plugins/project-json/build-nodes/project-json.spec.ts +++ b/packages/nx/src/plugins/project-json/build-nodes/project-json.spec.ts @@ -1,6 +1,6 @@ import * as memfs from 'memfs'; -import '../../../utils/testing/mock-fs'; +import '../../../internal-testing-utils/mock-fs'; import { PackageJson } from '../../../utils/package-json'; diff --git a/packages/nx/src/project-graph/utils/retrieve-workspace-files.spec.ts b/packages/nx/src/project-graph/utils/retrieve-workspace-files.spec.ts index 59dcecf842782..dac5b73750ec8 100644 --- a/packages/nx/src/project-graph/utils/retrieve-workspace-files.spec.ts +++ b/packages/nx/src/project-graph/utils/retrieve-workspace-files.spec.ts @@ -1,4 +1,4 @@ -import { TempFs } from '../../utils/testing/temp-fs'; +import { TempFs } from '../../internal-testing-utils/temp-fs'; import { retrieveProjectConfigurationPaths } from './retrieve-workspace-files'; describe('retrieveProjectConfigurationPaths', () => { diff --git a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts index 4c736b3865ad1..775caadaa269e 100644 --- a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts +++ b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts @@ -28,7 +28,7 @@ import { } from '../../utils/nx-plugin'; import { CreateProjectJsonProjectsPlugin } from '../../plugins/project-json/build-nodes/project-json'; import { - getProjectConfigurationFilesFromContext, + globWithWorkspaceContext, getProjectConfigurationsFromContext, getNxWorkspaceFilesFromContext, } from '../../utils/workspace-context'; @@ -201,16 +201,13 @@ export async function retrieveProjectConfigurationPaths( root, await loadNxPlugins(nxJson?.plugins ?? [], getNxRequirePaths(root), root) ); - return getProjectConfigurationFilesFromContext(root, projectGlobPatterns); + return globWithWorkspaceContext(root, projectGlobPatterns); } export function retrieveProjectConfigurationPathsWithoutPluginInference( root: string ): string[] { - return getProjectConfigurationFilesFromContext( - root, - configurationGlobsWithoutPlugins(root) - ); + return globWithWorkspaceContext(root, configurationGlobsWithoutPlugins(root)); } const projectsWithoutPluginCache = new Map< diff --git a/packages/nx/src/utils/command-line-utils.spec.ts b/packages/nx/src/utils/command-line-utils.spec.ts index 71d3328597ac9..dc82ffbde607b 100644 --- a/packages/nx/src/utils/command-line-utils.spec.ts +++ b/packages/nx/src/utils/command-line-utils.spec.ts @@ -1,5 +1,5 @@ import { splitArgsIntoNxArgsAndOverrides } from './command-line-utils'; -import { withEnvironmentVariables as withEnvironment } from '../../internal-testing-utils/with-environment'; +import { withEnvironmentVariables as withEnvironment } from '../internal-testing-utils/with-environment'; jest.mock('../project-graph/file-utils'); diff --git a/packages/nx/src/utils/workspace-context.ts b/packages/nx/src/utils/workspace-context.ts index f9afef9dcb9e4..d7a275aaa1018 100644 --- a/packages/nx/src/utils/workspace-context.ts +++ b/packages/nx/src/utils/workspace-context.ts @@ -25,12 +25,12 @@ export function getNxWorkspaceFilesFromContext( return workspaceContext.getWorkspaceFiles(globs, parseConfigurations); } -export function getProjectConfigurationFilesFromContext( +export function globWithWorkspaceContext( workspaceRoot: string, globs: string[] ) { ensureContextAvailable(workspaceRoot); - return workspaceContext.getProjectConfigurationFiles(globs); + return workspaceContext.glob(globs); } export function getProjectConfigurationsFromContext( diff --git a/packages/nx/tsconfig.lib.json b/packages/nx/tsconfig.lib.json index 43635e831d029..f569baac44676 100644 --- a/packages/nx/tsconfig.lib.json +++ b/packages/nx/tsconfig.lib.json @@ -11,7 +11,7 @@ "**/*_spec.ts", "jest.config.ts", "**/__fixtures__/**/*.*", - "internal-testing-utils/**/*.ts" + "./src/internal-testing-utils/**/*.ts" ], "include": ["**/*.ts"] } diff --git a/packages/nx/tsconfig.spec.json b/packages/nx/tsconfig.spec.json index be669d0b3b981..e4ae1d418cfc5 100644 --- a/packages/nx/tsconfig.spec.json +++ b/packages/nx/tsconfig.spec.json @@ -12,7 +12,7 @@ "**/*.spec.js", "**/*.spec.jsx", "**/*.d.ts", - "internal-testing-utils/**/*.ts", + "./src/internal-testing-utils/**/*.ts", "jest.config.ts" ] }