3 // Compression helper for CodeMirror
7 // bin/compress codemirror runmode javascript xml
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
14 // bin/compress codemirror --local /path/to/bin/UglifyJS
16 // Will use a local minifier instead of the online default one.
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.
25 var fs = require("fs");
28 console.log("usage: " + process.argv[1] + " [--local /path/to/uglifyjs] files...");
29 process.exit(ok ? 0 : 1);
32 var local = null, args = [], extraArgs = null, files = [], blob = "";
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+/);
39 extraArgs = parts.slice(1);
40 if (!extraArgs.length) extraArgs = ["-c", "-m"];
41 } else if (arg == "--help") {
43 } else if (arg[0] != "-") {
44 files.push({name: arg, re: new RegExp("(?:\\/|^)" + arg + (/\.js$/.test(arg) ? "$" : "\\.js$"))});
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);
58 if (local) args.push(file);
59 else blob += fs.readFileSync(file, "utf8");
68 if (!local && !blob) help(false);
71 console.log("Some speficied files were not found: " +
72 files.map(function(a){return a.name;}).join(", "));
77 require("child_process").spawn(local, args.concat(extraArgs), {stdio: ["ignore", process.stdout, process.stderr]});
79 var data = new Buffer("js_code=" + require("querystring").escape(blob), "utf8");
80 var req = require("http").request({
81 host: "marijnhaverbeke.nl",
85 headers: {"content-type": "application/x-www-form-urlencoded",
86 "content-length": data.length}
88 req.on("response", function(resp) {
89 resp.on("data", function (chunk) { process.stdout.write(chunk); });