-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgenerate-data.js
More file actions
179 lines (138 loc) · 5.52 KB
/
generate-data.js
File metadata and controls
179 lines (138 loc) · 5.52 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
const path = require('path');
const fse = require('fs-extra');
const fs = require('fs');
const _ = require('lodash');
import { getFileData, generateGlyphs, writeJSONToDisk, writeToDisk } from './utils';
// generate config data used to generate the font icon
const files = getFileData('./svgs');
const glyphs = generateGlyphs(files);
const packagePath = process.cwd();
const buildPath = path.join(packagePath, './build');
const svgPath = path.join(packagePath, './svgs');
const componentPath = path.join(packagePath, './components');
const configData = {
name: 'compass-icons',
css_prefix_text: 'icon-',
css_use_suffix: false,
hinting: false,
units_per_em: 1000,
ascent: 850,
glyphs,
};
const formatToPascalCase = (str) => _.camelCase(str).replace(/^(.)/, _.toUpper);
// write to root for fontelle-cli to retrieve
writeJSONToDisk('config.json', configData);
// write to build folder
writeJSONToDisk('./build/config.json', configData);
// generate icon glyph data
const iconGlyphs = configData.glyphs.map(({ css }) => `\t'${css}',`);
const iconGlyphsData = `const IconGlyphs: IconGlyphTypes[] = [\n${iconGlyphs.join('\n\t')}\n];
export type IconGlyphTypes = ${iconGlyphs.map((glyph) => glyph.trim().slice(0, -1)).join(' | ')};
export type NonBrokenIconGlyphTypes = ${iconGlyphs
.filter((glyph) => !glyph.includes('BRKN'))
.map((glyph) => glyph.trim().slice(0, -1))
.join(' | ')};
export default IconGlyphs;
`;
writeToDisk('./IconGlyphs.ts', iconGlyphsData);
const componentTemplate = ({ name, content }) => `import React from 'react';
import IconProps from './props';
const ${name}Icon: React.FC<IconProps> = ({ size, color, ...rest}: IconProps): JSX.Element => (
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width={size || '1em'}
height={size || '1em'}
fill={color || 'currentColor'}
viewBox="0 0 24 24"
{...rest}
>
${content}
</svg>
);
export default ${name}Icon;
`;
const generateComponents = async () => {
const svgFileNames = fs.readdirSync('./svgs');
const filtered = svgFileNames
.filter((name) => !name.startsWith('BRKN-'))
.map((nonBroken) => {
const sanitized = nonBroken.slice(0, -4).split('_')[0];
return {
fileName: nonBroken,
original: sanitized,
pascalName: formatToPascalCase(sanitized),
};
});
fs.mkdir(componentPath, () => console.log('##### created components folder'));
const promises = [];
for (let i = 0; i < filtered.length; i++) {
promises.push(readAndWriteFiles(filtered[i]));
}
promises.push(createComponentProps());
promises.push(createComponentIndex(filtered));
return Promise.all(promises);
};
const readAndWriteFiles = (item) =>
new Promise((resolve, reject) => {
const regex = /(?<group>(?<starttag><(?<tagname>svg)[^>]*>)(?<data>.*|\n*)(?<endtag><\/\3>))/gmsu;
const fileData = fs.readFileSync(path.join(svgPath, item.fileName), { encoding: 'utf-8' });
const result = regex.exec(fileData);
if (result === null) {
reject(item.fileName);
}
if (result) {
const classRegex = /class=".*?"/gmu;
const sanitizedResult = result.groups.data.replace(classRegex, '').replace('\n', '').replace('\t', '');
fs.writeFile(
path.join(componentPath, `${item.original}.tsx`),
componentTemplate({ name: item.pascalName, content: sanitizedResult }),
() => {}
);
resolve(item.fileName);
}
});
const createComponentIndex = (files) =>
new Promise((resolve, reject) => {
const indexFileData = `import { NonBrokenIconGlyphTypes } from '../IconGlyphs';\n\nimport IconProps from './props';\n${files
.map((item) => `import ${item.pascalName}Icon from './${item.original}';`)
.join('\n')}\n\nexport {\n${files
.map((item) => `${item.pascalName}Icon,`)
.join('\n\t')}\n};\n\nconst glyphMap: { [key in NonBrokenIconGlyphTypes]: React.FC<IconProps> } = {${files
.map((item) => `"${item.original}": ${item.pascalName}Icon,`)
.join('\n\t')}};\n\nexport default glyphMap`;
fs.writeFile(path.join(componentPath, 'index.tsx'), indexFileData, () => {});
resolve();
});
const createComponentProps = () =>
new Promise((resolve) => {
const indexFileData = `type IconProps = {\n\tsize?:number|string;\n\tcolor?:string;\n\ttitle?:string;\n\tclassName?:string;\n};\n\nexport default IconProps;`;
fs.writeFile(path.join(componentPath, 'props.ts'), indexFileData, () => {});
resolve();
});
async function createPackageFile() {
const packageData = await fse.readFile(path.resolve(packagePath, './package.json'), 'utf8');
const {
nyc,
scripts,
devDependencies,
husky,
workspaces,
'lint-staged': lintStaged,
files,
main,
...packageDataOther
} = JSON.parse(packageData);
const newPackageData = {
...packageDataOther,
main: 'css/compass-icons.css',
private: false,
};
const targetPath = path.resolve(buildPath, './package.json');
await fse.writeFile(targetPath, JSON.stringify(newPackageData, null, 2), 'utf8');
console.log(`Created package.json in ${targetPath}`);
}
createPackageFile()
.then(() => generateComponents())
.then(() => console.log('### Finished creating the project files'));