diff --git a/test/test.ts b/test/test.ts index ec47b18..6cbb350 100644 --- a/test/test.ts +++ b/test/test.ts @@ -52,6 +52,7 @@ tap.test('prepare for directory style tests', async () => { await myBucket.fastStore('dir1/file2.txt', 'dir1/file2.txt content'); await myBucket.fastStore('dir2/file1.txt', 'dir2/file1.txt content'); await myBucket.fastStore('dir3/file1.txt', 'dir3/file1.txt content'); + await myBucket.fastStore('dir3/dir4/file1.txt', 'dir3/dir4/file1.txt content'); await myBucket.fastStore('file1.txt', 'file1 content'); }); @@ -68,10 +69,10 @@ tap.test('should get base directory', async () => { }); tap.test('should correctly build paths for sub directories', async () => { - const dir1 = await baseDirectory.getSubDirectoryByName('dir1'); - expect(dir1).to.be.instanceOf(smartbucket.Directory); - const dir1BasePath = dir1.getBasePath(); - console.log(dir1BasePath); + const dir4 = await baseDirectory.getSubDirectoryByName('dir3/dir4'); + expect(dir4).to.be.instanceOf(smartbucket.Directory); + const dir4BasePath = dir4.getBasePath(); + console.log(dir4BasePath); }); tap.test('clean up directory style tests', async () => { @@ -79,6 +80,7 @@ tap.test('clean up directory style tests', async () => { await myBucket.fastRemove('dir1/file2.txt'); await myBucket.fastRemove('dir2/file1.txt'); await myBucket.fastRemove('dir3/file1.txt'); + await myBucket.fastRemove('dir3/dir4/file1.txt'); await myBucket.fastRemove('file1.txt'); }); diff --git a/ts/smartbucket.classes.directory.ts b/ts/smartbucket.classes.directory.ts index d40c04b..dd48106 100644 --- a/ts/smartbucket.classes.directory.ts +++ b/ts/smartbucket.classes.directory.ts @@ -43,6 +43,10 @@ export class Directory { const parentDirectories = this.getParentDirectories(); let basePath = ''; for (const parentDir of parentDirectories) { + if (parentDir.name === '') { + basePath = this.name; + continue; + } basePath = parentDir.name + '/' + this.name; } return basePath; @@ -91,9 +95,12 @@ export class Directory { const directoryArray: Directory[] = []; const duplexStream = plugins.streamfunction.createDuplexStream( async bucketItem => { - const subtractedPath = bucketItem.name.replace(this.getBasePath(), ''); + let subtractedPath = bucketItem.name.replace(this.getBasePath(), ''); + if (subtractedPath.startsWith('/')) { + subtractedPath = subtractedPath.substr(1); + } if (subtractedPath.includes('/')) { - const dirName = bucketItem.name.split('/')[0]; + const dirName = subtractedPath.split('/')[0]; if (directoryArray.find(directory => directory.name === dirName)) { return; } @@ -124,11 +131,20 @@ export class Directory { * gets a sub directory */ public async getSubDirectoryByName(dirNameArg: string): Promise { - // TODO: make this recursive - const directories = await this.listDirectories(); - return directories.find(directory => { - return directory.name === dirNameArg; - }); + const dirNameArray = dirNameArg.split('/'); + + const getDirectory = async (directoryArg: Directory, dirNameToSearch: string) => { + const directories = await directoryArg.listDirectories(); + return directories.find(directory => { + return directory.name === dirNameToSearch; + }); + }; + let wantedDirectory: Directory; + for (const dirNameToSearch of dirNameArray) { + const directoryToSearchIn = wantedDirectory ? wantedDirectory : this; + wantedDirectory = await getDirectory(directoryToSearchIn, dirNameToSearch); + } + return wantedDirectory; } /**