nx/packages/devkit/src/generators/visit-not-ignored-files.ts
2021-01-19 16:45:44 -05:00

35 lines
772 B
TypeScript

import { Tree } from '@nrwl/tao/src/shared/tree';
import { join } from 'path';
import ignore from 'ignore';
/**
* 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
) {
let ig;
if (tree.exists('.gitignore')) {
ig = ignore();
ig.add(tree.read('.gitignore').toString());
}
if (dirPath !== '' && ig?.ignores(dirPath)) {
return;
}
for (const child of tree.children(dirPath)) {
const fullPath = join(dirPath, child);
if (ig?.ignores(fullPath)) {
continue;
}
if (tree.isFile(fullPath)) {
visitor(fullPath);
} else {
visitNotIgnoredFiles(tree, fullPath, visitor);
}
}
}