Created
November 25, 2011 07:20
-
-
Save wookiehangover/1392986 to your computer and use it in GitHub Desktop.
compile a bunch of coffeez
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require 'fs' | |
{exec} = require 'child_process' | |
util = require 'util' | |
uglify = require 'uglify-js' | |
uglifycss = require 'uglifycss' | |
growl = require 'growl' | |
coffee_src_dir = 'app' | |
js_src_dir = 'public/js' | |
coffee_output = "#{coffee_src_dir}/app.coffee" | |
js_output = "#{js_src_dir}/app.js" | |
compile_params = "--output #{js_src_dir} --compile #{coffee_output}" | |
coffeeCompile = (options = "", file) -> | |
util.log "Compiling #{file}" | |
exec "coffee #{options} --compile #{file}", (err, stdout, stderr) -> | |
handleError(err) if err | |
displayNotification "Compiled #{file}" | |
displayNotification = (message = '') -> | |
options = | |
title: 'CoffeeScript' | |
image: 'lib/bear-sharktopus.jpeg' | |
growl.notify message, options | |
handleError = (error) -> | |
util.log "\t-----> #{error}" | |
displayNotification error | |
uglyStick = ( file, minfile, no_squeeze ) -> | |
jsp = uglify.parser | |
pro = uglify.uglify | |
fs.readFile file, 'utf8', (err, fileContents) -> | |
#console.log(fileContents) | |
ast = jsp.parse fileContents # parse code and get the initial AST | |
ast = pro.ast_mangle ast # get a new AST with mangled names | |
ast = pro.ast_squeeze ast unless no_squeeze | |
final_code = pro.gen_code ast # compressed code here | |
fs.writeFile minfile, final_code | |
#fs.unlink file, (err) -> handleError(err) if err | |
message = "Uglified #{minfile}" | |
console.log message | |
displayNotification message | |
catpile_files = [ | |
'lib/init' # named files, because order matters | |
'lib/helpers' | |
'lib/*' | |
'models/*' | |
'collections/*' | |
'controllers/base_controller' | |
'controllers/*' | |
'controllers/application_controller' | |
'c2' | |
] | |
task 'catpile', 'Build a single JavaScript file from prod coffeescript files', -> | |
console.log "Building #{js_output}" | |
appContents = [] | |
counter = 0 | |
traverse = ( dir, sub_dir, index ) -> | |
for file_or_directory in dir then do (file_or_directory) -> | |
path = sub_dir + '/' + file_or_directory | |
stats = fs.statSync( coffee_src_dir + '/' + path ) | |
if stats.isFile() and catpile_files.indexOf( path.replace('.coffee','') ) < 0 # make sure that its not one of the "named" files | |
processFile( path, index ) | |
else if stats.isDirectory() | |
traverse( fs.readdirSync(coffee_src_dir + '/' +path), path, index ) | |
processFile = ( file, index ) -> | |
try | |
appContents.push( fs.readFileSync("#{coffee_src_dir}/#{file}", 'utf8') ) | |
console.log "[#{++counter}] #{file}" | |
catch err | |
handleError(err) if err | |
for file, index in catpile_files then do (file, index) -> | |
if /\/\*/.test(file) | |
sub_dir = file.replace('/*', '') | |
traverse( fs.readdirSync( coffee_src_dir + '/' + sub_dir ), sub_dir, index ) | |
else | |
processFile( file + '.coffee', index) | |
# this is synchronous intentionally | |
fs.writeFile( coffee_output, appContents.join('\n\n'), null, 'utf8' ) | |
exec "coffee #{compile_params}", (err, stdout, stderr) -> | |
handleError(err) if err | |
message = "Compiled #{js_output}" | |
console.log( message ) | |
displayNotification( message ) | |
fs.unlink( coffee_output, (err) -> handleError(err) if err ) | |
uglyStick js_output, "public/js/src/app.min.js" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment