Created
September 28, 2021 02:14
-
-
Save zthxxx/95c55b76995e42a92ed1682d71fe1318 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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