Last active
December 15, 2018 23:35
-
-
Save GiuMagnani/12d44d910f29c5f94f5c90d0f4c1ff94 to your computer and use it in GitHub Desktop.
Node script to take the data from SVG paths ("d", "filename" and "viewBox") and print them as a keyed object for Icon Components.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const testFolder = __dirname + '/svgs'; | |
const fs = require('fs'); | |
fs.readdir(testFolder, (err, files) => { | |
const filePromises = []; | |
files.forEach((file, index) => { | |
let concatenatedPath = ""; | |
if (file.indexOf('svg') === -1) return; | |
filePromises.push(new Promise((resolve, reject) => { | |
fs.readFile(__dirname + '/svgs/' + file, 'utf8', (err, data) => { | |
if (err) throw err; | |
const matches = data.match(/ d="((?:\\.|[^"\\])*)"/g); | |
const name = file.split('.svg')[0]; | |
let viewBox = data.match(/viewBox="((?:\\.|[^"\\])*)"/g); | |
viewBox = viewBox[0].replace('viewBox="', "").replace('"', ''); | |
concatenatedPath = ""; | |
matches.forEach((string, matchIndex) => { | |
string = string.replace(' d="', matchIndex === 0 ? "" : " "); | |
string = string.replace(/['"]+/g, ""); | |
concatenatedPath = concatenatedPath + string; | |
return; | |
}); | |
const result = { | |
name: name, | |
path: concatenatedPath, | |
viewBox, | |
}; | |
resolve(result); | |
}); | |
})) | |
}); | |
Promise.all(filePromises).then((res) => { | |
let icons = {}; | |
res.map((icon) => { | |
return icons = { | |
...icons, | |
[icon.name]: icon, | |
} | |
}); | |
fs.writeFile(__dirname + '/svgs.txt', JSON.stringify(icons), function(err) { | |
if(err) { | |
return console.log(err); | |
} | |
console.log("The file was saved!"); | |
}); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment