From 25a34b45a450e4c26da51cd1b2e593e2e19be3ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 23 Mar 2023 10:25:38 -0300 Subject: [PATCH 1/2] fix(file): make getFilesByPattern recursive by default --- package.json | 2 +- src/Helpers/FakeApi.ts | 2 +- src/Helpers/Folder.ts | 16 ++--- src/Helpers/Module.ts | 5 +- tests/Unit/FolderTest.ts | 126 ++++++++++++++++++--------------------- 5 files changed, 67 insertions(+), 84 deletions(-) diff --git a/package.json b/package.json index f8f2b45..76b4bcc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "3.4.4", + "version": "3.4.5", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", diff --git a/src/Helpers/FakeApi.ts b/src/Helpers/FakeApi.ts index ec57902..f032a43 100644 --- a/src/Helpers/FakeApi.ts +++ b/src/Helpers/FakeApi.ts @@ -88,7 +88,7 @@ export class FakeApi { * Register all file routes found in folder path. */ public static async registerFolder(path: string): Promise { - const files = new Folder(path).getFilesByPattern('*/**/*.json', true) + const files = new Folder(path).getFilesByPattern('**/*.json') const promises = files.map(file => file.load().then(fileLoaded => this.registerFile(fileLoaded)), diff --git a/src/Helpers/Folder.ts b/src/Helpers/Folder.ts index f2de595..db8efd2 100644 --- a/src/Helpers/Folder.ts +++ b/src/Helpers/Folder.ts @@ -659,7 +659,7 @@ export class Folder { /** * Get all the files of folder using glob pattern. */ - public getFilesByPattern(pattern?: string, recursive = false): File[] { + public getFilesByPattern(pattern?: string): File[] { this.loadSync({ withSub: true, isInternalLoad: true }) if (pattern) { @@ -669,18 +669,14 @@ export class Folder { const files = [] this.files.forEach(file => { - if (pattern && minimatch(file.path, pattern)) { - files.push(file) - + if (pattern && !minimatch(file.path, pattern)) { return } files.push(file) }) - if (recursive) { - files.push(...Folder.getSubFiles(this.folders, pattern)) - } + files.push(...Folder.getSubFiles(this.folders, pattern)) return files } @@ -688,7 +684,7 @@ export class Folder { /** * Get all the folders of folder using glob pattern. */ - public getFoldersByPattern(pattern?: string, recursive = false): Folder[] { + public getFoldersByPattern(pattern?: string): Folder[] { this.loadSync({ withSub: true, isInternalLoad: true }) if (pattern) { @@ -698,8 +694,8 @@ export class Folder { const folders = [] this.folders.forEach(folder => { - if (recursive && folder.folders.length) { - folders.push(...Folder.getSubFolders(folder, recursive, pattern)) + if (folder.folders.length) { + folders.push(...Folder.getSubFolders(folder, true, pattern)) } if (pattern && minimatch(folder.path, pattern)) { diff --git a/src/Helpers/Module.ts b/src/Helpers/Module.ts index 065a4d0..371d06f 100644 --- a/src/Helpers/Module.ts +++ b/src/Helpers/Module.ts @@ -145,10 +145,7 @@ export class Module { const folder = await new Folder(path).load() - // FIXME Why glob pattern *.js is retrieving .d.ts and .js.map files? - return folder - .getFilesByPattern(`*/**/*.${Path.ext()}`, true) - .filter(file => file.extension.endsWith(`.${Path.ext()}`)) + return folder.getFilesByPattern(`**/*.${Path.ext()}`) } /** diff --git a/tests/Unit/FolderTest.ts b/tests/Unit/FolderTest.ts index a78d89c..94256ef 100644 --- a/tests/Unit/FolderTest.ts +++ b/tests/Unit/FolderTest.ts @@ -232,90 +232,80 @@ test.group('FolderTest', group => { test('should get all files/folders that match the pattern', async ({ assert }) => { const path = bigFolderPath.concat(sep, 'folder') - await File.createFileOfSize(path.concat('/file.txt'), 1024 * 1024) - - new Folder(path.concat('/A')).loadSync() - new Folder(path.concat('/A', '/B')).loadSync() - - await File.createFileOfSize(path.concat('/A', '/B', '/file.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileOne.ts'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileTwo.ts'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileOne.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileTwo.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileOne.json'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileTwo.json'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileOne.edge'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileTwo.edge'), 1024 * 1024) + + await new Folder(path.concat('/A')).load() + await new Folder(path.concat('/A', '/B')).load() + + await File.createFileOfSize(path.concat('/A', '/B', '/fileOne.ts'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileTwo.ts'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileOne.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileTwo.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileOne.json'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileTwo.json'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileOne.edge'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileTwo.edge'), 1024 * 1024) const folder = new Folder(path) - const files = folder.getFilesByPattern('**/*.txt') + const ts = folder.getFilesByPattern('**/*.ts') + const txt = folder.getFilesByPattern('**/*.txt') + const json = folder.getFilesByPattern('**/*.json') + const edge = folder.getFilesByPattern('**/*.edge') const folders = folder.getFoldersByPattern('**/*') - assert.lengthOf(files, 1) - assert.lengthOf(folders, 1) - }) - - test('should be able to get all files/folders without any pattern', async ({ assert }) => { - const path = bigFolderPath.concat(sep, 'folder') - - await File.createFileOfSize(path.concat('/file.txt'), 1024 * 1024) - - new Folder(path.concat('/A')).loadSync() - new Folder(path.concat('/A', '/B')).loadSync() - - await File.createFileOfSize(path.concat('/A', '/B', '/file.txt'), 1024 * 1024) - - const folder = new Folder(path) - - const files = folder.getFilesByPattern() - const folders = folder.getFoldersByPattern() - - assert.lengthOf(files, 1) - assert.lengthOf(folders, 1) - }) + assert.lengthOf(folders, 2) - test('should get all files/folders recursively that match the pattern', async ({ assert }) => { - const path = bigFolderPath.concat(sep, 'folder') + assert.lengthOf(ts, 4) + ts.forEach(file => assert.equal(file.extension, '.ts')) - await new Folder(path.concat('/A')).load({ withSub: false }) - new Folder(path.concat('/B')).loadSync({ withSub: false }) - new Folder(path.concat('/C')).loadSync() - new Folder(path.concat('/C', '/D')).loadSync() - new Folder(path.concat('/C', '/D', '/E')).loadSync() + assert.lengthOf(txt, 4) + txt.forEach(file => assert.equal(file.extension, '.txt')) - const size = 1024 * 1024 - - await File.createFileOfSize(path.concat('/A', '/file.txt'), size) - await File.createFileOfSize(path.concat('/B', '/file.txt'), size) - await File.createFileOfSize(path.concat('/C', '/file.txt'), size) - await File.createFileOfSize(path.concat('/C', '/D', '/file.txt'), size) - await File.createFileOfSize(path.concat('/C', '/D', '/E', '/file.txt'), size) - - const folder = new Folder(path) + assert.lengthOf(json, 4) + json.forEach(file => assert.equal(file.extension, '.json')) - assert.lengthOf(folder.getFilesByPattern('**/*.txt', true), 5) - assert.lengthOf(folder.getFoldersByPattern('**/*', true), 5) - - assert.lengthOf(folder.getFilesByPattern('*/*.txt', true), 3) - assert.lengthOf(folder.getFoldersByPattern('*', true), 3) + assert.lengthOf(edge, 4) + edge.forEach(file => assert.equal(file.extension, '.edge')) }) - test('should be able to get all files/folders recursively without any pattern', async ({ assert }) => { + test('should be able to get all files/folders without any pattern', async ({ assert }) => { const path = bigFolderPath.concat(sep, 'folder') - await new Folder(path.concat('/A')).load({ withSub: false }) - new Folder(path.concat('/B')).loadSync({ withSub: false }) - new Folder(path.concat('/C')).loadSync() - new Folder(path.concat('/C', '/D')).loadSync() - new Folder(path.concat('/C', '/D', '/E')).loadSync() - - const size = 1024 * 1024 - - await File.createFileOfSize(path.concat('/A', '/file.txt'), size) - await File.createFileOfSize(path.concat('/B', '/file.txt'), size) - await File.createFileOfSize(path.concat('/C', '/file.txt'), size) - await File.createFileOfSize(path.concat('/C', '/D', '/file.txt'), size) - await File.createFileOfSize(path.concat('/C', '/D', '/E', '/file.txt'), size) + await File.createFileOfSize(path.concat('/fileOne.ts'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileTwo.ts'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileOne.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileTwo.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileOne.json'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileTwo.json'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileOne.edge'), 1024 * 1024) + await File.createFileOfSize(path.concat('/fileTwo.edge'), 1024 * 1024) + + await new Folder(path.concat('/A')).load() + await new Folder(path.concat('/A', '/B')).load() + + await File.createFileOfSize(path.concat('/A', '/B', '/fileOne.ts'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileTwo.ts'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileOne.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileTwo.txt'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileOne.json'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileTwo.json'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileOne.edge'), 1024 * 1024) + await File.createFileOfSize(path.concat('/A', '/B', '/fileTwo.edge'), 1024 * 1024) const folder = new Folder(path) - const files = folder.getFilesByPattern(null, true) - const folders = folder.getFoldersByPattern(null, true) + const files = folder.getFilesByPattern() + const folders = folder.getFoldersByPattern() - assert.lengthOf(files, 5) - assert.lengthOf(folders, 5) + assert.lengthOf(files, 16) + assert.lengthOf(folders, 2) }) }) From 8ce4299b4ba25a33222e0d00e3066437fe838e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 23 Mar 2023 10:26:12 -0300 Subject: [PATCH 2/2] chore(npm): upgrade package version --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index e9c51ca..beb926d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "3.4.4", + "version": "3.4.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "3.4.4", + "version": "3.4.5", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0",