Aucune description

cli.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/env node
  2. /**
  3. * html-minifier CLI tool
  4. *
  5. * The MIT License (MIT)
  6. *
  7. * Copyright (c) 2014 Zoltan Frombach
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  10. * this software and associated documentation files (the "Software"), to deal in
  11. * the Software without restriction, including without limitation the rights to
  12. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  13. * the Software, and to permit persons to whom the Software is furnished to do so,
  14. * subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  21. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  22. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  23. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. */
  27. 'use strict';
  28. var cli = require('cli');
  29. var changeCase = require('change-case');
  30. var path = require('path');
  31. var fs = require('fs');
  32. var appName = require('./package.json').name;
  33. var appVersion = require('./package.json').version;
  34. var minify = require('./dist/htmlminifier.min.js').minify;
  35. var minifyOptions = {};
  36. var input = null;
  37. var output = null;
  38. cli.width = 100;
  39. cli.option_width = 40;
  40. cli.setApp(appName, appVersion);
  41. var usage = appName + ' [OPTIONS] [FILE(s)]\n\n';
  42. usage += ' If no input file(s) specified then STDIN will be used for input.\n';
  43. usage += ' If more than one input file specified those will be concatenated and minified together.\n\n';
  44. usage += ' When you specify a config file with the --config-file option (see sample-cli-config-file.conf for format)\n';
  45. usage += ' you can still override some of its contents by providing individual command line options, too.\n\n';
  46. usage += ' When you want to provide an array of strings for --ignore-custom-comments or --process-scripts options\n';
  47. usage += ' on the command line you must escape those such as --ignore-custom-comments "[\\"string1\\",\\"string1\\"]"\n';
  48. cli.setUsage(usage);
  49. var mainOptions = {
  50. removeComments: [[false, 'Strip HTML comments']],
  51. removeCommentsFromCDATA: [[false, 'Strip HTML comments from scripts and styles']],
  52. removeCDATASectionsFromCDATA: [[false, 'Remove CDATA sections from script and style elements']],
  53. collapseWhitespace: [[false, 'Collapse white space that contributes to text nodes in a document tree.']],
  54. conservativeCollapse: [[false, 'Always collapse to 1 space (never remove it entirely)']],
  55. preserveLineBreaks: [[false, 'Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break.']],
  56. collapseBooleanAttributes: [[false, 'Omit attribute values from boolean attributes']],
  57. removeAttributeQuotes: [[false, 'Remove quotes around attributes when possible.']],
  58. removeRedundantAttributes: [[false, 'Remove attributes when value matches default.']],
  59. useShortDoctype: [[false, 'Replaces the doctype with the short (HTML5) doctype']],
  60. removeEmptyAttributes: [[false, 'Remove all attributes with whitespace-only values']],
  61. removeOptionalTags: [[false, 'Remove unrequired tags']],
  62. removeEmptyElements: [[false, 'Remove all elements with empty contents']],
  63. lint: [[false, 'Toggle linting']],
  64. keepClosingSlash: [[false, 'Keep the trailing slash on singleton elements']],
  65. caseSensitive: [[false, 'Treat attributes in case sensitive manner (useful for SVG; e.g. viewBox)']],
  66. minifyJS: [[false, 'Minify Javascript in script elements and on* attributes (uses UglifyJS)']],
  67. minifyCSS: [[false, 'Minify CSS in style elements and style attributes (uses clean-css)']],
  68. minifyURLs: [[false, 'Minify URLs in various attributes (uses relateurl)']],
  69. ignoreCustomComments: [[false, 'Array of regex\'es that allow to ignore certain comments, when matched', 'string'], 'json'],
  70. processScripts: [[false, 'Array of strings corresponding to types of script elements to process through minifier (e.g. "text/ng-template", "text/x-handlebars-template", etc.)', 'string'], 'json'],
  71. maxLineLength: [[false, 'Max line length', 'number'], true]
  72. };
  73. var cliOptions = {
  74. version: ['v', 'Version information'],
  75. output: ['o', 'Specify output file (if not specified STDOUT will be used for output)', 'file'],
  76. 'config-file': ['c', 'Use config file', 'file']
  77. };
  78. var mainOptionKeys = Object.keys(mainOptions);
  79. mainOptionKeys.forEach(function(key) {
  80. cliOptions[changeCase.paramCase(key)] = mainOptions[key][0];
  81. });
  82. cli.parse(cliOptions);
  83. cli.main(function(args, options) {
  84. if (options.version) {
  85. process.stderr.write(appName + ' v' + appVersion);
  86. cli.exit(0);
  87. }
  88. if (options['config-file']) {
  89. try {
  90. var fileOptions = JSON.parse(fs.readFileSync(path.resolve(options['config-file']), 'utf8'));
  91. if ((fileOptions !== null) && (typeof fileOptions === 'object')) {
  92. minifyOptions = fileOptions;
  93. }
  94. }
  95. catch (e) {
  96. process.stderr.write('Error: Cannot read the specified config file');
  97. cli.exit(1);
  98. }
  99. }
  100. mainOptionKeys.forEach(function(key) {
  101. var paramKey = changeCase.paramCase(key);
  102. var value = options[paramKey];
  103. if (options[paramKey] !== null) {
  104. switch (mainOptions[key][1]) {
  105. case 'json':
  106. minifyOptions[key] = parseJSONOption(value);
  107. break;
  108. case true:
  109. minifyOptions[key] = value;
  110. break;
  111. default:
  112. minifyOptions[key] = true;
  113. }
  114. }
  115. });
  116. if (args.length) {
  117. input = args;
  118. }
  119. if (options.output) {
  120. output = options.output;
  121. }
  122. var original = '';
  123. var status = 0;
  124. if (input !== null) { // Minifying one or more files specified on the CMD line
  125. input.forEach(function(afile) {
  126. try {
  127. original += fs.readFileSync(afile, 'utf8');
  128. }
  129. catch (e) {
  130. status = 2;
  131. process.stderr.write('Error: Cannot read file ' + afile);
  132. }
  133. });
  134. }
  135. else { // Minifying input coming from STDIN
  136. var BUFSIZE = 4096;
  137. var buf = new Buffer(BUFSIZE);
  138. var bytesRead;
  139. while (true) { // Loop as long as stdin input is available.
  140. bytesRead = 0;
  141. try {
  142. bytesRead = fs.readSync(process.stdin.fd, buf, 0, BUFSIZE);
  143. }
  144. catch (e) {
  145. if (e.code === 'EAGAIN') { // 'resource temporarily unavailable'
  146. // Happens on OS X 10.8.3 (not Windows 7!), if there's no
  147. // stdin input - typically when invoking a script without any
  148. // input (for interactive stdin input).
  149. // If you were to just continue, you'd create a tight loop.
  150. process.stderr.write('ERROR: interactive stdin input not supported');
  151. cli.exit(2);
  152. }
  153. else if (e.code === 'EOF') {
  154. // Happens on Windows 7, but not OS X 10.8.3:
  155. // simply signals the end of *piped* stdin input.
  156. break;
  157. }
  158. throw e; // unexpected exception
  159. }
  160. if (bytesRead === 0) {
  161. // No more stdin input available.
  162. // OS X 10.8.3: regardless of input method, this is how the end
  163. // of input is signaled.
  164. // Windows 7: this is how the end of input is signaled for
  165. // *interactive* stdin input.
  166. break;
  167. }
  168. original += buf.toString('utf8', 0, bytesRead);
  169. }
  170. }
  171. function parseJSONOption(value) {
  172. if (value !== null) {
  173. var jsonArray;
  174. try {
  175. jsonArray = JSON.parse(value);
  176. }
  177. catch (e) {}
  178. if (jsonArray instanceof Array) {
  179. return jsonArray;
  180. }
  181. else {
  182. return [value];
  183. }
  184. }
  185. }
  186. // Run minify
  187. var minified = null;
  188. try {
  189. minified = minify(original, minifyOptions);
  190. }
  191. catch (e) {
  192. status = 3;
  193. process.stderr.write('Error: Minification error');
  194. }
  195. if (minified !== null) {
  196. // Write the output
  197. try {
  198. if (output !== null) {
  199. fs.writeFileSync(path.resolve(output), minified);
  200. }
  201. else {
  202. process.stdout.write(minified);
  203. }
  204. }
  205. catch (e) {
  206. status = 4;
  207. process.stderr.write('Error: Cannot write to output');
  208. }
  209. }
  210. cli.exit(status);
  211. });