UNPKG

recursive-barrel

Version:

40 lines (35 loc) 840 B
const fs = require('fs').promises const path = require('path') async function createBarrel(key, dir, n = 10, depth = 1) { if (depth === 0) { await fs.writeFile( path.join(dir, 'index.mjs'), `export const ${key} = ${JSON.stringify(key)}` ) return } await fs.writeFile( path.join(dir, 'index.mjs'), Array(n) .fill(0) .map((_, i) => `export * from './${i}'`) .join('\n') ) await Promise.all( Array(n) .fill(0) .map(async (_, i) => { await fs.mkdir(path.join(dir, i.toString())) await createBarrel( key + '_' + i, path.join(dir, i.toString()), n, depth - 1 ) }) ) } // 10^4 = 10,000 fs.mkdir(path.join(__dirname, 'dist')).then(() => createBarrel('b', path.join(__dirname, 'dist'), 10, 4) )