From bafd1e3f786bb2c30000cb8225908bfb4bfa8d28 Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Mon, 3 Apr 2023 16:01:34 -0400 Subject: [PATCH] fix(devkit): tree.children should support writes to directories that have the same name as the parent --- packages/nx/src/generators/tree.spec.ts | 10 ++++++++++ packages/nx/src/generators/tree.ts | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/generators/tree.spec.ts b/packages/nx/src/generators/tree.spec.ts index 18207753c6ba9a..f3e6eb17e5dc85 100644 --- a/packages/nx/src/generators/tree.spec.ts +++ b/packages/nx/src/generators/tree.spec.ts @@ -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']); diff --git a/packages/nx/src/generators/tree.ts b/packages/nx/src/generators/tree.ts index 6e79fcce28f5ec..36df191c7441b7 100644 --- a/packages/nx/src/generators/tree.ts +++ b/packages/nx/src/generators/tree.ts @@ -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; } });