Skip to content

Commit

Permalink
fix(devkit): tree.children should support writes to directories that …
Browse files Browse the repository at this point in the history
…have the same name as the parent
  • Loading branch information
AgentEnder committed Apr 3, 2023
1 parent b9c901b commit bafd1e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/nx/src/generators/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,16 @@ describe('tree', () => {
]);
});

it('should support nested dirs with same name as parent', () => {
tree.write('/parent-a/parent-a/parent-a-file.txt', 'parent content');
expect(tree.children('/parent-a')).toEqual([
'parent-a',
]);
expect(tree.children('/parent-a/parent-a')).toEqual([
'parent-a-file.txt',
]);
});

describe('at the root', () => {
it('should return a list of children', () => {
expect(tree.children('')).toEqual(['parent', 'root-file.txt']);
Expand Down
8 changes: 6 additions & 2 deletions packages/nx/src/generators/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,12 @@ export class FsTree implements Tree {
}
Object.keys(this.recordedChanges).forEach((f) => {
if (f.startsWith(`${path}/`)) {
const [_, file] = f.split(`${path}/`);
res[file.split('/')[0]] = true;
// Remove the current folder's path from the directory
const file = f.substring(path.length + 1);
// Split the path on segments, and take the first one
const basePath = file.split('/')[0];
// Mark it as a child of the current directory
res[basePath] = true;
}
});

Expand Down

0 comments on commit bafd1e3

Please sign in to comment.