This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
101 lines (80 loc) · 2.89 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
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const rimraf = require("rimraf");
// ----------------------------------------------------------------------------
// Options
const OPTIONS = {
outputDir: "./components",
svgDir: "./node_modules/feather-icons/dist/icons/",
};
// ----------------------------------------------------------------------------
// Helper functions
const pascalCase = destPath => {
const splitregex = new RegExp(`[${path.sep}-]+`);
let parts = destPath.split(splitregex);
parts = parts.map(part => {
return part.charAt(0).toUpperCase() + part.substring(1);
});
return parts.join("");
};
const replaceExt = (destPath, newExt) => {
return path.parse(destPath).base.replace(/.svg$/, newExt);
};
// ----------------------------------------------------------------------------
// Build components
const processFile = svgPath => {
const destPath = replaceExt(svgPath, ".js").replace(/_/g, "-");
const outputFileDirJs = path.dirname(path.join(OPTIONS.outputDir, destPath));
if (!fs.existsSync(outputFileDirJs)) {
fs.mkdirSync(outputFileDirJs, { recursive: true });
}
const absDestPathJs = path.join(OPTIONS.outputDir, pascalCase(destPath));
const svgData = loadSvgData(svgPath);
const fileString =
'const m = require("mithril");\nmodule.exports = m(\n ".icon",\n' +
` m.trust(\n '${svgData}',\n ),\n);\n`;
fs.writeFileSync(absDestPathJs, fileString);
};
const loadSvgData = svgPath => {
return fs
.readFileSync(svgPath, { encoding: "utf8" })
.replace(/<!--.*-->/g, "")
.replace(/<!DOCTYPE.*?>/, "")
.replace(/<\?xml .*?>/, "")
.replace(/\n|\r|\t/g, " ")
.replace(/\s+/g, " ")
.replace(/>\s+</g, "><")
.replace(/\s+$/, "")
.replace(/^\s+/, "");
};
// ----------------------------------------------------------------------------
// Build index
const buildIndex = files => {
const content = files
.map(svgPath => {
const className = pascalCase(replaceExt(svgPath, ""));
return ` ${className}: require("./components/${className}"),\n`;
})
.join("");
const fileString = `module.exports = {\n${content}};\n`;
fs.writeFileSync("./index.js", fileString);
};
// ----------------------------------------------------------------------------
// Main
const main = () => {
// 1. Clean the output directory / ensure it exists.
rimraf.sync(OPTIONS.outputDir);
fs.mkdirSync(OPTIONS.outputDir, { recursive: true });
// 2. Build a list of all files with the matching extension from the SVG
// directory.
const files = glob.sync(path.join(OPTIONS.svgDir, "/**/*.svg"));
// 3. Construct a Mithril component from each SVG file, writing out the
// results to the filesystem.
files.forEach(svgPath => processFile(svgPath));
// 4. Build the index, exporting all components for external use.
buildIndex(files);
};
if (require.main === module) {
main();
}