-
Notifications
You must be signed in to change notification settings - Fork 4
/
generateSvgIndex.js
48 lines (37 loc) · 1.63 KB
/
generateSvgIndex.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
48
// 입력하기 전에 color, size 관련 생각 한번 더하기 - ex ) modifier, social
// node generateSvgIndex.js 입력하면 src/icon/svg 폴더에 있는 svg 파일들을 폴더 별로 index.ts 파일로 만들어줌
import { readdirSync, readFileSync, writeFileSync } from "fs";
import { join, parse } from "path";
const SVG_ROOT_DIRECTORY = "./src/icon/svg";
function generateIndexFiles(rootDirectory) {
const directories = getDirectories(rootDirectory);
directories.forEach(directory => {
const indexFilePath = join(directory, "index.ts");
const files = readdirSync(directory);
const exports = files
.filter(file => file.endsWith(".svg"))
.map(file => {
const filePath = join(directory, file);
const fileName = parse(file).name;
const sanitizedFileName = fileName.replace(/-/g, "_");
let svgContent = readFileSync(filePath, "utf-8");
svgContent = updateSvgContent(svgContent);
writeFileSync(filePath, svgContent);
return `export { default as ${sanitizedFileName} } from './${fileName}.svg';`;
});
const content = join("\n");
writeFileSync(indexFilePath, content);
});
}
function getDirectories(directory) {
return readdirSync(directory, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => join(directory, dirent.name));
}
function updateSvgContent(svgContent) {
svgContent = svgContent.replace(/(width|height)="[^"]*"/g, '$1="current"');
// social , modifier 색깔 변경 x
// svgContent = svgContent.replace(/fill="[^"]*"/g, 'fill="current"');
return svgContent;
}
generateIndexFiles(SVG_ROOT_DIRECTORY);