Skip to content

Instantly share code, notes, and snippets.

@Davi-zzz
Last active April 2, 2026 22:51
Show Gist options
  • Select an option

  • Save Davi-zzz/82c1748d42d83e475468908f42610af9 to your computer and use it in GitHub Desktop.

Select an option

Save Davi-zzz/82c1748d42d83e475468908f42610af9 to your computer and use it in GitHub Desktop.
javascript to export elements as default consts
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const dir = path.dirname(fileURLToPath(import.meta.url));
const imagesDir = path.join(dir, 'src', 'images');
const files = fs.readdirSync(imagesDir);
const list = new Map();
const toBeInserted = [];
files.map(e => {
const treated = e
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-zA-Z0-9. ]/g, '')
.toLowerCase();
let item = treated;
const [name, ext] = item.split('.');
const [head, ...tail] = ext;
item = name + head.toUpperCase() + tail.join('');
list.set(item, e);
});
for(const entries of list.entries()) {
const [key, value] = entries;
console.log(key, value);
const base = (varname, path) => {
return `export { default as ${varname} } from "./${path}";`
}
toBeInserted.push(base(key, value));
}
fs.writeFileSync(
path.join(imagesDir, 'index.js'),
toBeInserted.join('\n')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment