|
1 |
| -const fs = require("fs"); |
| 1 | +const fs = require('fs'); |
2 | 2 | const fsp = fs.promises;
|
3 |
| -const path = require("path"); |
| 3 | +const path = require('path'); |
4 | 4 |
|
5 |
| -const fg = require("fast-glob"); |
6 |
| -const { optimize } = require("svgo"); |
7 |
| -const combine = require("svgcombiner"); |
8 |
| -const svgstore = require("svgstore"); |
| 5 | +const fg = require('fast-glob'); |
| 6 | +const { optimize } = require('svgo'); |
| 7 | +const combine = require('svgcombiner'); |
| 8 | +const svgstore = require('svgstore'); |
9 | 9 |
|
10 | 10 | async function setupIconStores() {
|
11 |
| - const stores = new Map(); |
12 |
| - stores.set("all", svgstore()); |
13 |
| - |
14 |
| - for (const variant of await fg(["*"], { |
15 |
| - cwd: __dirname, |
16 |
| - ignore: ["dist", "node_modules"], |
17 |
| - onlyDirectories: true, |
18 |
| - })) { |
19 |
| - stores.set(variant, svgstore()); |
20 |
| - } |
21 |
| - |
22 |
| - return stores; |
| 11 | + const stores = new Map(); |
| 12 | + stores.set('all', svgstore()); |
| 13 | + |
| 14 | + for (const variant of await fg(['*'], { |
| 15 | + cwd: __dirname, |
| 16 | + ignore: ['dist', 'node_modules'], |
| 17 | + onlyDirectories: true, |
| 18 | + })) { |
| 19 | + stores.set(variant, svgstore()); |
| 20 | + } |
| 21 | + |
| 22 | + return stores; |
23 | 23 | }
|
24 | 24 |
|
25 | 25 | async function main() {
|
26 |
| - // Not using svgo's loadConfig because it doesn't support dynamic config files |
27 |
| - const getSvgoConfig = require("./svgo.config.js"); |
| 26 | + // Not using svgo's loadConfig because it doesn't support dynamic config files |
| 27 | + const getSvgoConfig = require('./svgo.config.js'); |
28 | 28 |
|
29 |
| - // Hash to hold all icons arranged by processed name |
30 |
| - const icons = new Map(); |
31 |
| - const stores = await setupIconStores(); |
32 |
| - const variants = [...stores.keys()].filter((key) => key !== "all"); |
33 |
| - |
34 |
| - const files = await fg([`{${variants.join(",")}}/*.svg`], { |
35 |
| - cwd: __dirname, |
36 |
| - onlyFiles: true, |
37 |
| - }); |
| 29 | + // Hash to hold all icons arranged by processed name |
| 30 | + const icons = new Map(); |
| 31 | + const stores = await setupIconStores(); |
| 32 | + const variants = [...stores.keys()].filter((key) => key !== 'all'); |
38 | 33 |
|
39 |
| - const promises = []; |
40 |
| - for (const file of files) { |
41 |
| - const assetName = path.basename(file, ".svg"); |
| 34 | + const files = await fg([`{${variants.join(',')}}/*.svg`], { |
| 35 | + cwd: __dirname, |
| 36 | + onlyFiles: true, |
| 37 | + }); |
42 | 38 |
|
43 |
| - // Capture metadata about the icon from the path and filename |
44 |
| - const variant = path.dirname(file); |
| 39 | + const promises = []; |
| 40 | + for (const file of files) { |
| 41 | + const assetName = path.basename(file, '.svg'); |
45 | 42 |
|
46 |
| - // Read in the SVG contents |
47 |
| - const contents = await fsp.readFile(file, "utf-8"); |
48 |
| - const result = optimize(contents, getSvgoConfig({ clean: true })); |
| 43 | + // Capture metadata about the icon from the path and filename |
| 44 | + const variant = path.dirname(file); |
49 | 45 |
|
50 |
| - icons.set(assetName, { |
51 |
| - ...(icons.has(assetName) ? icons.get(assetName) : {}), |
52 |
| - [variant]: result.data, |
53 |
| - }); |
| 46 | + // Read in the SVG contents |
| 47 | + const contents = await fsp.readFile(file, 'utf-8'); |
| 48 | + const result = optimize(contents, getSvgoConfig({ clean: true })); |
54 | 49 |
|
55 |
| - stores.get(variant).add(assetName, result.data); |
| 50 | + icons.set(assetName, { |
| 51 | + ...(icons.has(assetName) ? icons.get(assetName) : {}), |
| 52 | + [variant]: result.data, |
| 53 | + }); |
56 | 54 |
|
57 |
| - const dest = path.join(__dirname, "dist", file); |
58 |
| - if (!fs.existsSync(path.dirname(dest))) { |
59 |
| - fs.mkdirSync(path.dirname(dest), { recursive: true }); |
60 |
| - } |
| 55 | + stores.get(variant).add(assetName, result.data); |
61 | 56 |
|
62 |
| - promises.push(fsp.writeFile(dest, result.data)); |
| 57 | + const dest = path.join(__dirname, 'dist', file); |
| 58 | + if (!fs.existsSync(path.dirname(dest))) { |
| 59 | + fs.mkdirSync(path.dirname(dest), { recursive: true }); |
63 | 60 | }
|
64 | 61 |
|
65 |
| - // Wait for all the SVG content to be read in and processed before continuing |
66 |
| - await Promise.all(promises); |
| 62 | + promises.push(fsp.writeFile(dest, result.data)); |
| 63 | + } |
67 | 64 |
|
68 |
| - if (icons.size === 0) return; |
| 65 | + // Wait for all the SVG content to be read in and processed before continuing |
| 66 | + await Promise.all(promises); |
69 | 67 |
|
70 |
| - // For each entry in the map, combine the SVG variants into a single SVG file with multiple symbols |
71 |
| - promises.length = 0; |
72 |
| - [...icons.entries()].map(([iconName, data]) => { |
73 |
| - // Combine the SVG variants into a single SVG file with multiple symbols |
74 |
| - const contents = combine(iconName, data); |
| 68 | + if (icons.size === 0) return; |
75 | 69 |
|
76 |
| - // Add the combined SVG to the "all" store so we can generate a single SVG file with all icons |
77 |
| - stores.get("all").add(iconName, contents); |
| 70 | + // For each entry in the map, combine the SVG variants into a single SVG file with multiple symbols |
| 71 | + promises.length = 0; |
| 72 | + [...icons.entries()].map(([iconName, data]) => { |
| 73 | + // Combine the SVG variants into a single SVG file with multiple symbols |
| 74 | + const contents = combine(iconName, data); |
78 | 75 |
|
79 |
| - // Write the combined SVG to the dist folder |
80 |
| - const destPath = path.join(__dirname, "dist/combined"); |
81 |
| - if (!fs.existsSync(destPath)) fs.mkdirSync(destPath, { recursive: true }); |
| 76 | + // Add the combined SVG to the "all" store so we can generate a single SVG file with all icons |
| 77 | + stores.get('all').add(iconName, contents); |
82 | 78 |
|
83 |
| - promises.push(fsp.writeFile(path.join(destPath, `${iconName}.svg`), optimize(contents, getSvgoConfig())?.data)); |
84 |
| - }); |
| 79 | + // Write the combined SVG to the dist folder |
| 80 | + const destPath = path.join(__dirname, 'dist/combined'); |
| 81 | + if (!fs.existsSync(destPath)) fs.mkdirSync(destPath, { recursive: true }); |
85 | 82 |
|
86 |
| - // Finally, we write out the stores to disk |
87 |
| - for (const [identifier, store] of stores.entries()) { |
88 |
| - const isFullSet = identifier === "all"; |
89 |
| - const filename = isFullSet ? "spectrum-css-icons" : `spectrum-css-icons-${identifier}`; |
90 |
| - const dest = path.join(__dirname, `dist/${filename}.svg`); |
91 |
| - const config = getSvgoConfig( |
92 |
| - isFullSet |
93 |
| - ? { |
94 |
| - idPrefix: "spectrum-css-icon", |
95 |
| - removeViewBox: true, |
96 |
| - } |
97 |
| - : {}, |
98 |
| - ); |
99 |
| - |
100 |
| - const result = optimize(store.toString(), config); |
101 |
| - |
102 |
| - if (!result?.data) continue; |
103 |
| - |
104 |
| - promises.push(fsp.writeFile(dest, result?.data)); |
105 |
| - } |
| 83 | + promises.push(fsp.writeFile(path.join(destPath, `${iconName}.svg`), optimize(contents, getSvgoConfig())?.data)); |
| 84 | + }); |
| 85 | + |
| 86 | + // Finally, we write out the stores to disk |
| 87 | + for (const [identifier, store] of stores.entries()) { |
| 88 | + const isFullSet = identifier === 'all'; |
| 89 | + const filename = isFullSet ? 'spectrum-css-icons' : `spectrum-css-icons-${identifier}`; |
| 90 | + const dest = path.join(__dirname, `dist/${filename}.svg`); |
| 91 | + const config = getSvgoConfig( |
| 92 | + isFullSet |
| 93 | + ? { |
| 94 | + idPrefix: 'spectrum-css-icon', |
| 95 | + removeViewBox: true, |
| 96 | + } |
| 97 | + : {}, |
| 98 | + ); |
| 99 | + |
| 100 | + const result = optimize(store.toString(), config); |
106 | 101 |
|
107 |
| - return Promise.all(promises); |
| 102 | + if (!result?.data) continue; |
| 103 | + |
| 104 | + promises.push(fsp.writeFile(dest, result?.data)); |
| 105 | + } |
| 106 | + |
| 107 | + return Promise.all(promises); |
108 | 108 | }
|
109 | 109 |
|
110 | 110 | main()
|
111 |
| - .then(() => { |
112 |
| - console.log("✔ Icons generated successfully."); |
113 |
| - process.exit(0); |
114 |
| - }) |
115 |
| - .catch((e) => { |
116 |
| - console.error(e); |
117 |
| - process.exit(1); |
118 |
| - }); |
| 111 | + .then(() => { |
| 112 | + console.log('✔ Icons generated successfully.'); |
| 113 | + process.exit(0); |
| 114 | + }) |
| 115 | + .catch((e) => { |
| 116 | + console.error(e); |
| 117 | + process.exit(1); |
| 118 | + }); |
0 commit comments