Skip to content

Commit

Permalink
Merge pull request #39 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix(file): make getFilesByPattern recursive by default
  • Loading branch information
jlenon7 authored Mar 23, 2023
2 parents 79e215a + 8ce4299 commit 400fdb1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 86 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/FakeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class FakeApi {
* Register all file routes found in folder path.
*/
public static async registerFolder(path: string): Promise<void> {
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)),
Expand Down
16 changes: 6 additions & 10 deletions src/Helpers/Folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -669,26 +669,22 @@ 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
}

/**
* 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) {
Expand All @@ -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)) {
Expand Down
5 changes: 1 addition & 4 deletions src/Helpers/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`)
}

/**
Expand Down
126 changes: 58 additions & 68 deletions tests/Unit/FolderTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 400fdb1

Please sign in to comment.