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 #15

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

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

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

const data = await getFreshDataBySlug(noteDir)

return data

}

https://github.com/JournalOfTrialAndError/centeroftrialanderror.com/blob/1c2a62be7e2ccb916db6dfb0a5f0c2701cee1fef/libs/scripts/src/lib/clone/mdxDataBySlug.mjs#L33

// @ts-check
import { readFile, writeFile } from 'fs/promises'
import { join } from 'path'
import readdirp from 'readdirp'
import { slugify } from './slug.mjs'

export const getFreshDataBySlug = async (noteDir) => {
  const rawDir = await readdirp.promise(noteDir, { alwaysStat: true })
  // Only include md(x) files
  return rawDir
    .filter((entry) => /\.mdx?$/.test(entry.path))
    .reduce((acc, curr) => {
      const name = curr.basename.replace(/\.mdx?$/, '')
      const slug = slugify(name)
      const { atime, mtime, ctime, birthtime, ...stats } = { ...curr.stats }
      acc[slug] = {
        stats,
        fullPath: curr.fullPath,
        path: curr.path,
        name,
        slug,
        folders:
          curr.path
            .replace(curr.basename, '')
            .split('/')
            .filter((entry) => entry) ?? [],
        basename: curr.basename,
      }
      return acc
    }, {})
}

// TODO: Make the dataBy... files inherit from the same function
export const mdxDataBySlug = async (dataDir, noteDir) => {
  // if (process.env.ALWAYS_FRESH !== 'true' && process.env.NODE_ENV !== 'production') {
  //   const data = await getFreshDataBySlug(noteDir)
  //   return data
  // }
  const datapath = join(dataDir, 'dataBySlug.json')
  try {
    const data = JSON.parse(await readFile(datapath, 'utf8'))
    return data
  } catch (e) {
    console.log('No data found, writing new')
    const data = await getFreshDataBySlug(noteDir)
    await writeFile(datapath, JSON.stringify(data))
    return data
  }
}

6aed892d2c54468c663835105aef4d8ece8dab97

@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