File size: 1,547 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
#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const globOrig = require('glob')
const { promisify } = require('util')
const glob = promisify(globOrig)

function collectPaths(routes, paths = []) {
  for (const route of routes) {
    if (route.path && !route.redirect) {
      paths.push(route.path)
    }

    if (route.routes) {
      collectPaths(route.routes, paths)
    }
  }
}

async function main() {
  const manifest = 'errors/manifest.json'
  let hadError = false

  const dir = path.dirname(manifest)
  const files = await glob(path.join(dir, '**/*.md'))

  const manifestData = JSON.parse(await fs.promises.readFile(manifest, 'utf8'))

  const paths = []
  collectPaths(manifestData.routes, paths)

  const missingFiles = files.filter(
    (file) => !paths.includes(`/${file}`) && file !== 'errors/template.md'
  )

  if (missingFiles.length) {
    hadError = true
    console.log(`Missing paths in ${manifest}:\n${missingFiles.join('\n')}`)
  } else {
    console.log(`No missing paths in ${manifest}`)
  }

  for (const filePath of paths) {
    if (
      !(await fs.promises
        .access(path.join(process.cwd(), filePath), fs.constants.F_OK)
        .then(() => true)
        .catch(() => false))
    ) {
      console.log('Could not find path:', filePath)
      hadError = true
    }
  }

  if (hadError) {
    throw new Error('missing/incorrect manifest items detected see above')
  }
}

main()
  .then(() => console.log('success'))
  .catch((err) => {
    console.error(err)
    process.exit(1)
  })