Skip to content

Instantly share code, notes, and snippets.

@danielkv
Last active November 30, 2022 17:24
Show Gist options
  • Select an option

  • Save danielkv/c040f948ecd03e128b919ee597920ace to your computer and use it in GitHub Desktop.

Select an option

Save danielkv/c040f948ecd03e128b919ee597920ace to your computer and use it in GitHub Desktop.

Revisions

  1. danielkv revised this gist Nov 30, 2022. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions export.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,3 @@
    /* eslint-disable @typescript-eslint/no-unsafe-argument */
    /* eslint-disable @typescript-eslint/no-unsafe-assignment */
    /* eslint-disable @typescript-eslint/no-unsafe-return */
    /* eslint-disable @typescript-eslint/restrict-template-expressions */
    /* eslint-disable @typescript-eslint/no-unsafe-call */
    /* eslint-disable @typescript-eslint/no-unsafe-member-access */
    const path = require('path')
    const fs = require('fs')
    const { format } = require('prettier')
  2. danielkv created this gist Nov 30, 2022.
    59 changes: 59 additions & 0 deletions export.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    /* eslint-disable @typescript-eslint/no-unsafe-argument */
    /* eslint-disable @typescript-eslint/no-unsafe-assignment */
    /* eslint-disable @typescript-eslint/no-unsafe-return */
    /* eslint-disable @typescript-eslint/restrict-template-expressions */
    /* eslint-disable @typescript-eslint/no-unsafe-call */
    /* eslint-disable @typescript-eslint/no-unsafe-member-access */
    const path = require('path')
    const fs = require('fs')
    const { format } = require('prettier')

    const folder = path.resolve(__dirname)
    const outputFile = path.resolve(__dirname, 'index.ts')

    function generateExport(name, findings) {
    let exportString = 'export {'

    findings.forEach((finding, index) => {
    if (index > 0) exportString += ','
    exportString += `\n ${finding.component}`
    })

    exportString += `\n} from './${name}'`

    return exportString
    }

    fs.readdir(folder, (err, files) => {
    if (err) return
    const exports = []

    files.forEach((file) => {
    const dirPath = `${folder}/${file}`
    if (!fs.statSync(dirPath).isDirectory()) return

    const filePath = `${dirPath}/index.tsx`
    if (!fs.existsSync(dirPath) || !fs.statSync(filePath).isFile()) return
    const content = fs.readFileSync(filePath, 'utf-8')

    const exportRegExp =
    /export[\s]+(type|const|default|function)[\s]+([a-zA-Z]+):?[\s]+/gm

    const match = [...content.matchAll(exportRegExp)]
    const findings = match.reduce((acc, match) => {
    const newFinding = {
    model: match[1],
    component: match[2],
    }

    acc.push(newFinding)

    return acc
    }, [])

    exports.push(generateExport(file, findings))
    })

    const formatedContent = format(exports.join('\n'))
    fs.writeFileSync(outputFile, formatedContent)
    })