Skip to content

Instantly share code, notes, and snippets.

@deckchairhq
Last active December 13, 2015 21:38
Show Gist options
  • Save deckchairhq/4978465 to your computer and use it in GitHub Desktop.
Save deckchairhq/4978465 to your computer and use it in GitHub Desktop.
Cakefile for compiling and concatenating files and directories of files in order.
# Read more about Cakefiles:
# https://github.com/jashkenas/coffee-script/wiki/[HowTo]-Compiling-and-Setting-Up-Build-Tools
# http://coffeescript.org/documentation/docs/cake.html
# http://coffeescript.org/#cake
fs = require 'fs'
path = require 'path'
coffee_script = require 'coffee-script'
{exec} = require 'child_process'
closure = require('./utils/closure')
closure_file = 'src/closure.coffee'
app_files = [
'src/lib/*'
'src/init.coffee'
'src/models/*'
'src/collections/*'
'src/application.coffee'
]
generated_filename = 'deckchair-js-sdk-latest'
addFilenameToList = ( filename ) ->
file_extension = path.extname( filename )
if file_extension == '.coffee' or file_extension == '.js'
console.log "Found file: #{ filename }"
expanded_files.push( filename )
compileCoffeescript = (source_file, options={}) ->
console.log "Compiling file: #{ source_file }"
coffeescript = fs.readFileSync source_file, 'utf8'
js = coffee_script.compile(coffeescript, options)
return js
writeToFile = (output_file, content) ->
fs.writeFileSync output_file, content, 'utf8'
console.log "Exporting to file #{ output_file }"
app_contents = new Array
expanded_files = new Array
option '-o', '--output [OUTPUT_FOLDER]', 'set the output dir for `build`'
task 'build', 'Build single application file from source files', (options) ->
options.output or= './build'
# Create an array of all files to build
# paths ending with * will be expanded automatically
for filepath, idx in app_files then do (filepath, idx) ->
path_parts = filepath.split( path.sep )
last_path_part = path_parts[path_parts.length-1]
if last_path_part == '*'
# Is a directory
clean_path = path.join.apply @, path_parts[..path_parts.length-2] # http://readystate4.com/2008/08/17/javascript-argument-unpacking-converting-an-array-into-a-list-of-arguments/
files = fs.readdirSync "#{ clean_path }"
for filename, idx in files then do (filename, idx) ->
addFilenameToList( path.join( clean_path, filename) )
else
# Is a file
addFilenameToList( filepath )
# Import compiled Coffeescript and standard JS files
# store their contents in an array
for file_path, index in expanded_files then do (file_path, index) ->
console.log "Reading: #{ file_path }"
file_extension = path.extname( file_path )
if file_extension == '.coffee'
js_string = compileCoffeescript( file_path,
bare: true
)
if file_extension == '.js'
js_string = fs.readFileSync( file_path, 'utf8' )
app_contents[index] = js_string
output = app_contents.join('\n\n')
if closure_file
console.log( "Wrapping in Closure file" )
closure_js = compileCoffeescript closure_file,
bare: true
yield_pattern = /((\/\*\s*?YIELD\s*?\*\/))/
output = closure_js.replace( yield_pattern, (args...) ->
output
)
writeToFile( "#{ options.output }/#{ generated_filename }.js", output )
console.log 'Minifying...'
closure.compile output, ( code ) ->
writeToFile( "#{ options.output }/#{ generated_filename }.min.js", code )
console.log 'Done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment