-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.js
executable file
·147 lines (119 loc) · 4.45 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const readdir = require("recursive-readdir");
const camelCase = require("lodash.camelcase");
const upperFirst = require("lodash.upperfirst");
const uniqBy = require("lodash.uniqby");
const pkgPath = path.join(__dirname, "./node_modules/material-design-icons");
const cryptoPath = path.join(__dirname, "./token-icons");
const outBaseDir = path.join(__dirname, "./svg");
const examplesBaseDir = path.join(__dirname, "./examples");
const mdOutDir = path.join(__dirname + "/svg", "./md");
const mdExamplesDir = path.join(__dirname + "/examples", "./md");
const cryptoOutDir = path.join(__dirname + "/svg", "./tokens");
const cryptoExamplesDir = path.join(__dirname + "/examples", "./tokens");
const ignore = (file, stats) => {
if (stats.isDirectory()) return false;
// ignore svg/design directories
if (file === "design") return true;
return !/\.svg$/.test(file);
};
const is24px = filename => /24px.svg$/.test(filename);
const rename = filename =>
path
.basename(filename, path.extname(filename))
.replace(/^ic_/, "")
.replace(/_24px$/, "")
.replace(/^3d/, "ThreeD"); // remove number from beginning
const readFile = filename => {
const content = fs.readFileSync(filename, "utf8");
const name = camelCase(rename(filename));
return {
filename,
name,
content
};
};
const writeMdFile = ({ name, content }) => {
const filename = path.join(mdOutDir, name + ".svg");
fs.writeFileSync(filename, content);
};
const writeCryptoFile = ({ name, content }) => {
const filename = path.join(cryptoOutDir, name + ".svg");
fs.writeFileSync(filename, content);
};
const exampleTemplate = ({ name }) => `import React from 'react'
import { ${name} } from '..'
export default props => (
<${name}
size={48}
color='#07c'
/>
)`;
const createMdExample = ({ name }) => {
const content = exampleTemplate({
name: upperFirst(name)
});
const filename = path.join(mdExamplesDir, upperFirst(name) + ".js");
fs.writeFileSync(filename, content);
};
const createCryptoExample = ({ name }) => {
const content = exampleTemplate({
name: upperFirst(name)
});
const filename = path.join(cryptoExamplesDir, upperFirst(name) + ".js");
fs.writeFileSync(filename, content);
};
const docTemplate = ({ icons = [] }) => `
# Icons (${icons.length})
${icons.map(({ name }) => `- \`${upperFirst(name)}\``).join("\n")}
`;
const createDoc = icons => {
const filename = path.join(__dirname, "./ICONS.md");
const content = docTemplate({ icons });
fs.writeFileSync(filename, content);
};
const copy = async () => {
// Create missing base directories
if (!fs.existsSync(outBaseDir)) fs.mkdirSync(outBaseDir);
if (!fs.existsSync(examplesBaseDir)) fs.mkdirSync(examplesBaseDir);
// Read crypto icons from local directory
const cryptoFiles = await readdir(cryptoPath, [ignore]);
// Sort crypto icons
const cryptoIcons = uniqBy(cryptoFiles, file => path.basename(file))
.map(readFile)
.sort((a, b) => (a.name < b.name ? -1 : 1));
// Create missing crypto directories
if (!fs.existsSync(cryptoOutDir)) fs.mkdirSync(cryptoOutDir);
if (!fs.existsSync(cryptoExamplesDir)) fs.mkdirSync(cryptoExamplesDir);
// Copy crypto icons to svg directory
cryptoIcons.forEach(writeCryptoFile);
// Create crypto examples
cryptoIcons.forEach(createCryptoExample);
console.log(cryptoIcons.length, " token icons copied");
// Read material icons
const mdFiles = await readdir(pkgPath, [ignore]);
// Grab crypto icon names so we can easily resolve name collisions with material icons
const cryptoIconNames = cryptoIcons.map(icon => icon.name);
// Sort material icons
const mdIcons = uniqBy(mdFiles, file => path.basename(file))
.filter(is24px)
.map(readFile)
.map(icon => cryptoIconNames.includes(icon.name) ? { ...icon, name: `${icon.name}Icon` } : icon)
.sort((a, b) => (a.name < b.name ? -1 : 1));
// Create missing material directories
if (!fs.existsSync(mdOutDir)) fs.mkdirSync(mdOutDir);
if (!fs.existsSync(mdExamplesDir)) fs.mkdirSync(mdExamplesDir);
// Copy material icons to svg directory
mdIcons.forEach(writeMdFile);
// Create material examples
mdIcons.forEach(createMdExample);
console.log(mdIcons.length, " material icons copied");
// Combine icon sets
const combinedIcons = mdIcons.concat(cryptoIcons);
// Create markdown doc of all icons
createDoc(combinedIcons);
console.log(combinedIcons.length, " icons added to ICONS.md");
};
copy();