modules/cmd/prepareGZIP.js

  1. /**
  2. * Command line module.
  3. *
  4. * GZIP `staticFolder` files according to `usePreparedGzip` `headersPostprocessors` for specified application
  5. *
  6. * Usage:
  7. *
  8. * >ub.exe cmd/prepareGZIP -help
  9. *
  10. * For example during deploy UnityBase documentation to {@link https://unitybase.info/api/server/} we execute:
  11. *
  12. * > ub cmd/prepareGZIP -cfg pathToDocumentationConfig.json
  13. *
  14. * @author pavel.mash
  15. * @module cmd/prepareGZIP
  16. */
  17. "use strict";
  18. var
  19. fs = require('fs'),
  20. path = require('path'),
  21. cmdLineOpt = require('cmd/options'),
  22. /** @type cmd/argv */
  23. argv = require('./argv'),
  24. util = require('util'),
  25. gzipFile = require('UBCompressors').gzipFile;
  26. module.exports = function prepareGZIP(options) {
  27. var
  28. configFileName = argv.getConfigFileName(),
  29. configPath,
  30. config, inetPub;
  31. if (!options){
  32. var opts = cmdLineOpt.describe('cmd/prepareGZIP', 'GZIP `staticFolder` files according to `usePreparedGzip` `staticRules` for specified application')
  33. .add({short: 'cfg', long: 'cfg', param: 'localServerConfig', defaultValue: 'ubConfig.json', searchInEnv: false, help: 'Path to server config'})
  34. .add({short: 'd', long: 'deleteOriginals', defaultValue: false, help: 'Delete source files we compress(not recommended if you have a client what not support compression'})
  35. .add({short: 'largeWhen', long: 'largeWhen', param: 'size', defaultValue: 3*1024, help: 'ompress only files with size > largeWhen bytes'});
  36. options = opts.parseVerbose({}, true);
  37. if (!options) return;
  38. }
  39. var deleteOriginals = options.deleteOriginals ;
  40. var largeWhen = options.largeWhen;
  41. config = argv.getServerConfiguration();
  42. if (!config.httpServer || ! config.httpServer.inetPub) {
  43. console.warn('config.httpServer.inetPub not configured');
  44. return;
  45. }
  46. inetPub = config.httpServer.inetPub;
  47. var headersPostprocessors = config.httpServer.headersPostprocessors;
  48. var gzipRules = _.filter(headersPostprocessors, {usePreparedGzip: true});
  49. if (!gzipRules.length){
  50. console.warn('header post-processors with `usePreparedGzip: true` option not found');
  51. return;
  52. }
  53. // create regular expressions file name must be test with
  54. gzipRules.forEach(function(rule){
  55. console.log('Apply rule:', rule.location, 'for files large when', largeWhen, 'bytes');
  56. rule.re = new RegExp(rule.location);
  57. });
  58. configPath = path.dirname(configFileName);
  59. var startFrom = relToAbs(configPath, inetPub);
  60. var endWith = startFrom.charAt(startFrom.length-1);
  61. if (( endWith !== '\\') && (endWith !== '/')) {
  62. startFrom += '/';
  63. }
  64. if (!fs.isDir(startFrom)){
  65. console.warn('inetPub root "%s" dose not exists', startFrom);
  66. return;
  67. } else {
  68. console.debug('Start scanning folder', startFrom );
  69. }
  70. var totalSize = 0, totalGZSize = 0, totalCount = 0, totalGZCount = 0;
  71. function gzipFolder(startFromPath){
  72. var
  73. fullFileName, folderContent, sz;
  74. folderContent = fs.readdirSync(startFromPath);
  75. folderContent.forEach(function(fileName){
  76. fullFileName = startFromPath + fileName;
  77. if (fs.isDir(fullFileName)) {
  78. gzipFolder(fullFileName + '\\');
  79. } else {
  80. ++totalCount; util.print('.');
  81. //console.debug('Try', fullFileName);
  82. if (_.some(gzipRules, function(rule){ return rule.re.test(fullFileName) }) ){ // some of usePreparedGzip rule is applied to this file
  83. sz = fs.statSync(fullFileName).size; totalSize += sz;
  84. if (sz > largeWhen){ // more when 3kb
  85. gzipFile(fullFileName, fullFileName + '.gz');
  86. totalGZSize += fs.statSync(fullFileName + '.gz').size;
  87. ++totalGZCount;
  88. if (deleteOriginals){
  89. debugger;
  90. fs.unlinkSync(fullFileName);
  91. }
  92. }
  93. }
  94. }
  95. });
  96. }
  97. gzipFolder(startFrom);
  98. util.print('\n');
  99. console.log('Compress %d of %d files from %d to %d bytes (%d bytes traffic savings)', totalGZCount, totalCount, totalSize, totalGZSize, totalGZSize ? totalGZSize - totalSize : 0);
  100. };