2018-08-27 00:23:57 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Compiles our icons into static .js files that can be imported in the browser
|
2021-02-08 19:14:31 +03: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-27 00:23:57 +03:00
|
|
|
*/
|
|
|
|
|
2021-10-25 22:13:10 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const util = require('util');
|
|
|
|
const { minify } = require('uglify-js');
|
2019-07-14 22:05:38 +03:00
|
|
|
|
2021-10-25 22:13:10 +03:00
|
|
|
const UTF8 = 'utf8';
|
2019-08-15 14:23:35 +03:00
|
|
|
|
2021-10-25 22:13:10 +03:00
|
|
|
const rootDir = path.resolve(__dirname, '..', '..');
|
|
|
|
const dataFile = path.resolve(rootDir, '_data', 'simple-icons.json');
|
|
|
|
const indexFile = path.resolve(rootDir, 'index.js');
|
|
|
|
const iconsDir = path.resolve(rootDir, 'icons');
|
2019-08-15 14:23:35 +03:00
|
|
|
|
2021-10-25 22:13:10 +03:00
|
|
|
const templatesDir = path.resolve(__dirname, 'templates');
|
|
|
|
const indexTemplateFile = path.resolve(templatesDir, 'index.js');
|
|
|
|
const iconObjectTemplateFile = path.resolve(templatesDir, 'icon-object.js');
|
2019-08-15 14:23:35 +03:00
|
|
|
|
|
|
|
const indexTemplate = fs.readFileSync(indexTemplateFile, UTF8);
|
|
|
|
const iconObjectTemplate = fs.readFileSync(iconObjectTemplateFile, UTF8);
|
2018-08-27 00:23:57 +03:00
|
|
|
|
2019-07-14 22:05:38 +03:00
|
|
|
const data = require(dataFile);
|
2021-10-25 22:13:10 +03:00
|
|
|
const { getIconSlug } = require('../utils.js');
|
2018-08-27 00:23:57 +03:00
|
|
|
|
2019-07-14 18:07:24 +03:00
|
|
|
// Local helper functions
|
2021-10-25 22:13:10 +03:00
|
|
|
const escape = (value) => {
|
2020-07-28 13:33:40 +03:00
|
|
|
return value.replace(/(?<!\\)'/g, "\\'");
|
2021-10-25 22:13:10 +03:00
|
|
|
};
|
|
|
|
const iconToKeyValue = (icon) => {
|
2021-05-25 19:40:11 +03:00
|
|
|
return `'${icon.slug}':${iconToObject(icon)}`;
|
2021-10-25 22:13:10 +03:00
|
|
|
};
|
|
|
|
const licenseToObject = (license) => {
|
2021-03-02 15:01:31 +03:00
|
|
|
if (license === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (license.url === undefined) {
|
2021-04-15 19:33:25 +03:00
|
|
|
license.url = `https://spdx.org/licenses/${license.type}`;
|
2021-03-02 15:01:31 +03:00
|
|
|
}
|
|
|
|
return license;
|
2021-10-25 22:13:10 +03:00
|
|
|
};
|
|
|
|
const iconToObject = (icon) => {
|
|
|
|
return util.format(
|
|
|
|
iconObjectTemplate,
|
2019-08-15 14:23:35 +03:00
|
|
|
escape(icon.title),
|
|
|
|
escape(icon.slug),
|
|
|
|
escape(icon.svg),
|
|
|
|
escape(icon.source),
|
2021-03-02 15:01:31 +03:00
|
|
|
escape(icon.hex),
|
2021-03-26 12:33:04 +03:00
|
|
|
icon.guidelines ? `'${escape(icon.guidelines)}'` : undefined,
|
2021-03-02 15:01:31 +03:00
|
|
|
licenseToObject(icon.license),
|
2019-08-15 14:23:35 +03:00
|
|
|
);
|
2021-10-25 22:13:10 +03:00
|
|
|
};
|
|
|
|
const minifyAndWrite = (filepath, rawJavaScript) => {
|
2021-02-08 19:14:31 +03:00
|
|
|
const { error, code } = minify(rawJavaScript);
|
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
} else {
|
|
|
|
fs.writeFileSync(filepath, code);
|
|
|
|
}
|
2021-10-25 22:13:10 +03:00
|
|
|
};
|
2019-07-14 18:07:24 +03:00
|
|
|
|
|
|
|
// 'main'
|
|
|
|
const icons = [];
|
2021-10-25 22:13:10 +03:00
|
|
|
data.icons.forEach((icon) => {
|
2021-02-22 16:15:37 +03:00
|
|
|
const filename = getIconSlug(icon);
|
2021-02-08 19:14:31 +03: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 14:23:35 +03:00
|
|
|
|
2021-02-08 19:14:31 +03:00
|
|
|
// write the static .js file for the icon
|
|
|
|
const jsFilepath = path.resolve(iconsDir, `${filename}.js`);
|
|
|
|
minifyAndWrite(jsFilepath, `module.exports=${iconToObject(icon)};`);
|
2018-08-27 00:23:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// write our generic index.js
|
2021-10-25 22:13:10 +03:00
|
|
|
const rawIndexJs = util.format(
|
|
|
|
indexTemplate,
|
|
|
|
icons.map(iconToKeyValue).join(','),
|
|
|
|
);
|
2021-02-08 19:14:31 +03:00
|
|
|
minifyAndWrite(indexFile, rawIndexJs);
|