forked from gsoft-inc/ov-igloo-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
123 lines (97 loc) · 2.99 KB
/
build.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require('fs');
const path = require('path');
const camelCase = require('camelcase');
const { optimize } = require('svgo');
const { config } = require('./scripts/config');
const { getAllFiles } = require('./scripts/helper');
const generateSvg = require('./scripts/generate-svg');
const generateComponent = require('./scripts/generate-component');
const generateList = require('./scripts/generate-list');
const {
ICONS_SOURCE_DIR,
ICONS_DIST_DIR,
ICONS_SIZES,
} = require('./scripts/constants');
const getFiles = (dir) => {
return new Promise((resolve, reject) => {
if (fs.existsSync(dir)) {
resolve(getAllFiles(dir));
} else {
reject(`no such directory ${dir}`);
}
});
};
const checkSize = (width, height, name) => {
const sizes = ICONS_SIZES;
if (sizes.includes(Number(width)) && sizes.includes(Number(height))) {
return true;
} else {
console.error(
`The size of ${name} is not correct. width: ${width} height: ${height}`
);
process.exit(1);
}
};
const checkSameName = (data) => {
const sizes = ICONS_SIZES;
const dataGroupedBySize = sizes.map((s) => data.filter((d) => d.group === s));
dataGroupedBySize.map((data) => {
const unique = [...new Set(data.map((d) => d.name))];
const lookup = data.reduce((a, e) => {
a[e.name] = ++a[e.name] || 0;
return a;
}, {});
if (data.length === unique.length) {
return true;
} else {
console.error(
`Array contains duplicates icon name: `,
data.filter((e) => lookup[e.name])
);
process.exit(1);
}
});
};
const handleName = (file) => {
const splitPath = file.split(path.sep);
const fileName = splitPath[splitPath.length - 1].replace('.svg', '');
const [, group] = splitPath;
const options = { pascalCase: true };
const formatedName =
fileName === '1-on-1'
? camelCase('OneOnOne', options)
: camelCase(fileName, options);
const formatedGroup = Number(group.replace('px', ''));
return { name: formatedName, group: formatedGroup };
};
const build = async (dir) => {
console.log('\nBuild started...');
const response = await getFiles(dir);
const files = response.filter((word) => !word.includes('.DS_Store'));
const result = files.map((file) => {
const { name, group } = handleName(file);
const svg = fs.readFileSync(file, 'utf-8');
const svgo = optimize(svg, { path: file, ...config });
const { info, data } = svgo;
checkSize(info.width, info.height, name);
return {
name,
group,
size: { width: info.width, height: info.height },
data,
};
});
checkSameName(result);
return result;
};
build(ICONS_SOURCE_DIR)
.then((icons) => {
// generate new svg files with svgo optimization
generateSvg(icons, ICONS_DIST_DIR);
// generate react components
generateComponent(icons);
// generate icons list for docs
generateList(icons);
})
.catch((error) => console.error(error))
.then(() => console.log('\n🚀 Build completed!\n'));