Guest User

Untitled

a guest
Jun 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. const fs = require('fs');
  2. const path = require('path');
  3. const fx = require('mkdir-recursive');
  4.  
  5. const iconFolder = './src/content/app/fx/icons/'
  6. const svgFolder = `${iconFolder}svg/`;
  7. const shortSvgFoler = '/fx/icons/svg/'
  8.  
  9. /**
  10. * source: Folder where the icon are
  11. * cssfilename: Filename of the css file
  12. * cssprefix: Prefix to add on css class
  13. */
  14. const ICONS = [
  15. {
  16. source: './src/content/app/icons/',
  17. cssfilename: '_icons-mobile.scss',
  18. cssprefix: '.icon-mobile-',
  19. },
  20. ];
  21.  
  22. // Create the svg folder if it doesn't exist yet
  23. if (!fs.exists(svgFolder)) {
  24. fx.mkdir(svgFolder, error => {
  25. if (error) {
  26. return console.log('error', error);
  27. }
  28.  
  29. console.log('SVG folder ahs been created in: ', svgFolder);
  30. addIcons();
  31. });
  32. } else {
  33. addIcons
  34. }
  35.  
  36. function addIcons() {
  37. try {
  38. ICONS.forEach(icon => {
  39. let backgroundSVG = '';
  40. let numberOfIcons = 0;
  41.  
  42. fs.readdirSync(icon.source).forEach(file => {
  43. copyFile(icon.source, svgFolder, file);
  44.  
  45. backgroundSVG += createCssAttr(shortSvgFoler, file, icon.cssprefix);
  46.  
  47. numberOfIcons++;
  48. });
  49.  
  50. fs.writeFile(iconFolder + icon.cssfilename, backgroundSVG, error => {
  51. if (error) {
  52. return console.log('Error 3: ', error);
  53. }
  54. console.log(`${numberOfIcons} icons have been added!`);
  55. });
  56. });
  57. } catch (error) {
  58. console.log('Error 1: ', error);
  59. }
  60.  
  61. }
  62.  
  63. /**
  64. * Add CSS attribute into the class
  65. * @param {String} source
  66. * @param {String} filename
  67. * @param {String} prefix
  68. */
  69. function createCssAttr(dest, filename, prefix) {
  70. const filenameWithoutExtension = path.basename(filename, '.svg');
  71. return `${prefix}${filenameWithoutExtension} {
  72. background-image: url('${dest + filename}');
  73. background-repeat: no-repeat;
  74. }\n`;
  75. }
  76.  
  77. /**
  78. * Copy icons into a new folder in destination path
  79. * @param {String} source
  80. * @param {String} dest
  81. * @param {String} file
  82. */
  83. function copyFile(source, dest, file) {
  84. try {
  85. fs.createReadStream(source + file).pipe(fs.createWriteStream(dest + file));
  86. } catch (error) {
  87. console.log('Error 2: ', error);
  88. }
  89. }
Add Comment
Please, Sign In to add comment