node-loaders.mjs 828 B

123456789101112131415161718192021222324252627282930
  1. import { promises } from 'fs';
  2. import { camelize, pascalize, snakelize } from '../misc/strings.mjs';
  3. function FileSystemIconLoader(dir, transform) {
  4. return async (name) => {
  5. const paths = [
  6. `${dir}/${name}.svg`,
  7. `${dir}/${camelize(name)}.svg`,
  8. `${dir}/${pascalize(name)}.svg`,
  9. `${dir}/${snakelize(name)}.svg`
  10. ];
  11. let stat;
  12. for (const path of paths) {
  13. try {
  14. stat = await promises.lstat(path);
  15. } catch (err) {
  16. continue;
  17. }
  18. if (stat.isFile()) {
  19. let svg = await promises.readFile(path, "utf-8");
  20. const cleanupIdx = svg.indexOf("<svg");
  21. if (cleanupIdx > 0)
  22. svg = svg.slice(cleanupIdx);
  23. return typeof transform === "function" ? await transform(svg) : svg;
  24. }
  25. }
  26. };
  27. }
  28. export { FileSystemIconLoader };