diff --git a/.changeset/purple-days-film.md b/.changeset/purple-days-film.md new file mode 100644 index 0000000000..09d760c33e --- /dev/null +++ b/.changeset/purple-days-film.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs-node': patch +--- + +Fixed bug that prevented deletion of stale files when non-empty directories where tried to be deleted as well. diff --git a/plugins/techdocs-node/src/stages/publish/helpers.test.ts b/plugins/techdocs-node/src/stages/publish/helpers.test.ts index fa934d6dd9..7b303b3bf5 100644 --- a/plugins/techdocs-node/src/stages/publish/helpers.test.ts +++ b/plugins/techdocs-node/src/stages/publish/helpers.test.ts @@ -158,6 +158,16 @@ describe('getStaleFiles', () => { expect(staleFiles).toHaveLength(1); expect(staleFiles).toEqual(expect.arrayContaining(['stale_file.png'])); }); + + it('should not return directories as stale files if they are parent directories of new files', () => { + const oldFiles = [...defaultFiles, 'default/Component/backstage/foo']; + const newFiles = [ + ...defaultFiles, + 'default/Component/backstage/foo/bar/index.html', + ]; + const staleFiles = getStaleFiles(newFiles, oldFiles); + expect(staleFiles).toHaveLength(0); + }); }); describe('getCloudPathForLocalPath', () => { diff --git a/plugins/techdocs-node/src/stages/publish/helpers.ts b/plugins/techdocs-node/src/stages/publish/helpers.ts index decefae187..0b9384dba3 100644 --- a/plugins/techdocs-node/src/stages/publish/helpers.ts +++ b/plugins/techdocs-node/src/stages/publish/helpers.ts @@ -169,8 +169,21 @@ export const getStaleFiles = ( oldFiles: string[], ): string[] => { const staleFiles = new Set(oldFiles); + const removedParentDirs = new Set(); newFiles.forEach(newFile => { staleFiles.delete(newFile); + + // We have to traverse through the directory hierarchy of a new file and + // ensure that we won't try to delete one of the parent directories. + let parentDir = newFile.substring(0, newFile.lastIndexOf('/')); + while ( + !removedParentDirs.has(parentDir) && + parentDir.length >= newFile.indexOf('/') + ) { + staleFiles.delete(parentDir); + removedParentDirs.add(parentDir); + parentDir = parentDir.substring(0, parentDir.lastIndexOf('/')); + } }); return Array.from(staleFiles); };