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