compress 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env node
  2. // Compression helper for CodeMirror
  3. //
  4. // Example:
  5. //
  6. // bin/compress codemirror runmode javascript xml
  7. //
  8. // Will take lib/codemirror.js, addon/runmode/runmode.js,
  9. // mode/javascript/javascript.js, and mode/xml/xml.js, run them though
  10. // the online minifier at http://marijnhaverbeke.nl/uglifyjs, and spit
  11. // out the result.
  12. //
  13. // bin/compress codemirror --local /path/to/bin/UglifyJS
  14. //
  15. // Will use a local minifier instead of the online default one.
  16. //
  17. // Script files are specified without .js ending. Prefixing them with
  18. // their full (local) path is optional. So you may say lib/codemirror
  19. // or mode/xml/xml to be more precise. In fact, even the .js suffix
  20. // may be speficied, if wanted.
  21. "use strict";
  22. var fs = require("fs");
  23. function help(ok) {
  24. console.log("usage: " + process.argv[1] + " [--local /path/to/uglifyjs] files...");
  25. process.exit(ok ? 0 : 1);
  26. }
  27. var local = null, args = [], extraArgs = null, files = [], blob = "";
  28. for (var i = 2; i < process.argv.length; ++i) {
  29. var arg = process.argv[i];
  30. if (arg == "--local" && i + 1 < process.argv.length) {
  31. var parts = process.argv[++i].split(/\s+/);
  32. local = parts[0];
  33. extraArgs = parts.slice(1);
  34. if (!extraArgs.length) extraArgs = ["-c", "-m"];
  35. } else if (arg == "--help") {
  36. help(true);
  37. } else if (arg[0] != "-") {
  38. files.push({name: arg, re: new RegExp("(?:\\/|^)" + arg + (/\.js$/.test(arg) ? "$" : "\\.js$"))});
  39. } else help(false);
  40. }
  41. function walk(dir) {
  42. fs.readdirSync(dir).forEach(function(fname) {
  43. if (/^[_\.]/.test(fname)) return;
  44. var file = dir + fname;
  45. if (fs.statSync(file).isDirectory()) return walk(file + "/");
  46. if (files.some(function(spec, i) {
  47. var match = spec.re.test(file);
  48. if (match) files.splice(i, 1);
  49. return match;
  50. })) {
  51. if (local) args.push(file);
  52. else blob += fs.readFileSync(file, "utf8");
  53. }
  54. });
  55. }
  56. walk("lib/");
  57. walk("addon/");
  58. walk("mode/");
  59. if (!local && !blob) help(false);
  60. if (files.length) {
  61. console.log("Some speficied files were not found: " +
  62. files.map(function(a){return a.name;}).join(", "));
  63. process.exit(1);
  64. }
  65. if (local) {
  66. require("child_process").spawn(local, args.concat(extraArgs), {stdio: ["ignore", process.stdout, process.stderr]});
  67. } else {
  68. var data = new Buffer("js_code=" + require("querystring").escape(blob), "utf8");
  69. var req = require("http").request({
  70. host: "marijnhaverbeke.nl",
  71. port: 80,
  72. method: "POST",
  73. path: "/uglifyjs",
  74. headers: {"content-type": "application/x-www-form-urlencoded",
  75. "content-length": data.length}
  76. });
  77. req.on("response", function(resp) {
  78. resp.on("data", function (chunk) { process.stdout.write(chunk); });
  79. });
  80. req.end(data);
  81. }