Skip to content

Commit

Permalink
chore(core): review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Sep 13, 2023
1 parent e792542 commit 7bf5f48
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 75 deletions.
21 changes: 2 additions & 19 deletions docs/generated/devkit/glob.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
# Function: glob

**glob**(`patterns`): `Promise`<`string`[]\>

Performs a glob search on the files in a workspace. Paths should be unix-style
with forward slashes.

#### Parameters

| Name | Type | Description |
| :--------- | :--------- | :---------------------- |
| `patterns` | `string`[] | A list of glob patterns |

#### Returns

`Promise`<`string`[]\>

Normalized paths in the workspace that match the provided glob patterns.

**glob**(`tree`, `patterns`): `Promise`<`string`[]\>
**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.
Expand All @@ -32,6 +15,6 @@ Paths should be unix-style with forward slashes.

#### Returns

`Promise`<`string`[]\>
`string`[]

Normalized paths in the workspace that match the provided glob patterns.
10 changes: 4 additions & 6 deletions packages/nx/src/generators/utils/glob.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ describe('glob', () => {
fs.writeFile('3.txt', '3');
fs.writeFile('4.md', '4');

const withTree = (await glob(tree, ['*.txt'])).sort();
const withoutTree = (await glob(['*.txt'])).sort();
const results = glob(tree, ['*.txt']).sort();

expect(withTree).toEqual(withoutTree);
expect(withTree).toMatchInlineSnapshot(`
expect(results).toMatchInlineSnapshot(`
[
"1.txt",
"2.txt",
Expand All @@ -40,7 +38,7 @@ describe('glob', () => {
tree.write('3.txt', '3');
fs.writeFile('4.md', '4');

const withTree = (await glob(tree, ['*.txt'])).sort();
const withTree = glob(tree, ['*.txt']).sort();

expect(withTree).toMatchInlineSnapshot(`
[
Expand All @@ -58,7 +56,7 @@ describe('glob', () => {
tree.delete('3.txt');
fs.writeFile('4.md', '4');

const withTree = (await glob(tree, ['*.txt'])).sort();
const withTree = glob(tree, ['*.txt']).sort();

expect(withTree).toMatchInlineSnapshot(`
[
Expand Down
48 changes: 13 additions & 35 deletions packages/nx/src/generators/utils/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import { globWithWorkspaceContext } from '../../utils/workspace-context';

import minimatch = require('minimatch');

/**
* Performs a glob search on the files in a workspace. Paths should be unix-style
* with forward slashes.
*
* @param patterns A list of glob patterns
* @returns Normalized paths in the workspace that match the provided glob patterns.
*/
export async function glob(patterns: string[]): Promise<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.
Expand All @@ -23,35 +14,22 @@ export async function glob(patterns: string[]): Promise<string[]>;
* @param patterns A list of glob patterns
* @returns Normalized paths in the workspace that match the provided glob patterns.
*/
export async function glob(tree: Tree, patterns: string[]): Promise<string[]>;

export async function glob(
treeOrPatterns: Tree | string[],
maybePatterns?: string[]
): Promise<string[]> {
const { patterns, root } =
'root' in treeOrPatterns
? { patterns: maybePatterns, root: treeOrPatterns.root }
: { patterns: treeOrPatterns, root: workspaceRoot };

const matches = new Set(globWithWorkspaceContext(root, patterns));
export function glob(tree: Tree, patterns: string[]): string[] {
const matches = new Set(globWithWorkspaceContext(tree.root, patterns));

if ('root' in treeOrPatterns) {
const tree = treeOrPatterns;
const combinedGlob = combineGlobPatterns(patterns);
const matcher = minimatch.makeRe(combinedGlob);
const combinedGlob = combineGlobPatterns(patterns);
const matcher = minimatch.makeRe(combinedGlob);

if (!matcher) {
throw new Error('Invalid glob pattern: ' + 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);
}
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);
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions packages/nx/src/hasher/task-hasher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import {
} from './task-hasher';
import { withEnvironmentVariables } from '../internal-testing-utils/with-environment';

jest.mock('../utils/workspace-root', () => {
return {
workspaceRoot: tempFs.tempDir,
};
});

describe('TaskHasher', () => {
const packageJson = {
name: 'nrwl',
Expand Down
5 changes: 0 additions & 5 deletions packages/nx/src/internal-testing-utils/temp-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ type NestedFiles = {
export class TempFs {
readonly tempDir: string;

private previousEnvWorkspaceRootPath: string;
private previousWorkspaceRoot: string;

constructor(private dirname: string, overrideWorkspaceRoot = true) {
this.tempDir = realpathSync(mkdtempSync(join(tmpdir(), this.dirname)));

this.previousEnvWorkspaceRootPath = process.env.NX_WORKSPACE_ROOT_PATH;
this.previousWorkspaceRoot = workspaceRoot;

if (overrideWorkspaceRoot) {
process.env.NX_WORKSPACE_ROOT_PATH = this.tempDir;
setWorkspaceRoot(this.tempDir);
}
}
Expand Down Expand Up @@ -86,7 +82,6 @@ export class TempFs {

cleanup() {
rmSync(this.tempDir, { recursive: true });
process.env.NX_WORKSPACE_ROOT_PATH = this.previousEnvWorkspaceRootPath;
setWorkspaceRoot(this.previousWorkspaceRoot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,7 @@ export async function retrieveProjectConfigurationPaths(
export function retrieveProjectConfigurationPathsWithoutPluginInference(
root: string
): string[] {
return globWithWorkspaceContext(
root,
configurationGlobsWithoutPlugins(root)
);
return globWithWorkspaceContext(root, configurationGlobsWithoutPlugins(root));
}

const projectsWithoutPluginCache = new Map<
Expand Down

0 comments on commit 7bf5f48

Please sign in to comment.