rsnext/packages/next/lib/flat-readdir.ts
Damien Simonin Feugas 977ff52472
fix(#37106): middleware can not be loaded from src folder (#37428)
## Bug
fixes #37106

Please note that, as for `pages/` the `src/middleware` file is ignored when `/middleware` is present.

## How to test

1. Rebuild next.js `pnpm build`
2. Run dedicated tests: `pnpm testheadless --testPathPattern middleware-src/`
2022-06-08 14:10:05 +00:00

26 lines
691 B
TypeScript

import { join } from 'path'
import { nonNullable } from './non-nullable'
import { promises } from 'fs'
export async function flatReaddir(dir: string, include: RegExp) {
const dirents = await promises.readdir(dir, { withFileTypes: true })
const result = await Promise.all(
dirents.map(async (part) => {
const absolutePath = join(dir, part.name)
if (part.isSymbolicLink()) {
const stats = await promises.stat(absolutePath)
if (stats.isDirectory()) {
return null
}
}
if (part.isDirectory() || !include.test(part.name)) {
return null
}
return absolutePath
})
)
return result.filter(nonNullable)
}