bs-raw-files-generator.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*!
  2. * Bootstrap Grunt task for generating raw-files.min.js for the Customizer
  3. * https://getbootstrap.com/
  4. * Copyright 2014-2019 Twitter, Inc.
  5. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  6. */
  7. 'use strict';
  8. var fs = require('fs');
  9. var btoa = require('btoa');
  10. var glob = require('glob');
  11. function getFiles(type) {
  12. var files = {};
  13. var recursive = type === 'less';
  14. var globExpr = recursive ? '/**/*' : '/*';
  15. glob.sync(type + globExpr)
  16. .filter(function (path) {
  17. return type === 'fonts' ? true : new RegExp('\\.' + type + '$').test(path);
  18. })
  19. .forEach(function (fullPath) {
  20. var relativePath = fullPath.replace(/^[^/]+\//, '');
  21. files[relativePath] = type === 'fonts' ? btoa(fs.readFileSync(fullPath)) : fs.readFileSync(fullPath, 'utf8');
  22. });
  23. return 'var __' + type + ' = ' + JSON.stringify(files) + '\n';
  24. }
  25. module.exports = function generateRawFilesJs(grunt, banner) {
  26. if (!banner) {
  27. banner = '';
  28. }
  29. var dirs = ['js', 'less', 'fonts'];
  30. var files = banner + dirs.map(getFiles).reduce(function (combined, file) {
  31. return combined + file;
  32. }, '');
  33. var rawFilesJs = 'docs/assets/js/raw-files.min.js';
  34. try {
  35. fs.writeFileSync(rawFilesJs, files);
  36. } catch (err) {
  37. grunt.fail.warn(err);
  38. }
  39. grunt.log.writeln('File ' + rawFilesJs.cyan + ' created.');
  40. };