ef00732685
- Made the use of `const` and `let` consistent - import required functions and types from 'react' instead of using the `React.` namespace. - Added `Expand` type, which can expand complex types to their "final" result. - Ensured that we use `as const` for DEFAULT_XXX_TAG where we used a string. So that we have the type of `div` instead of `string` for example. - Used `interface` over `type` where possible. I'm personally more of a `type` fan. But the TypeScript recommends `interfaces` where possible because they are faster, yield better error messages and so on.
47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
import fs from 'fs'
|
|
import path from 'path'
|
|
export type ExamplesType = {
|
|
name: string
|
|
path: string
|
|
children?: ExamplesType[]
|
|
}
|
|
|
|
export async function resolveAllExamples(...paths: string[]) {
|
|
let base = path.resolve(process.cwd(), ...paths)
|
|
|
|
if (!fs.existsSync(base)) {
|
|
return false
|
|
}
|
|
|
|
let files = await fs.promises.readdir(base, { withFileTypes: true })
|
|
let items: ExamplesType[] = []
|
|
|
|
for (let file of files) {
|
|
// Skip reserved filenames from Next. E.g.: _app.tsx, _error.tsx
|
|
if (file.name.startsWith('_')) {
|
|
continue
|
|
}
|
|
|
|
let bucket: ExamplesType = {
|
|
name: file.name.replace(/-/g, ' ').replace(/\.tsx?/g, ''),
|
|
path: [...paths, file.name]
|
|
.join('/')
|
|
.replace(/^pages/, '')
|
|
.replace(/\.tsx?/g, '')
|
|
.replace(/\/+/g, '/'),
|
|
}
|
|
|
|
if (file.isDirectory()) {
|
|
let children = await resolveAllExamples(...paths, file.name)
|
|
|
|
if (children) {
|
|
bucket.children = children
|
|
}
|
|
}
|
|
|
|
items.push(bucket)
|
|
}
|
|
|
|
return items
|
|
}
|