Skip to content

Instantly share code, notes, and snippets.

@Roms1383
Created November 8, 2020 03:47
Show Gist options
  • Save Roms1383/f1934b6906751c2f555c5a8a4b4cd89d to your computer and use it in GitHub Desktop.
Save Roms1383/f1934b6906751c2f555c5a8a4b4cd89d to your computer and use it in GitHub Desktop.
Write a TypeScript barrel file from files found in directory
const path = require('path')
const fs = require('fs')
const source = process.env.PWD
const filenames = fs.readdirSync(source)
.filter(filename => filename.indexOf('.ts') !== -1 && filename !== 'index.ts')
.map(filename => filename.replace('.ts', ''))
const content = filenames
.map(filename => `export * from './${filename}'`)
.join('\n')
fs.writeFileSync(path.join(source, 'index.ts'), content, { encoding: 'utf8' })
console.log(`wrote to ${source}/index.ts`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment