-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devkit): add method for tree-aware glob search (#19128)
- Loading branch information
1 parent
188dc8f
commit de2824a
Showing
44 changed files
with
196 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |
2 changes: 1 addition & 1 deletion
2
...devkit/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/eslint-plugin/src/rules/enforce-module-boundaries.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import 'nx/src/utils/testing/mock-fs'; | ||
import 'nx/src/internal-testing-utils/mock-fs'; | ||
|
||
import { | ||
ProjectGraph, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
...-testing-utils/assert-valid-migrations.ts → ...-testing-utils/assert-valid-migrations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...s/run-migration-against-this-workspace.ts → ...s/run-migration-against-this-workspace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/nx/src/migrations/update-14-2-0/add-json-schema.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/nx/src/migrations/update-14-3-4/create-target-defaults.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/nx/src/migrations/update-15-0-0/migrate-to-inputs.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
de2824a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev