Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the dataBy... files inherit from the same function #16

Open
github-actions bot opened this issue Jul 25, 2022 · 0 comments
Open

Make the dataBy... files inherit from the same function #16

github-actions bot opened this issue Jul 25, 2022 · 0 comments
Labels

Comments

@github-actions
Copy link

Make the dataBy... files inherit from the same function

https://github.com/JournalOfTrialAndError/centeroftrialanderror.com/blob/1c2a62be7e2ccb916db6dfb0a5f0c2701cee1fef/libs/utils/mdx/src/lib/mdxDataByName.ts#L8

import { Stats } from 'fs'
import { readFile, writeFile } from 'fs/promises'
import { join } from 'path'
import readdirp from 'readdirp'
import { DATA_DIR, NEXT_PUBLIC_NOTE_DIR } from '@zkp/paths'
import { slugify } from '@zkp/slugify'

// TODO: Make the dataBy... files inherit from the same function
export const mdxDataByName = async () => {
  const datapath = join(DATA_DIR, 'dataByName.json')
  try {
    const data = JSON.parse(await readFile(datapath, 'utf8'))
    return data
  } catch (e) {
    const data = (await readdirp.promise(NEXT_PUBLIC_NOTE_DIR, { alwaysStat: true }))
      // Only include md(x) files
      .filter((entry) => /\.mdx?$/.test(entry.path))
      .reduce(
        (acc, curr) => {
          const name = curr.basename.replace(/\.mdx?$/, '').toLowerCase()
          acc[name] = {
            stats: curr.stats!,
            fullPath: curr.fullPath,
            path: curr.path,
            name,
            slug: slugify(name),
            folders: curr.basename.split('/') ?? [],
            basename: curr.basename,
          }
          return acc
        },
        {} as {
          [name: string]: {
            basename: string
            stats: Stats
            fullPath: string
            name: string
            folders: string[]
            path: string
            slug: string
          }
        },
      )

    await writeFile(datapath, JSON.stringify(data))
    return data
  }
}

208e02d67253ff2d0f900673df873f9ad1fde34d

@github-actions github-actions bot added the todo label Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants