grunt.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const fs = require('fs');
  2. const path = require('path');
  3. const sass = require('node-sass');
  4. let iconsIndex = [];
  5. // Merge a `source` object to a `target` recursively
  6. const merge = (target, source) => {
  7. // Check if font name is changed
  8. if (source['font-name']) {
  9. target['font-name'] = source['font-name'];
  10. }
  11. // Check if root dir is changed
  12. if (source['root-dir']) {
  13. target['root-dir'] = source['root-dir'];
  14. }
  15. // Check for icon changes
  16. if (source.icons) {
  17. for (let icon of source['icons']) {
  18. let index = iconsIndex.indexOf(icon.name);
  19. // Icon is replaced
  20. if (index !== -1) {
  21. target.icons[index] = icon;
  22. }
  23. // New icon is added
  24. else {
  25. target.icons.push(icon);
  26. iconsIndex.push(icon.name);
  27. }
  28. }
  29. }
  30. return target;
  31. }
  32. module.exports = function(grunt) {
  33. grunt.initConfig({
  34. sass: {
  35. options: {
  36. implementation: sass
  37. },
  38. dist: {
  39. files: {
  40. 'css/videojs-icons.css': 'scss/videojs-icons.scss'
  41. }
  42. }
  43. },
  44. watch: {
  45. all: {
  46. files: ['**/*.hbs', '**/*.js', './icons.json'],
  47. tasks: ['default']
  48. }
  49. }
  50. });
  51. grunt.registerTask('generate-font', function() {
  52. const done = this.async();
  53. let webfontsGenerator = require('webfonts-generator');
  54. let iconConfig = grunt.file.readJSON(path.join(__dirname, '..', 'icons.json'));
  55. let svgRootDir = iconConfig['root-dir'];
  56. if (grunt.option('exclude-default')) {
  57. // Exclude default video.js icons
  58. iconConfig.icons = [];
  59. }
  60. let icons = iconConfig.icons;
  61. // Index default icons
  62. icons.forEach(icon => iconsIndex.push(icon.name));
  63. // Merge custom icons
  64. const customPaths = (grunt.option('custom-json') || '').split(',').filter(Boolean);
  65. customPaths.forEach(customPath => {
  66. const customConfig = grunt.file.readJSON(path.resolve(process.cwd(), customPath));
  67. iconConfig = merge(iconConfig, customConfig);
  68. });
  69. icons = iconConfig.icons;
  70. let iconFiles = icons.map(icon => {
  71. // If root-dir is specified for a specific icon, use that.
  72. if (icon['root-dir']) {
  73. return icon['root-dir'] + icon.svg;
  74. }
  75. // Otherwise, use the default root-dir.
  76. return svgRootDir + icon.svg;
  77. });
  78. webfontsGenerator({
  79. files: iconFiles,
  80. dest: 'fonts/',
  81. fontName: iconConfig['font-name'],
  82. cssDest: 'scss/_icons.scss',
  83. cssTemplate: './templates/scss.hbs',
  84. htmlDest: 'index.html',
  85. htmlTemplate: './templates/html.hbs',
  86. html: true,
  87. rename: iconPath => {
  88. const fileName = path.basename(iconPath);
  89. const iconName = icons.find(icon => path.basename(icon.svg) === fileName).name;
  90. return iconName;
  91. },
  92. types: ['svg', 'woff', 'ttf']
  93. }, error => {
  94. if (error) {
  95. console.error(error);
  96. done(false);
  97. }
  98. done();
  99. });
  100. });
  101. grunt.registerTask('update-base64', function() {
  102. const iconScssFile = './scss/_icons.scss';
  103. const iconConfig = grunt.option('custom-json') ?
  104. grunt.file.readJSON(path.resolve(process.cwd(), grunt.option('custom-json'))) :
  105. grunt.file.readJSON(path.join(__dirname, '..', 'icons.json'));
  106. const fontName = iconConfig['font-name'];
  107. const fontFiles = {
  108. woff: './fonts/' + fontName + '.woff'
  109. };
  110. let scssContents = fs.readFileSync(iconScssFile).toString();
  111. Object.keys(fontFiles).forEach(font => {
  112. const fontFile = fontFiles[font];
  113. const fontContent = fs.readFileSync(fontFile);
  114. const regex = new RegExp(`(url.*font-${font}.*base64,)([^\\s]+)(\\).*)`);
  115. scssContents = scssContents.replace(regex, `$1${fontContent.toString('base64')}$3`);
  116. });
  117. fs.writeFileSync(iconScssFile, scssContents);
  118. });
  119. grunt.registerTask('default', ['generate-font', 'update-base64', 'sass']);
  120. };