Skip to content

Instantly share code, notes, and snippets.

@dodozhang21
Created October 2, 2018 18:20
Show Gist options
  • Save dodozhang21/cc20e0b2fe5b8e2d4f9e150e5d0743af to your computer and use it in GitHub Desktop.
Save dodozhang21/cc20e0b2fe5b8e2d4f9e150e5d0743af to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const iconDirectory = path.join(__dirname, '/shared/partials/icons/');
const singleFile = path.join(__dirname, '/shared/partials/icons/36px/pinterest-gallery-overlay.hbs');
function walkSync(dir, filelist) {
const files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function (file) {
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist);
}
else {
filelist.push(dir + file);
}
});
return filelist;
}
function updateIconFile(file) {
// Return new promise
return new Promise((resolve, reject) => {
// Do async job
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
reject(err);
}
// Update the content.
const matches = data.match(/<svg[^>]+>/);
if (matches) {
const svgTag = matches[0];
const startTag = svgTag.slice(0, -1);
const newSvgTag = `${startTag} role="img" {{#if aria-label}}aria-label="{{aria-label}}"{{else}}aria-hidden="true"{{/if}} tabindex="-1">`;
const content = data.replace(svgTag, newSvgTag);
fs.writeFile(file, content, (err) => {
if (err) {
resolve({
filename: file,
status: false,
content: err,
});
}
else {
resolve({
filename: file,
status: true,
content,
});
}
});
}
else {
resolve({
filename: file,
status: false,
content: 'No svg found.',
});
}
});
});
}
const fileList = [];
walkSync(iconDirectory, fileList);
const readList = [];
fileList.forEach((file) => {
readList.push(updateIconFile(file));
});
Promise.all(readList).then((values) => {
values.forEach((value) => {
if (value.status) {
// write to file
}
else {
console.log(value);
}
});
});
// Promise.all([updateIconFile(singleFile)]).then((values) => {
// console.log(values);
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment