Skip to content

Instantly share code, notes, and snippets.

@Hypnosphi
Last active July 24, 2020 23:32
Show Gist options
  • Save Hypnosphi/043d6f138c3dd2ad88472ea74f7bc12f to your computer and use it in GitHub Desktop.
Save Hypnosphi/043d6f138c3dd2ad88472ea74f7bc12f to your computer and use it in GitHub Desktop.
// __mocks__/icons.js
import path from 'path';
import fs from 'fs';
const svgRE = /\.svg$/
function getIcons(kind) {
const files = {};
function readDirectory(directory) {
fs.readdirSync(directory).forEach((file) => {
const fullPath = path.resolve(directory, file);
if (fs.statSync(fullPath).isDirectory()) {
readDirectory(fullPath);
return;
}
if (!svgRE.test(fullPath)) return;
const key = `${kind} ${file.replace(svgRE, '')}`;
files[key] = key;
});
}
return files;
}
export default {
Line: getIcons('Line'),
Solid: getIcons('Solid'),
};
// icons.js
const lineIcons = require.context('../../assets/icons/Line', true, /.+\.svg$/);
const solidIcons = require.context('../../assets/icons/Solid', true, /.+\.svg$/);
const requireAll = requireContext => requireContext.keys().map(requireContext);
const toObjectNames = (state, icon) => ({ ...state, [icon.default.id]: icon.default.id });
export default {
Line: requireAll(lineIcons).reduce(toObjectNames, {}),
Solid: requireAll(solidIcons).reduce(toObjectNames, {}),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment