[Pkg-javascript-commits] [sockjs-client] 13/434: More powerful generating script.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:46:57 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository sockjs-client.

commit f58e6fd53ca7ab540908858c9e7bfdcfb7dbf9d7
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Sun Jul 24 17:30:02 2011 +0100

    More powerful generating script.
---
 bin/minify.coffee            | 29 ++++++++++++++
 bin/minify.js                | 31 ---------------
 bin/render.coffee            | 90 ++++++++++++++++++++++++++++++++++++++++++++
 bin/render.js                | 55 ---------------------------
 gen.sh                       |  6 +++
 lib/all.js                   |  4 ++
 {static => lib}/json2.min.js |  0
 run.sh                       |  6 +--
 8 files changed, 131 insertions(+), 90 deletions(-)

diff --git a/bin/minify.coffee b/bin/minify.coffee
new file mode 100644
index 0000000..e5b1b1f
--- /dev/null
+++ b/bin/minify.coffee
@@ -0,0 +1,29 @@
+#!/usr/bin/env coffee
+
+fs = require('fs')
+uglify = require('uglify-js')
+optparse = require('optparse')
+
+switches = [
+    ['-p', '--pretty', 'Prettify javascript']
+]
+
+minify_options = {}
+parser = new optparse.OptionParser(switches)
+parser.on 'pretty', ->
+                   minify_options.beautify = true
+
+filenames = parser.parse(process.ARGV.slice(2))
+
+content = for filename in filenames
+            fs.readFileSync(filename, encoding='utf8')
+
+console.warn(" [.] Minifying:", filenames.join(', '))
+
+minify = (data, minify_options)->
+    ast = uglify.parser.parse(data)
+    ast = uglify.uglify.ast_mangle(ast)
+    ast = uglify.uglify.ast_squeeze(ast)
+    uglify.uglify.gen_code(ast, minify_options)
+
+process.stdout.write(minify(content.join('\n')))
diff --git a/bin/minify.js b/bin/minify.js
deleted file mode 100644
index c18c8e2..0000000
--- a/bin/minify.js
+++ /dev/null
@@ -1,31 +0,0 @@
-var fs = require('fs');
-var uglify = require('uglify-js');
-var optparse = require('optparse');
-
-var switches = [
-    ['-p', '--pretty', 'Prettify javascript']
-];
-
-(function(){
-     var gen_options = {};
-     var parser = new optparse.OptionParser(switches);
-     parser.on('pretty', function() {
-                   gen_options.beautify = true;
-               });
-     var filenames = parser.parse(process.ARGV.slice(2));
-
-     var content = [];
-     for(var i = 0; i < filenames.length; i++) {
-         var data = fs.readFileSync(filenames[i], encoding='utf8');
-         content.push( data );
-     }
-     console.warn(" [.] Minifying:", filenames.join(', '));
-
-     var data = content.join('\n');
-     var ast = uglify.parser.parse(data);
-     ast = uglify.uglify.ast_mangle(ast);
-     ast = uglify.uglify.ast_squeeze(ast);
-     var minified = uglify.uglify.gen_code(ast, gen_options);
-
-     process.stdout.write(minified);
- })();
diff --git a/bin/render.coffee b/bin/render.coffee
new file mode 100644
index 0000000..24fdf47
--- /dev/null
+++ b/bin/render.coffee
@@ -0,0 +1,90 @@
+#!/usr/bin/env coffee
+
+fs = require('fs')
+uglify = require('uglify-js')
+optparse = require('optparse')
+
+array_flatten = (arr, acc) ->
+    if typeof acc is 'undefined'
+        acc = []
+    if typeof arr is 'string'
+        acc.push(arr)
+        return acc
+
+    if arr.constructor isnt Array
+        throw "Value is not an Array nor a String!"
+
+    for v in arr
+        if typeof v is 'string'
+            acc.push(v)
+        else if v.constructor is Array
+            array_flatten(v, acc)
+        else
+            throw "Value is not an Array nor a String!"
+    return acc
+
+
+minify = (data, minify_options)->
+    ast = uglify.parser.parse(data)
+    ast = uglify.uglify.ast_mangle(ast)
+    ast = uglify.uglify.ast_squeeze(ast)
+    uglify.uglify.gen_code(ast, minify_options)
+
+render = (filename, depth, options) ->
+    tags =
+        include: (args) ->
+            if args.length > 1 and args[1].indexOf('c') isnt -1
+                options.comment = true
+            render(args[0], depth + '    ', options)
+        version: ->
+            "0.0.1"
+        include_and_minify: (args) ->
+            if args.length > 1 and args[1].indexOf('c') isnt -1
+                options.comment = true
+            d = render(args[0], depth + '    ', options)
+            if options.minify
+                return minify(array_flatten(d).join(''), options)
+            return d
+
+    console.warn(depth + " [.] Rendering", filename)
+
+    # no trailing whitespace
+    data = fs.readFileSync(filename, encoding='utf8').replace(/\s+$/, '')
+
+    content = []
+    if options.comment
+        content.push('\n// ' + depth + '[*] Including ' + filename + '\n')
+
+    elements = data.split(/<!--\s*([^>]+)\s*-->/g)
+    for i in [0...elements.length]
+        e = elements[i];
+        if i % 2 is 0
+            content.push(e)
+        else
+            p = e.split(' ')
+            content.push( tags[p[0]](p.slice(1)) )
+
+    if options.comment
+        content.push('\n// ' + depth + '[*] End of ' + filename + '\n')
+    return content
+
+
+main = ->
+    switches = [
+        ['-p', '--pretty', 'Prettify javascript']
+        ['-m', '--minify', 'Minify javascript']
+    ]
+    options = {minify:false}
+    parser = new optparse.OptionParser(switches)
+    parser.on 'pretty', ->
+                   options.beautify = true
+    parser.on 'minify', ->
+                   options.minify = true
+    filenames = parser.parse(process.ARGV.slice(2))
+
+    content = for filename in filenames
+        render(filename, '', options)
+    content.push('\n')
+    process.stdout.write(array_flatten(content).join(''), 'utf8')
+
+main()
\ No newline at end of file
diff --git a/bin/render.js b/bin/render.js
deleted file mode 100644
index c466ef6..0000000
--- a/bin/render.js
+++ /dev/null
@@ -1,55 +0,0 @@
-var fs = require('fs');
-
-function array_flatten(arr, acc) {
-    if (typeof acc === 'undefined') {
-        acc = [];
-    }
-    if(typeof arr === 'string') {
-        acc.push(arr);
-        return acc;
-    } else if (arr.constructor !== Array) {
-        throw "Value is not an Array nor a String!";
-    }
-    for(var i=0, l=arr.length; i < l; i++) {
-        var v = arr[i];
-        if(typeof v === 'string') {
-            acc.push(v);
-        } else if(v.constructor === Array) {
-            array_flatten(v, acc);
-        }else{
-            throw "Value is not an Array nor a String!";
-        }
-    }
-    return acc;
-}
-
-function render(filename, depth) {
-    console.warn(depth + " [.] Rendering", filename);
-
-    var data = fs.readFileSync(filename, encoding='utf8');
-    data = data.replace(/\s+$/, ''); // trailing whitespace
-    var content = [];
-    content.push('// ' + depth + '[*] Including ' + filename);
-
-    var elements = data.split(/<!--\s*include\s+(\S+)\s*-->/g);
-    for(var i=0; i < elements.length; i++) {
-        var e = elements[i];
-        if(i % 2 === 0) {
-            content.push(e);
-        } else {
-            content.push( render(e, depth + '    ') );
-        }
-    }
-    content.push('// ' + depth + '[*] End of ' + filename);
-    return content;
-}
-
-(function(){
-     var content = [];
-     for(var i = 2; i < process.argv.length; i++) {
-         var filename = process.argv[i];
-         content.push( render(filename, '') );
-     }
-     content.push('\n');
-     process.stdout.write(array_flatten(content).join('\n'), 'utf8');
-})();
diff --git a/gen.sh b/gen.sh
new file mode 100755
index 0000000..74fb3c0
--- /dev/null
+++ b/gen.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+echo " [*] Generating javascript"
+coffee bin/render.coffee lib/all.js > sockjs.js && \
+coffee bin/render.coffee --minify lib/all.js > sockjs.min.js && \
+coffee bin/render.coffee --minify --pretty lib/all.js > sockjs.pretty.js && \
+echo " [*] ok"
diff --git a/lib/all.js b/lib/all.js
new file mode 100644
index 0000000..8dcad34
--- /dev/null
+++ b/lib/all.js
@@ -0,0 +1,4 @@
+// SockJS client, version <!-- version -->, MIT License
+//     https://github.com/majek/sockjs-client
+<!-- include lib/json2.min.js -->
+<!-- include_and_minify lib/main.js c -->
diff --git a/static/json2.min.js b/lib/json2.min.js
similarity index 100%
rename from static/json2.min.js
rename to lib/json2.min.js
diff --git a/run.sh b/run.sh
index 6690e0e..e6713e1 100755
--- a/run.sh
+++ b/run.sh
@@ -6,10 +6,8 @@ fi
 
 while [ 1 ]; do
     echo " [*] Generating javascript"
-    node bin/render.js lib/main.js > sockjs.js && \
-        coffee -o tests/ -c --bare tests-src/*.coffee && \
-        node bin/minify.js sockjs.js > sockjs.min.js && \
-        node bin/minify.js --pretty sockjs.js > sockjs.pretty.js && \
+    coffee -o tests/ -c --bare tests-src/*.coffee && \
+        coffee bin/render.coffee lib/all.js > sockjs.js && \
     while [ 1 ]; do
         echo " [*] Running http server"
         node bin/simple_http_server.js &

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list