-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateExports.js
47 lines (37 loc) · 1.11 KB
/
generateExports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import * as fs from 'node:fs';
import * as path from 'node:path';
import { promisify } from 'node:util';
const readdirAsync = promisify(fs.readdir);
const lstatAsync = promisify(fs.lstat);
export async function generateExports(dirName) {
try {
const files = await readdirAsync(dirName);
let exportData = '';
for (const file of files) {
if (file === 'index.js') continue;
const filePath = path.join(dirName, file);
const stats = await lstatAsync(filePath);
const isFile = stats.isFile();
if (isFile) {
const fileName = path.parse(file).name;
exportData += `export { default as ${fileName}} from "./${fileName}.js"; \n`;
}
}
fs.writeFileSync('./utils/index.js', exportData, 'utf8');
} catch (err) {
console.error(err);
}
}
generateExports('utils');
// fs.readdir('utils', (err, files) => {
// if (err) {
// console.error(err);
// process.exit(1);
// }
// for (const file of files) {
// const filePath = path.join('utils', file);
// if (!filePath.endsWith('.js')) {
// generateExports(filePath);
// }
// }
// });