-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate-imports.cjs
49 lines (38 loc) · 1.52 KB
/
generate-imports.cjs
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
48
49
const fs = require('fs');
const path = require('path');
const rootDir = process.argv[2];
if (!rootDir) {
console.error('Please provide a root directory as an argument.');
process.exit(1);
}
const getSvgFilesInDir = (dir) => {
return fs.readdirSync(dir).filter(file => {
const ext = path.extname(file);
return ['.png', '.svg', '.gif', '.webp', '.jpeg', '.jpg'].includes(ext);
}).map(file => path.join(dir, file))
};
const generateNamespaceExportsForDir = (dir) => {
const svgFiles = getSvgFilesInDir(dir);
if (svgFiles.length === 0) return; // Skip folders without SVGs
const namespaceExports = svgFiles.map(file => {
const variableName = path.basename(file, path.extname(file))
.replace(/^(_+)/, '$1')
.replace(/-([a-z0-9])/gi, (_, g) => g.toUpperCase());
return `export * as ${variableName} from './${path.basename(file)}';`;
}).join('\n');
const outputPath = path.join(dir, 'index.ts');
fs.writeFileSync(outputPath, namespaceExports, 'utf-8');
};
const walkDirectories = (dir) => {
const items = fs.readdirSync(dir);
for (const item of items) {
const itemPath = path.join(dir, item);
const stat = fs.statSync(itemPath);
if (stat && stat.isDirectory()) {
generateNamespaceExportsForDir(itemPath);
walkDirectories(itemPath);
}
}
};
generateNamespaceExportsForDir(rootDir); // Generate for rootDir itself
walkDirectories(rootDir); // Then walk its subdirectories