1 | const fs = require('fs').promises
|
2 | const path = require('path')
|
3 |
|
4 | async function createBarrel(key, dir, n = 10, depth = 1) {
|
5 | if (depth === 0) {
|
6 | await fs.writeFile(
|
7 | path.join(dir, 'index.mjs'),
|
8 | `export const ${key} = ${JSON.stringify(key)}`
|
9 | )
|
10 | return
|
11 | }
|
12 |
|
13 | await fs.writeFile(
|
14 | path.join(dir, 'index.mjs'),
|
15 | Array(n)
|
16 | .fill(0)
|
17 | .map((_, i) => `export * from './${i}'`)
|
18 | .join('\n')
|
19 | )
|
20 |
|
21 | await Promise.all(
|
22 | Array(n)
|
23 | .fill(0)
|
24 | .map(async (_, i) => {
|
25 | await fs.mkdir(path.join(dir, i.toString()))
|
26 | await createBarrel(
|
27 | key + '_' + i,
|
28 | path.join(dir, i.toString()),
|
29 | n,
|
30 | depth - 1
|
31 | )
|
32 | })
|
33 | )
|
34 | }
|
35 |
|
36 |
|
37 | fs.mkdir(path.join(__dirname, 'dist')).then(() =>
|
38 | createBarrel('b', path.join(__dirname, 'dist'), 10, 4)
|
39 | )
|