-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
visit-not-ignored-files.ts
38 lines (34 loc) · 1 KB
/
visit-not-ignored-files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { Tree } from 'nx/src/generators/tree';
import { createGitIgnoreFromTree } from 'nx/src/utils/ignore';
import { join, relative, sep } from 'path';
/**
* Utility to act on all files in a tree that are not ignored by git.
*/
export function visitNotIgnoredFiles(
tree: Tree,
dirPath: string = tree.root,
visitor: (path: string) => void
): void {
const ignore = createGitIgnoreFromTree(tree);
function inner(dirPath: string) {
dirPath = normalizePathRelativeToRoot(dirPath, tree.root);
if (dirPath !== '' && ignore.ignores(dirPath)) {
return;
}
for (const child of tree.children(dirPath)) {
const fullPath = join(dirPath, child);
if (ignore.ignores(fullPath)) {
continue;
}
if (tree.isFile(fullPath)) {
visitor(fullPath);
} else {
inner(fullPath);
}
}
}
inner(dirPath);
}
function normalizePathRelativeToRoot(path: string, root: string): string {
return relative(root, join(root, path)).split(sep).join('/');
}