You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate-svg.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env node
  2. 'use strict';
  3. const fastGlob = require('fast-glob');
  4. const Svgo = require('svgo');
  5. const {resolve, parse} = require('path');
  6. const {readFile, writeFile, mkdir} = require('fs').promises;
  7. const glob = (pattern) => fastGlob.sync(pattern, {cwd: resolve(__dirname), absolute: true});
  8. const outputDir = resolve(__dirname, '../public/img/svg');
  9. function exit(err) {
  10. if (err) console.error(err);
  11. process.exit(err ? 1 : 0);
  12. }
  13. async function processFile(file, {prefix = ''} = {}) {
  14. const name = `${prefix}${parse(file).name}`;
  15. const svgo = new Svgo({
  16. plugins: [
  17. {removeXMLNS: true},
  18. {removeDimensions: true},
  19. {
  20. addClassesToSVGElement: {
  21. classNames: [
  22. 'svg',
  23. name,
  24. ],
  25. },
  26. },
  27. {
  28. addAttributesToSVGElement: {
  29. attributes: [
  30. {'width': '16'},
  31. {'height': '16'},
  32. {'aria-hidden': 'true'},
  33. ],
  34. },
  35. },
  36. ],
  37. });
  38. const {data} = await svgo.optimize(await readFile(file, 'utf8'));
  39. await writeFile(resolve(outputDir, `${name}.svg`), data);
  40. }
  41. async function main() {
  42. try {
  43. await mkdir(outputDir);
  44. } catch {}
  45. for (const file of glob('../node_modules/@primer/octicons/build/svg/*.svg')) {
  46. await processFile(file, {prefix: 'octicon-'});
  47. }
  48. for (const file of glob('../web_src/svg/*.svg')) {
  49. await processFile(file);
  50. }
  51. }
  52. main().then(exit).catch(exit);