Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit of trivial wrapper script

  • Loading branch information...
commit e64671951bb3a88a5b6c4d168084f68111919c07 0 parents
@runpaint authored
Showing with 65 additions and 0 deletions.
  1. +15 −0 README.md
  2. +50 −0 js-compile
15 README.md
@@ -0,0 +1,15 @@
+Compiles one or more JavaScript files/fragments with the [Google Closure
+Compiler](http://code.google.com/closure/compiler/docs/overview.html).
+
+The default compilation level is _WHITESPACE\_ONLY_. Another level can be
+specified with the `--level` option. _SIMPLE\_OPTIMIZATIONS_ can be
+abbreviated as _simple_, _ADVANCED\_OPTIMIZATIONS_ as _advanced_, and
+_WHITESPACE\_ONLY_ as _whitespace_.
+
+The JavaScript to compress can be supplied on STDIN, or contained in files
+named on the command line. The compressed JavaScript is printed to STDOUT.
+
+For example, `js-compile some.js` performs whitespace-only compression on the
+file named _some.js_. `js-compile --level=advanced <some.js >compressed.js`
+performs advanced compression on the files it receives via STDIN, then writes
+then concatenated result to _compressed.js_.
50 js-compile
@@ -0,0 +1,50 @@
+#!/usr/bin/env ruby
+require 'httparty'
+require 'getoptlong'
+
+class Closure
+ include HTTParty
+ base_uri 'http://closure-compiler.appspot.com'
+ attr_reader :code
+ LEVELS = { whitespace: :WHITESPACE_ONLY, simple: :SIMPLE_OPTIMIZATIONS,
+ advanced: :ADVANCED_OPTIMIZATIONS }
+
+ def code=(js)
+ raise ArgumentError unless @code = String.try_convert(js)
+ end
+
+ def level=(level)
+ @level = String.try_convert(level).to_sym
+ @level = LEVELS[@level] if LEVELS.key?(@level)
+ raise ArgumentError unless LEVELS.values.include?(@level.to_sym)
+ end
+
+ def level
+ @level || LEVELS[:whitespace]
+ end
+
+ def compiled_code
+ raise errors if errors?
+ compile['compiledCode']
+ end
+
+ def errors?
+ compile.key?('serverErrors')
+ end
+
+ def errors
+ compile['serverErrors'].map{|e| e['error']}.join("\n")
+ end
+
+ def compile
+ @compile ||= self.class.post('/compile', body: { output_format: :json,
+ js_code: code, compilation_level: level, output_info: :compiled_code})
+ end
+end
+
+compiler = Closure.new
+GetoptLong.new([ "--level", "-l", GetoptLong::REQUIRED_ARGUMENT ]).each do |opt, arg|
+ compiler.send((opt[2..-1] + '=').to_sym, arg)
+end
+compiler.code= ARGF.read
+puts compiler.compiled_code
Please sign in to comment.
Something went wrong with that request. Please try again.