Skip to content

Instantly share code, notes, and snippets.

@zthxxx
Created September 28, 2021 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zthxxx/95c55b76995e42a92ed1682d71fe1318 to your computer and use it in GitHub Desktop.
Save zthxxx/95c55b76995e42a92ed1682d71fe1318 to your computer and use it in GitHub Desktop.
import path from 'path'
import { promises as fs } from 'fs'
import { camelCase } from 'lodash-es'
const [ , self, dir, mergeFlag] = process.argv
if (!dir) {
console.log(`
usage:
node ${path.relative(process.cwd(), self)} <target-dir> [--merge]
`)
process.exit(1)
}
const isMerge = mergeFlag === '--merge'
const list = await fs.readdir(dir)
const fileNames = list
.filter(item => (
item !== 'index.ts'
&& /\.ts$/.test(item)
))
.map(file => file.replace(/\.ts$/, ''))
console.log(`
// generated by
// node ${path.relative(process.cwd(), self)} ${dir} ${isMerge ? '--merge' : ''}
${
fileNames
.map(fileName => `import { default as ${camelCase(fileName)} } from './${fileName}'`)
.join('\n')
}
export default {
${
fileNames
.map(fileName => camelCase(fileName))
.map(fileName => isMerge
? `...${fileName},`
: `${fileName},`
)
.join('\n ')
}
}
`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment