mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
27 lines
1.1 KiB
TypeScript
27 lines
1.1 KiB
TypeScript
import { glob } from 'fs/promises'
|
|
import { join, normalize, posix, sep } from 'path'
|
|
import _routerMock from 'next-router-mock'
|
|
import { createDynamicRouteParser } from 'next-router-mock/dynamic-routes'
|
|
|
|
export const routerMock = _routerMock
|
|
|
|
const pipe =
|
|
<T>(...fns: Array<(arg: T) => T>) =>
|
|
(value: T) =>
|
|
fns.reduce((acc, fn) => fn(acc), value)
|
|
// normalize file paths to posix format (windows FS support)
|
|
const toPosix = (str: string) => str.replaceAll(sep, posix.sep)
|
|
const pagesRoot = toPosix(join(process.cwd(), `./pages`))
|
|
// remove the base filepath
|
|
const removePrefix = (str: string) => str.replace(pagesRoot, ``)
|
|
// remove the file extension or index segment
|
|
const removeExt = (str: string) => str.replace(/.tsx|\/index.tsx/, ``)
|
|
|
|
// Glob the `pages/` directory for all dynamic route segments
|
|
const paths = [
|
|
...(await Array.fromAsync(glob(`${pagesRoot}/**/*\].tsx`))),
|
|
...(await Array.fromAsync(glob(`${pagesRoot}/**/**\]/**/index.tsx`))),
|
|
].map(pipe(normalize, toPosix, removePrefix, removeExt))
|
|
|
|
routerMock.useParser(createDynamicRouteParser(paths))
|