-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-icons.js
101 lines (90 loc) · 3.96 KB
/
build-icons.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
const { resolve, relative, join } = require('path');
const { readFileSync, writeFileSync } = require('fs');
const { readdir } = require('fs').promises;
const set = require('lodash/set')
const get = require('lodash/get');
const { writeFile, mkdir } = require('fs/promises');
async function getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
}));
return Array.prototype.concat(...files);
}
function makeIconsReadme(template, iconFilenames, templateFilename) {
const headerStopText = '## Icons';
let headerStopIdx = template.indexOf(headerStopText);
if (headerStopIdx === -1) {
throw new Error(`The template stop line "${headerStopText}" was not found in the Icons readme template: ${templateFilename}`);
}
let readmeContents = `${template.slice(0, headerStopIdx)}${headerStopText}\n\n`;
const iconSection = iconFilenames.map(iconFilename => {
const absoluteIconsPath = resolve(__dirname, 'icons');
const relativeFilename = relative(absoluteIconsPath, iconFilename);
let iconName = iconFilename.substr(iconFilename.lastIndexOf('/') + 1);
iconName = iconName.replace('.svg', '').replace(/[_-]/g, ' ');
return `![${iconName}](https://raw.githubusercontent.com/kadena-community/design-system/main/icons/${relativeFilename} "${iconName}")`;
});
return readmeContents + iconSection.join('\n');
}
(async function () {
const iconFilenames = (await getFiles('./icons')).filter(x => x.endsWith('.svg'));
const svgs = iconFilenames.map(svg => {
const content = readFileSync(svg, 'utf8');
const file = relative('./icons', svg).split('/').pop();
const name = (content.match(/name="(.*?)"/) ?? [])[1] ?? file.replace('.svg', '');
const style = (content.match(/data-style="(.*?)"/) ?? [])[1];
const description = (content.match(/description="(.*?)"/) ?? [])[1];
const [svgTagMatch] = content.match(/<svg[^>]*>/);
const [, width] = svgTagMatch.match(/width="([\d.]+)(px)?"/) ?? []
const [, height] = svgTagMatch.match(/height="([\d.]+)(px)?"/) ?? []
if (style !== 'animated' || style !== 'custom') {
return {
path: svg,
folder: relative('./icons', svg).split('/').slice(0, -1).concat(`${style}_${name}`),
file,
style,
content,
name,
dimensions: { width, height },
description: description ?? ''
}
}
});
const jsonFile = svgs.reduce((memo, svg) => {
if (svg?.style !== 'animated' && svg?.style !== 'custom' && svg) {
set(memo, svg.folder.join('.'), {
"$type": "icon",
"$name": svg.name,
"$description": svg.description,
"$style": svg.style,
"$value": svg.content,
"$dimensions": svg.dimensions,
...get(memo, svg.folder.join('.'))
})
}
return memo;
}, {});
Object.keys(jsonFile).forEach(async (iconType) => {
const directoryPath = `./tokens/foundation/icon/${iconType}`
await mkdir(directoryPath, { recursive: true });
const writeFilePath = join(__dirname, `${directoryPath}/svg.${iconType}.tokens.json`)
await writeFile(writeFilePath, JSON.stringify({
kda: {
foundation: {
icon: {
[iconType]: jsonFile[iconType]
}
}
}
}), { flag: 'w', encoding: 'utf-8' })
})
const writeSVGFilePath = join(__dirname, `./builds/tokens/kda-design-system.raw.svg.tokens.json`)
await writeFile(writeSVGFilePath, JSON.stringify({ kda: { foundation: { icon: jsonFile } } }), { flag: 'w', encoding: 'utf-8' })
const readmeTemplateFilename = join(__dirname, './icons/README.md.template');
const readmeFilename = join(__dirname, './icons/README.md');
const template = readFileSync(readmeTemplateFilename).toString();
const readmeContents = makeIconsReadme(template, iconFilenames, readmeTemplateFilename);
writeFileSync(readmeFilename, readmeContents);
})()