File size: 2,618 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import preval from 'preval.macro'
import title from 'title'

const excludes = ['/_app.js', '/_document.js', '/_error.js']

// watch all meta files
const meta = {}
function importAll(r) {
  return r.keys().forEach(key => {
    meta[key.slice(1)] = r(key)
  })
}
importAll(require.context('../pages/', true, /meta\.json$/))

// use macro to load the file list
const items = preval`
  const { readdirSync, readFileSync } = require('fs')
  const { resolve, join } = require('path')
  const extension = /\.(mdx?|jsx?)$/
  
  function getFiles(dir, route) {
    const files = readdirSync(dir, { withFileTypes: true })

    // go through the directory
    const items = files
      .map(f => {
        const filePath = resolve(dir, f.name)
        const fileRoute = join(route, f.name.replace(extension, '').replace(/^index$/, ''))

        if (f.isDirectory()) {
          const children = getFiles(filePath, fileRoute)
          if (!children.length) return null
          return { name: f.name, children, route: fileRoute }
        } else if (f.name === 'meta.json') {
          return null
        } else if (extension.test(f.name)) {
          return { name: f.name.replace(extension, ''), route: fileRoute }
        }
      })
      .map(item => {
        if (!item) return
        return { ...item, metaPath: join(route, 'meta.json') }
      })
      .filter(Boolean)

    return items
  }
  module.exports = getFiles(join(process.cwd(), 'pages'), '/')
`

const attachPageConfig = function (items) {
  let folderMeta = null
  let fnames = null

  return items
    .filter(item => !excludes.includes(item.name))
    .map(item => {
      const { metaPath, ...rest } = item
      folderMeta = meta[metaPath]
      if (folderMeta) {
        fnames = Object.keys(folderMeta)
      }

      const pageConfig = folderMeta?.[item.name]

      if (rest.children) rest.children = attachPageConfig(rest.children)

      if (pageConfig) {
        if (typeof pageConfig === 'string') {
          return { ...rest, title: pageConfig }
        }
        return { ...rest, ...pageConfig }
      } else {
        if (folderMeta) {
          return null
        }
        return { ...rest, title: title(item.name) }
      }
    })
    .filter(Boolean)
    .sort((a, b) => {
      if (folderMeta) {
        return fnames.indexOf(a.name) - fnames.indexOf(b.name)
      }
      // by default, we put directories first
      if (!!a.children !== !!b.children) {
        return !!a.children ? -1 : 1
      }
      // sort by file name
      return a.name < b.name ? -1 : 1
    })
}

export default () => {
  return attachPageConfig(items)
}