Skip to content

Commit

Permalink
First commit of major refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgtonge committed Sep 17, 2012
1 parent 2e1637d commit bfb0ca5
Show file tree
Hide file tree
Showing 15 changed files with 899 additions and 3,405 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ before_script:
- "npm install" - "npm install"


script: script:
- "node_modules/.bin/cake test" - "node_modules/.bin/mocha"
240 changes: 225 additions & 15 deletions Cakefile
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,231 @@
fs = require 'fs' # ** Cakefile Template ** is a Template for a common Cakefile that you may use in a coffeescript nodejs project.
{exec} = require 'child_process' #
# It comes baked in with 5 tasks:
#
# * build - compiles your src directory to your lib directory
# * watch - watches any changes in your src directory and automatically compiles to the lib directory
# * test - runs mocha test framework, you can edit this task to use your favorite test framework
# * docs - generates annotated documentation using docco
# * clean - clean generated .js files
files = [
'lib'
'src'
]


task 'build', 'Build JS files from Coffee sources', -> fs = require 'fs'
{print} = require 'util'
{spawn, exec} = require 'child_process'


exec 'coffee -c -o js/ src/', (err, stdout, stderr) -> try
throw err if err which = require('which').sync
console.log stdout + stderr catch err
if process.platform.match(/^win/)?
console.log 'WARNING: the which module is required for windows\ntry: npm install which'
which = null


exec 'coffee -c test/backbone-query-test.coffee', (err, stdout, stderr) -> # ANSI Terminal Colors
throw err if err bold = '\x1b[0;1m'
console.log stdout + stderr green = '\x1b[0;32m'
reset = '\x1b[0m'
red = '\x1b[0;31m'

# Cakefile Tasks
#
# ## *docs*
#
# Generate Annotated Documentation
#
# <small>Usage</small>
#
# ```
# cake docs
# ```
task 'docs', 'generate documentation', -> docco()

# ## *build*
#
# Builds Source
#
# <small>Usage</small>
#
# ```
# cake build
# ```
task 'build', 'compile source', -> build -> log ":)", green

# ## *watch*
#
# Builds your source whenever it changes
#
# <small>Usage</small>
#
# ```
# cake watch
# ```
task 'watch', 'compile and watch', -> build true, -> log ":-)", green

# ## *test*
#
# Runs your test suite.
#
# <small>Usage</small>
#
# ```
# cake test
# ```
task 'test', 'run tests', -> build -> mocha -> log ":)", green

# ## *clean*
#
# Cleans up generated js files
#
# <small>Usage</small>
#
# ```
# cake clean
# ```
task 'clean', 'clean generated files', -> clean -> log ";)", green


# Internal Functions
#
# ## *walk*
#
# **given** string as dir which represents a directory in relation to local directory
# **and** callback as done in the form of (err, results)
# **then** recurse through directory returning an array of files
#
# Examples
#
# ``` coffeescript
# walk 'src', (err, results) -> console.log results
# ```
walk = (dir, done) ->
results = []
fs.readdir dir, (err, list) ->
return done(err, []) if err
pending = list.length
return done(null, results) unless pending
for name in list
file = "#{dir}/#{name}"
try
stat = fs.statSync file
catch err
stat = null
if stat?.isDirectory()
walk file, (err, res) ->
results.push name for name in res
done(null, results) unless --pending
else
results.push file
done(null, results) unless --pending

# ## *log*
#
# **given** string as a message
# **and** string as a color
# **and** optional string as an explanation
# **then** builds a statement and logs to console.
#
log = (message, color, explanation) -> console.log color + message + reset + ' ' + (explanation or '')

# ## *launch*
#
# **given** string as a cmd
# **and** optional array and option flags
# **and** optional callback
# **then** spawn cmd with options
# **and** pipe to process stdout and stderr respectively
# **and** on child process exit emit callback if set and status is 0
launch = (cmd, options=[], callback) ->
cmd = which(cmd) if which
app = spawn cmd, options
app.stdout.pipe(process.stdout)
app.stderr.pipe(process.stderr)
app.on 'exit', (status) -> callback?() if status is 0

# ## *build*
#
# **given** optional boolean as watch
# **and** optional function as callback
# **then** invoke launch passing coffee command
# **and** defaulted options to compile src to lib
build = (watch, callback) ->
if typeof watch is 'function'
callback = watch
watch = false

options = ['-c', '-b', '-o' ]
options = options.concat files
options.unshift '-w' if watch
launch 'coffee', options, callback

# ## *unlinkIfCoffeeFile*
#
# **given** string as file
# **and** file ends in '.coffee'
# **then** convert '.coffee' to '.js'
# **and** remove the result
unlinkIfCoffeeFile = (file) ->
if file.match /\.coffee$/
fs.unlink file.replace(/\.coffee$/, '.js')
true
else false

# ## *clean*
#
# **given** optional function as callback
# **then** loop through files variable
# **and** call unlinkIfCoffeeFile on each
clean = (callback) ->
try
for file in files
unless unlinkIfCoffeeFile file
walk file, (err, results) ->
for f in results
unlinkIfCoffeeFile f

callback?()
catch err

# ## *moduleExists*
#
# **given** name for module
# **when** trying to require module
# **and** not found
# **then* print not found message with install helper in red
# **and* return false if not found
moduleExists = (name) ->
try
require name
catch err
log "#{name} required: npm install #{name}", red
false


# ## *mocha*
#
# **given** optional array of option flags
# **and** optional function as callback
# **then** invoke launch passing mocha command
mocha = (options, callback) ->
#if moduleExists('mocha')
if typeof options is 'function'
callback = options
options = []
# add coffee directive
options.push '--coffee'
options.push 'coffee:coffee-script'

launch 'mocha', options, callback

# ## *docco*
#
# **given** optional function as callback
# **then** invoke launch passing docco command
docco = (callback) ->
#if moduleExists('docco')
walk 'src', (err, files) -> launch 'docco', files, callback


task 'uglify', 'Minify and obfuscate', -> task 'uglify', 'Minify and obfuscate', ->
uglify = require 'uglify-js' uglify = require 'uglify-js'
Expand All @@ -24,9 +240,3 @@ task 'uglify', 'Minify and obfuscate', ->
final_code = pro.gen_code ast # compressed code here final_code = pro.gen_code ast # compressed code here


fs.writeFile 'js/backbone-query.min.js', final_code fs.writeFile 'js/backbone-query.min.js', final_code

task "test", "Test the code", ->
path = require 'path'
reporter = require('nodeunit').reporters.default

reporter.run ["test/backbone-query-test.js"]
Loading

0 comments on commit bfb0ca5

Please sign in to comment.