Skip to content

Instantly share code, notes, and snippets.

@lucasbento
Last active January 13, 2017 11:04
Show Gist options
  • Save lucasbento/2908d54acf4ae527dd0be5941bf14fca to your computer and use it in GitHub Desktop.
Save lucasbento/2908d54acf4ae527dd0be5941bf14fca to your computer and use it in GitHub Desktop.
Small script to fix a mess with directories and put all the icons `.svg` files on a single directory
import fs from 'fs';
import path from 'path';
const readDir = path => fs.readdirSync(path);
const outputDir = '/Users/Lucas/Downloads/Font';
const rootPath = '/Users/Lucas/Downloads/Icons';
const files = readDir(rootPath);
const getDirectories = (dirFiles, srcPath) => dirFiles.filter(file => fs.statSync(path.join(srcPath, file)).isDirectory());
const getIconsFolders = () => {
const directories = getDirectories(files, rootPath);
directories.forEach((dir) => {
const dirPath = `${rootPath}/${dir}`;
let iconFolders = getDirectories(readDir(dirPath), dirPath);
// Skip if there is no SVG folder
if (iconFolders.indexOf('SVG') === -1) {
return null;
}
iconFolders = ['SVG'];
iconFolders.forEach((iconFolder) => {
const svgFolder = `${dirPath}/SVG`;
const svgFiles = readDir(svgFolder).filter(file => file.endsWith('-64.svg'));
svgFiles.forEach((svgFile) => {
const svgFilePath = `${svgFolder}/${svgFile}`;
const fileName = svgFile
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
.replace('-64svg', '.svg');
console.log(`Copying ${svgFile} to ${outputDir}/${fileName}`);
fs.createReadStream(svgFilePath).pipe(fs.createWriteStream(`${outputDir}/${fileName}`));
});
});
});
};
getIconsFolders();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment