2018-08-26 23:23:57 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Compiles our icons into static .js files that can be imported in the browser
|
2021-02-08 18:14:31 +02:00
|
|
|
* and are tree-shakeable. The static .js files go in icons/{filename}.js. Also
|
|
|
|
* generates an index.js that exports all icons by title, but is not
|
|
|
|
* tree-shakeable
|
2018-08-26 23:23:57 +02:00
|
|
|
*/
|
|
|
|
|
2019-07-14 21:05:38 +02:00
|
|
|
const fs = require("fs");
|
2021-02-08 18:14:31 +02:00
|
|
|
const path = require("path");
|
2019-07-14 21:05:38 +02:00
|
|
|
const util = require("util");
|
|
|
|
const minify = require("uglify-js").minify;
|
|
|
|
|
2019-08-15 13:23:35 +02:00
|
|
|
const UTF8 = "utf8";
|
|
|
|
|
2021-02-08 18:14:31 +02:00
|
|
|
const dataFile = path.resolve(__dirname, "..", "_data", "simple-icons.json");
|
|
|
|
const indexFile = path.resolve(__dirname, "..", "index.js");
|
|
|
|
const iconsDir = path.resolve(__dirname, "..", "icons");
|
2019-08-15 13:23:35 +02:00
|
|
|
|
2021-02-08 18:14:31 +02:00
|
|
|
const indexTemplateFile = path.resolve(__dirname, "templates", "index.js");
|
|
|
|
const iconObjectTemplateFile = path.resolve(__dirname, "templates", "icon-object.js");
|
2019-08-15 13:23:35 +02:00
|
|
|
|
|
|
|
const indexTemplate = fs.readFileSync(indexTemplateFile, UTF8);
|
|
|
|
const iconObjectTemplate = fs.readFileSync(iconObjectTemplateFile, UTF8);
|
2018-08-26 23:23:57 +02:00
|
|
|
|
2019-07-14 21:05:38 +02:00
|
|
|
const data = require(dataFile);
|
2021-02-08 18:14:31 +02:00
|
|
|
const { titleToSlug } = require("./utils.js");
|
2018-08-26 23:23:57 +02:00
|
|
|
|
2019-07-14 17:07:24 +02:00
|
|
|
// Local helper functions
|
2019-07-14 21:15:26 +02:00
|
|
|
function escape(value) {
|
2020-07-28 12:33:40 +02:00
|
|
|
return value.replace(/(?<!\\)'/g, "\\'");
|
2019-07-14 21:15:26 +02:00
|
|
|
}
|
2019-07-14 17:07:24 +02:00
|
|
|
function iconToKeyValue(icon) {
|
2021-02-19 15:06:43 +02:00
|
|
|
let iconName = escape(icon.title);
|
|
|
|
if (icon.slug !== titleToSlug(icon.title)) {
|
|
|
|
iconName = icon.slug;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `'${iconName}':${iconToObject(icon)}`;
|
2019-07-14 17:07:24 +02:00
|
|
|
}
|
|
|
|
function iconToObject(icon) {
|
2019-08-15 13:23:35 +02:00
|
|
|
return util.format(iconObjectTemplate,
|
|
|
|
escape(icon.title),
|
|
|
|
escape(icon.slug),
|
|
|
|
escape(icon.svg),
|
|
|
|
escape(icon.source),
|
|
|
|
escape(icon.hex)
|
|
|
|
);
|
2019-07-14 17:07:24 +02:00
|
|
|
}
|
2021-02-08 18:14:31 +02:00
|
|
|
function minifyAndWrite(filepath, rawJavaScript) {
|
|
|
|
const { error, code } = minify(rawJavaScript);
|
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
} else {
|
|
|
|
fs.writeFileSync(filepath, code);
|
|
|
|
}
|
|
|
|
}
|
2019-07-14 17:07:24 +02:00
|
|
|
|
|
|
|
// 'main'
|
|
|
|
const icons = [];
|
2018-08-26 23:23:57 +02:00
|
|
|
data.icons.forEach(icon => {
|
2021-02-19 15:06:43 +02:00
|
|
|
const filename = icon.slug || titleToSlug(icon.title);
|
2021-02-08 18:14:31 +02:00
|
|
|
const svgFilepath = path.resolve(iconsDir, `${filename}.svg`);
|
|
|
|
icon.svg = fs.readFileSync(svgFilepath, UTF8).replace(/\r?\n/, '');
|
|
|
|
icon.slug = filename;
|
|
|
|
icons.push(icon);
|
2019-08-15 13:23:35 +02:00
|
|
|
|
2021-02-08 18:14:31 +02:00
|
|
|
// write the static .js file for the icon
|
|
|
|
const jsFilepath = path.resolve(iconsDir, `${filename}.js`);
|
|
|
|
minifyAndWrite(jsFilepath, `module.exports=${iconToObject(icon)};`);
|
2018-08-26 23:23:57 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// write our generic index.js
|
2019-08-15 13:23:35 +02:00
|
|
|
const rawIndexJs = util.format(indexTemplate, icons.map(iconToKeyValue).join(','));
|
2021-02-08 18:14:31 +02:00
|
|
|
minifyAndWrite(indexFile, rawIndexJs);
|