2021-08-26 13:22:20 +02:00
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
|
import outlineStroke from 'svg-outline-stroke';
|
|
|
|
|
import { parse, stringify } from 'svgson'; // eslint-disable-line import/no-extraneous-dependencies
|
|
|
|
|
|
2020-10-19 06:26:39 -03:00
|
|
|
const inputDir = `./icons/`;
|
2021-07-26 13:12:13 -05:00
|
|
|
const outputDirs = {
|
2021-09-26 12:46:47 +02:00
|
|
|
'converted_icons-200': '1',
|
|
|
|
|
'converted_icons-300': '1.5',
|
2021-08-26 13:22:20 +02:00
|
|
|
converted_icons: '2',
|
2021-09-26 12:46:47 +02:00
|
|
|
'converted_icons-500': '2.5',
|
|
|
|
|
'converted_icons-600': '3',
|
2021-08-26 13:22:20 +02:00
|
|
|
};
|
2020-10-19 06:26:39 -03:00
|
|
|
|
|
|
|
|
function transformForward(node) {
|
2021-03-23 19:26:50 +01:00
|
|
|
if (node.name === 'svg') {
|
2020-10-19 06:26:39 -03:00
|
|
|
return {
|
|
|
|
|
...node,
|
|
|
|
|
attributes: {
|
|
|
|
|
...node.attributes,
|
|
|
|
|
width: 960,
|
2021-03-23 19:26:50 +01:00
|
|
|
height: 960,
|
|
|
|
|
},
|
2020-10-19 06:26:39 -03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function transformBackwards(node) {
|
2021-03-23 19:26:50 +01:00
|
|
|
if (node.name === 'svg') {
|
2020-10-19 06:26:39 -03:00
|
|
|
const { width, height, ...attributes } = node.attributes;
|
|
|
|
|
return {
|
|
|
|
|
...node,
|
2021-03-23 19:26:50 +01:00
|
|
|
attributes,
|
2020-10-19 06:26:39 -03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
2021-08-26 13:22:20 +02:00
|
|
|
|
|
|
|
|
async function init() {
|
|
|
|
|
console.time('icon outliner');
|
|
|
|
|
try {
|
|
|
|
|
const createDirectories = Object.keys(outputDirs).map(directory => fs.mkdir(`./${directory}`));
|
|
|
|
|
await Promise.all(createDirectories);
|
|
|
|
|
|
|
|
|
|
const icons = await fs.readdir(inputDir);
|
|
|
|
|
const parsedIconNodes = await Promise.all(
|
|
|
|
|
icons.map(async file => {
|
|
|
|
|
const iconContent = await fs.readFile(`${inputDir}${file}`);
|
|
|
|
|
const iconNode = await parse(iconContent.toString(), {
|
|
|
|
|
transformNode: transformForward,
|
|
|
|
|
});
|
|
|
|
|
return [file, iconNode];
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
|
Object.entries(outputDirs).map(async ([directory, width]) => {
|
|
|
|
|
await Promise.all(
|
|
|
|
|
parsedIconNodes.map(async ([file, iconNode]) => {
|
|
|
|
|
iconNode.attributes['stroke-width'] = width;
|
|
|
|
|
const outlined = await outlineStroke(stringify(iconNode));
|
|
|
|
|
const outlinedWithoutAttrs = await parse(outlined, {
|
|
|
|
|
transformNode: transformBackwards,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await fs.writeFile(`./${directory}/${file}`, stringify(outlinedWithoutAttrs));
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.timeEnd('icon outliner');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init();
|