2021-03-23 19:26:50 +01:00
|
|
|
const { promises: fs } = require('fs');
|
|
|
|
|
const outlineStroke = require('svg-outline-stroke');
|
|
|
|
|
const { parse, stringify } = require('svgson');
|
2020-10-19 06:26:39 -03:00
|
|
|
|
|
|
|
|
const inputDir = `./icons/`;
|
|
|
|
|
const outputDir = `./converted_icons/`;
|
|
|
|
|
|
|
|
|
|
async function init() {
|
|
|
|
|
try {
|
|
|
|
|
const files = await fs.readdir(inputDir);
|
2021-03-23 19:26:50 +01:00
|
|
|
for (const file of files) {
|
2020-10-19 06:26:39 -03:00
|
|
|
const icon = await fs.readFile(`${inputDir}${file}`);
|
|
|
|
|
const scaled = await parse(icon.toString(), {
|
2021-03-23 19:26:50 +01:00
|
|
|
transformNode: transformForward,
|
2020-10-19 06:26:39 -03:00
|
|
|
});
|
|
|
|
|
const outlined = await outlineStroke(stringify(scaled));
|
|
|
|
|
const outlinedWithoutAttrs = await parse(outlined, {
|
2021-03-23 19:26:50 +01:00
|
|
|
transformNode: transformBackwards,
|
2020-10-19 06:26:39 -03:00
|
|
|
});
|
2021-03-23 19:26:50 +01:00
|
|
|
await fs.writeFile(`${outputDir}${file}`, stringify(outlinedWithoutAttrs));
|
2020-10-19 06:26:39 -03:00
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|