Skip to content

Commit

Permalink
feat: change fs.fs.readDirContentRecursive to fs.readDir and allow no…
Browse files Browse the repository at this point in the history
…n recursive search
  • Loading branch information
sahachide committed Oct 19, 2020
1 parent 9d4e170 commit 77ac53d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/controller/ControllerLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ControllerLoader extends AbstractZenFileLoader {
public async load(): Promise<Controllers> {
const controllers = new Map() as Controllers
const filePaths = (
await fs.readDirContentRecursive(fs.resolveZenPath('controller'))
await fs.readDir(fs.resolveZenPath('controller'))
).filter((filePath: string) =>
filePath.toLowerCase().endsWith(fs.resolveZenFileExtension('controller')),
)
Expand Down
2 changes: 1 addition & 1 deletion src/database/EntityLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { log } from '../log/logger'
export class EntityLoader extends AbstractZenFileLoader {
public async load(): Promise<Entities> {
const entities = new Map() as Entities
const filePaths = await fs.readDirContentRecursive(fs.resolveZenPath('entity'))
const filePaths = await fs.readDir(fs.resolveZenPath('entity'), false)

for (const filePath of filePaths) {
const { key, module } = await this.loadModule(filePath)
Expand Down
21 changes: 17 additions & 4 deletions src/filesystem/FS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join, resolve } from 'path'

import { path as appDir } from 'app-root-path'
import { config } from '../config/config'
import { join } from 'path'
import { log } from '../log/logger'
import { promises } from 'fs'
import { readDirRecursive } from './readDirRecursiveGenerator'
Expand Down Expand Up @@ -28,11 +29,23 @@ export abstract class fs {
return success
}

public static async readDirContentRecursive(dir: string = appDir): Promise<string[]> {
public static async readDir(dir: string, recursive: boolean = true): Promise<string[]> {
const files = []

for await (const file of readDirRecursive(dir)) {
files.push(file)
if (recursive) {
for await (const file of readDirRecursive(dir)) {
files.push(file)
}
} else {
const dirContent = await promises.readdir(dir, {
withFileTypes: true,
})

for (const content of dirContent) {
if (content.isFile()) {
files.push(resolve(dir, content.name))
}
}
}

return files
Expand Down
4 changes: 1 addition & 3 deletions src/service/ServiceLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export class ServiceLoader extends AbstractZenFileLoader {
return services
}

const filePaths = (
await fs.readDirContentRecursive(fs.resolveZenPath('service'))
).filter((filePath: string) =>
const filePaths = (await fs.readDir(fs.resolveZenPath('service'))).filter((filePath: string) =>
filePath.toLowerCase().endsWith(fs.resolveZenFileExtension('service')),
)

Expand Down
8 changes: 4 additions & 4 deletions src/template/TemplateEngineLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export class TemplateEngineLoader extends AbstractZenFileLoader {
}
}
protected async loadFiles(): Promise<string[]> {
return (
await fs.readDirContentRecursive(fs.resolveZenPath('view'))
).filter((filePath: string) => filePath.endsWith(`.${config.template.extension}`))
return (await fs.readDir(fs.resolveZenPath('view'))).filter((filePath: string) =>
filePath.endsWith(`.${config.template.extension}`),
)
}
protected async loadFilters(): Promise<TemplateFiltersMap> {
const filters = new Map() as TemplateFiltersMap
Expand All @@ -32,7 +32,7 @@ export class TemplateEngineLoader extends AbstractZenFileLoader {
return filters
}

const filePaths = (await fs.readDirContentRecursive(dir)).filter((filePath: string) =>
const filePaths = (await fs.readDir(dir)).filter((filePath: string) =>
filePath.endsWith(fs.resolveZenFileExtension()),
)

Expand Down
4 changes: 2 additions & 2 deletions test/filesystem/fs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fs } from '../../src/filesystem/FS'
import { getFixtureDir } from '../helper/getFixtureDir'
import { join } from 'path'
import { fs } from '../../src/filesystem/FS'

describe('FS', () => {
it("checks that a file exists or doesn't extist", async () => {
Expand All @@ -13,7 +13,7 @@ describe('FS', () => {

it('read the directory content recursively', async () => {
const testDir = getFixtureDir('testapp')
const content = await fs.readDirContentRecursive(testDir)
const content = await fs.readDir(testDir)
const normalizedContent = content.map((filePath) => filePath.replace(testDir, ''))

expect(normalizedContent).toContain('/src/controller/ResponseController.ts')
Expand Down

0 comments on commit 77ac53d

Please sign in to comment.