Skip to content

Commit

Permalink
feat(core): address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold authored and AgentEnder committed Jun 8, 2022
1 parent 123336f commit b36e1a6
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 74 deletions.
24 changes: 12 additions & 12 deletions docs/generated/devkit/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ It only uses language primitives and immutable objects
- [applyChangesToString](../../devkit/index#applychangestostring)
- [convertNxExecutor](../../devkit/index#convertnxexecutor)
- [convertNxGenerator](../../devkit/index#convertnxgenerator)
- [createIgnoreFromFS](../../devkit/index#createignorefromfs)
- [createIgnore](../../devkit/index#createignore)
- [createIgnoreFromTree](../../devkit/index#createignorefromtree)
- [createProjectGraphAsync](../../devkit/index#createprojectgraphasync)
- [defaultTasksRunner](../../devkit/index#defaulttasksrunner)
Expand Down Expand Up @@ -899,19 +899,19 @@ Convert an Nx Generator into an Angular Devkit Schematic.

---

### createIgnoreFromFS
### createIgnore

**createIgnoreFromFS**(`rootDir`, `ignoreFiles`): `Ignore`
**createIgnore**(`rootDir?`, `ignoreFiles?`): `Ignore`

Reads ignore files from the file system and returns an object which can be
used to check whether a file should be ignored.

#### Parameters

| Name | Type | Description |
| :------------ | :--------- | :------------------------------------------------------------------------------------------------------------------------------------- |
| `rootDir` | `string` | The directory in which to start searching for ignore files. Paths evaluated by the returned object must be relative to this directory. |
| `ignoreFiles` | `string`[] | The filename of ignore files to include, e.g. ".gitignore" |
| Name | Type | Default value | Description |
| :------------ | :--------- | :--------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `rootDir` | `string` | `workspaceRoot` | The directory in which to start searching for ignore files. Paths evaluated by the returned object must be relative to this directory. Defaults to the workspace root. |
| `ignoreFiles` | `string`[] | `workspaceIgnoreFiles` | The filename of ignore files to include, e.g. ".gitignore". Defaults to [".gitignore", ".nxignore"]. |

#### Returns

Expand All @@ -921,17 +921,17 @@ used to check whether a file should be ignored.

### createIgnoreFromTree

**createIgnoreFromTree**(`tree`, `ignoreFiles`): `Ignore`
**createIgnoreFromTree**(`tree`, `ignoreFiles?`): `Ignore`

Reads ignore files from a Tree and returns an object which can be
used to check whether a file should be ignored.

#### Parameters

| Name | Type | Description |
| :------------ | :-------------------------------- | :------------------------------------------------------------------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/index#tree) | The tree in which to searching for ignore files. Paths evaluated by the returned object must be relative to the tree root. |
| `ignoreFiles` | `string`[] | The filename of ignore files to include, e.g. ".gitignore" |
| Name | Type | Default value | Description |
| :------------ | :-------------------------------- | :--------------------- | :------------------------------------------------------------------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/index#tree) | `undefined` | The tree in which to searching for ignore files. Paths evaluated by the returned object must be relative to the tree root. |
| `ignoreFiles` | `string`[] | `workspaceIgnoreFiles` | The filename of ignore files to include, e.g. ".gitignore". Defaults to [".gitignore", ".nxignore"]. |

#### Returns

Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/devkit.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/add-nx-to-monorepo/src/add-nx-to-monorepo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import {
createIgnoreFromFS,
createIgnore,
output,
readJsonFile,
writeJsonFile,
Expand Down Expand Up @@ -94,7 +94,7 @@ function allProjectPackageJsonFiles(repoRoot: string) {
}

function allPackageJsonFiles(repoRoot: string, dirName: string) {
const ignore = createIgnoreFromFS(repoRoot, ['.gitignore']);
const ignore = createIgnore(repoRoot, ['.gitignore']);
const relDirName = path.relative(repoRoot, dirName);
if (
relDirName &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@nrwl/devkit';
import { execFileSync, fork } from 'child_process';
import { watch } from 'chokidar';
import { createWorkspaceIgnore } from 'nx/src/utils/ignore';
import { createIgnore } from 'nx/src/utils/ignore';
import { readModulePackageJson } from 'nx/src/utils/package-json';
import { platform } from 'os';
import { resolve } from 'path';
Expand Down Expand Up @@ -87,7 +87,7 @@ function createFileWatcher(
root: string,
changeHandler: () => void
): () => void {
const ignore = createWorkspaceIgnore(root);
const ignore = createIgnore(root);
const layout = workspaceLayout();

const watcher = watch(
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/generators/stories/lib/tree-utilities.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Tree } from '@nrwl/devkit';
import { joinPathFragments } from '@nrwl/devkit';
import { createGitignoreFromTree } from 'nx/src/utils/ignore';
import { createGitIgnoreFromTree } from 'nx/src/utils/ignore';

export function getAllFilesRecursivelyFromDir(
tree: Tree,
dir: string
): string[] {
const ignore = createGitignoreFromTree(tree);
const ignore = createGitIgnoreFromTree(tree);

function inner(dir: string) {
if (ignore.ignores(dir)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,4 @@ export { cacheDir } from 'nx/src/utils/cache-directory';
/**
* @category Utils
*/
export { createIgnoreFromFS, createIgnoreFromTree } from 'nx/src/utils/ignore';
export { createIgnore, createIgnoreFromTree } from 'nx/src/utils/ignore';
4 changes: 2 additions & 2 deletions packages/devkit/src/generators/visit-not-ignored-files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Tree } from 'nx/src/generators/tree';
import { createGitignoreFromTree } from 'nx/src/utils/ignore';
import { createGitIgnoreFromTree } from 'nx/src/utils/ignore';
import { join, relative, sep } from 'path';

/**
Expand All @@ -10,7 +10,7 @@ export function visitNotIgnoredFiles(
dirPath: string = tree.root,
visitor: (path: string) => void
): void {
const ignore = createGitignoreFromTree(tree);
const ignore = createGitIgnoreFromTree(tree);

function inner(dirPath: string) {
dirPath = normalizePathRelativeToRoot(dirPath, tree.root);
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/utils/copy-assets-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fg from 'fast-glob';
import * as fse from 'fs-extra';
import { Ignore } from 'ignore';
import * as minimatch from 'minimatch';
import { createWorkspaceIgnore } from 'nx/src/utils/ignore';
import { createIgnore } from 'nx/src/utils/ignore';
import * as path from 'path';

export type FileEventType = 'create' | 'update' | 'delete';
Expand Down Expand Up @@ -59,7 +59,7 @@ export class CopyAssetsHandler {
this.projectDir = opts.projectDir;
this.outputDir = opts.outputDir;
this.callback = opts.callback ?? defaultFileEventHandler;
this.ignore = createWorkspaceIgnore(opts.rootDir);
this.ignore = createIgnore(opts.rootDir);

this.assetGlobs = opts.assets.map((f) => {
let isGlob = false;
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/dep-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { defaultFileHasher } from '../hasher/file-hasher';
import { pruneExternalNodes } from '../project-graph/operators';
import { createProjectGraphAsync } from '../project-graph/project-graph';
import { writeJsonFile } from '../utils/fileutils';
import { createWorkspaceIgnore } from '../utils/ignore';
import { createIgnore } from '../utils/ignore';
import { output } from '../utils/output';
import { joinPathFragments } from '../utils/path';
import { workspaceRoot } from '../utils/workspace-root';
Expand Down Expand Up @@ -437,7 +437,7 @@ function debounce(fn: (...args) => void, time: number) {
}

function createFileWatcher(root: string, changeHandler: () => Promise<void>) {
const ignore = createWorkspaceIgnore(root);
const ignore = createIgnore(root);
const layout = workspaceLayout();

const watcher = watch(
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from 'path';
import { basename, dirname, join } from 'path';
import { performance } from 'perf_hooks';
import { readJsonFile } from '../utils/fileutils';
import { createWorkspaceIgnore } from '../utils/ignore';
import { createIgnore } from '../utils/ignore';
import { logger } from '../utils/logger';
import { loadNxPlugins, readPluginPackageJson } from '../utils/nx-plugin';
import { sortObjectByKeys } from '../utils/object-sort';
Expand Down Expand Up @@ -566,7 +566,7 @@ export function globForProjectFiles(
join(root, '.git'),
];

const ignore = createWorkspaceIgnore(root);
const ignore = createIgnore(root);

const globResults = globSync(combinedProjectGlobPattern, {
ignore: ALWAYS_IGNORE,
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/hasher/git-based-file-hasher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { performance } from 'perf_hooks';
import { createNxignore } from '../utils/ignore';
import { createNxIgnore } from '../utils/ignore';
import { workspaceRoot } from '../utils/workspace-root';
import { FileHasherBase } from './file-hasher-base';
import { getFileHashes, getGitHashForFiles } from './git-hasher';
Expand All @@ -15,7 +15,7 @@ export class GitBasedFileHasher extends FileHasherBase {
this.clear();

const gitResult = await getFileHashes(workspaceRoot);
const ignore = createNxignore();
const ignore = createNxIgnore();
gitResult.allFiles.forEach((hash, filename) => {
if (!ignore.ignores(filename)) {
this.fileHashes.set(filename, hash);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/hasher/node-based-file-hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readdirSync, statSync } from 'fs';
import { join, relative } from 'path';
import { performance } from 'perf_hooks';
import { FileData } from '../config/project-graph';
import { createWorkspaceIgnore } from '../utils/ignore';
import { createIgnore } from '../utils/ignore';
import { normalizePath } from '../utils/path';
import { stripIndents } from '../utils/strip-indents';
import { workspaceRoot } from '../utils/workspace-root';
Expand Down Expand Up @@ -65,7 +65,7 @@ export class NodeBasedFileHasher extends FileHasherBase {
}

function getIgnoredGlobs() {
const ig = createWorkspaceIgnore();
const ig = createIgnore();
ig.add(stripIndents`
node_modules
tmp
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/project-graph/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FileData } from '../config/project-graph';
import { toOldFormatOrNull, Workspaces } from '../config/workspaces';
import type { NxArgs } from '../utils/command-line-utils';
import { fileExists, readJsonFile } from '../utils/fileutils';
import { createWorkspaceIgnore } from '../utils/ignore';
import { createIgnore } from '../utils/ignore';
import { jsonDiff } from '../utils/json-diff';
import { workspaceRoot } from '../utils/workspace-root';

Expand Down Expand Up @@ -37,7 +37,7 @@ export function calculateFileChanges(
f: string,
r: void | string
) => string = defaultReadFileAtRevision,
ignore = createWorkspaceIgnore()
ignore = createIgnore()
): FileChange[] {
files = files.filter((f) => !ignore.ignores(f));

Expand Down
29 changes: 14 additions & 15 deletions packages/nx/src/utils/ignore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { vol } from 'memfs';
import { createTree } from '../generators/testing-utils/create-tree';
import { Tree } from '../generators/tree';
import {
createCombinedIgnore,
createIgnoreFromFS,
createIgnore,
createIgnoreFromTree,
} from './ignore';
import { stripIndents } from './strip-indents';

describe('createIgnoreFromFS', () => {
let tmpDir: string;

beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nx-createIgnoreFromFS-'));
});
jest.mock('fs', () => require('memfs').fs);

describe('createIgnore', () => {
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true });
vol.reset();
});

it('should combine all ignore files in tree', () => {
fs.writeFileSync(path.join(tmpDir, '.ignore'), 'a');
fs.mkdirSync(path.join(tmpDir, 'b'));
fs.writeFileSync(path.join(tmpDir, 'b/.ignore'), 'c');
const ignore = createIgnoreFromFS(tmpDir, ['.ignore']);
vol.fromJSON(
{
'.ignore': 'a',
'b/.ignore': 'c',
},
'/root'
);

const ignore = createIgnore('/root', ['.ignore']);
expect(ignore.ignores('a')).toBe(true);
expect(ignore.ignores('b/c')).toBe(true);
});
Expand Down
40 changes: 17 additions & 23 deletions packages/nx/src/utils/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@ import * as fs from 'fs';
import ignore, { Ignore } from 'ignore';
import * as path from 'path';
import { Tree } from '../generators/tree';
import { workspaceRoot } from './workspace-root';
import { normalizePath } from './path';
import { workspaceRoot } from './workspace-root';

const workspaceIgnoreFiles = ['.gitignore', '.nxignore'];

export function createWorkspaceIgnore(root = workspaceRoot): Ignore {
return createIgnoreFromFS(root, workspaceIgnoreFiles);
}

export function createNxignore(root: string = workspaceRoot): Ignore {
return createIgnoreFromFS(root, ['.nxignore']);
export function createNxIgnore(root: string = workspaceRoot): Ignore {
return createIgnore(root, ['.nxignore']);
}

export function createGitignoreFromTree(tree: Tree): Ignore {
export function createGitIgnoreFromTree(tree: Tree): Ignore {
return createIgnoreFromTree(tree, ['.gitignore']);
}

export function readWorkspaceIgnorePatterns(root = workspaceRoot): string[] {
return combineIgnorePatterns(
workspaceIgnoreFiles.flatMap((ignoreFile) =>
readIgnoreFilesFromFS(root, ignoreFile)
readIgnoreFiles(root, ignoreFile)
)
);
}
Expand All @@ -40,16 +36,16 @@ export function createIgnoreFromPatterns(patterns: string[]): Ignore {
*
* @param rootDir The directory in which to start searching for ignore files.
* Paths evaluated by the returned object must be relative to this directory.
* @param ignoreFiles The filename of ignore files to include, e.g. ".gitignore"
* Defaults to the workspace root.
* @param ignoreFiles The filename of ignore files to include, e.g. ".gitignore".
* Defaults to [".gitignore", ".nxignore"].
*/
export function createIgnoreFromFS(
rootDir: string,
ignoreFiles: string[]
export function createIgnore(
rootDir = workspaceRoot,
ignoreFiles = workspaceIgnoreFiles
): Ignore {
return createCombinedIgnore(
ignoreFiles.flatMap((ignoreFile) =>
readIgnoreFilesFromFS(rootDir, ignoreFile)
)
ignoreFiles.flatMap((ignoreFile) => readIgnoreFiles(rootDir, ignoreFile))
);
}

Expand All @@ -59,11 +55,12 @@ export function createIgnoreFromFS(
*
* @param tree The tree in which to searching for ignore files.
* Paths evaluated by the returned object must be relative to the tree root.
* @param ignoreFiles The filename of ignore files to include, e.g. ".gitignore"
* @param ignoreFiles The filename of ignore files to include, e.g. ".gitignore".
* Defaults to [".gitignore", ".nxignore"].
*/
export function createIgnoreFromTree(
tree: Tree,
ignoreFiles: string[]
ignoreFiles = workspaceIgnoreFiles
): Ignore {
return createCombinedIgnore(
ignoreFiles.flatMap((ignoreFile) =>
Expand All @@ -72,7 +69,7 @@ export function createIgnoreFromTree(
);
}

export interface IgnoreFile {
interface IgnoreFile {
/**
* Path to the ignore file, relative to the root directory.
*/
Expand All @@ -83,10 +80,7 @@ export interface IgnoreFile {
content: string;
}

function readIgnoreFilesFromFS(
rootDir: string,
ignoreFile: string
): IgnoreFile[] {
function readIgnoreFiles(rootDir: string, ignoreFile: string): IgnoreFile[] {
const ignoreFilePaths = fastGlob.sync(`**/${ignoreFile}`, {
cwd: rootDir,
dot: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/executors/file-server/file-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import * as chalk from 'chalk';
import { execFileSync, fork } from 'child_process';
import { watch } from 'chokidar';
import { createWorkspaceIgnore } from 'nx/src/utils/ignore';
import { createIgnore } from 'nx/src/utils/ignore';
import { readModulePackageJson } from 'nx/src/utils/package-json';
import { platform } from 'os';
import { resolve } from 'path';
Expand Down Expand Up @@ -86,7 +86,7 @@ function createFileWatcher(
root: string,
changeHandler: () => void
): () => void {
const ignore = createWorkspaceIgnore(root);
const ignore = createIgnore(root);
const layout = workspaceLayout();

const watcher = watch(
Expand Down

0 comments on commit b36e1a6

Please sign in to comment.