bs-raw-files-generator.js 1.3 KB

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