Created
April 18, 2013 14:24
-
-
Save silviopaganini/5413109 to your computer and use it in GitHub Desktop.
standard CakeFile
This file contains 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
### | |
silvio paganini | s2paganini | @silviopaganini | |
Thanks to https://github.com/jashkenas | |
### | |
### -------------- INIT VARS -------------- ### | |
PROJECT_NAME = "" | |
CMS_FOLDER = "project/develop/cms/" | |
OUTPUT_FOLDER = "website/js/" | |
SOURCE_FOLDER = "project/develop/src/" | |
OUTPUT_JS = "#{OUTPUT_FOLDER}main.js" | |
OUTPUT_JS_MIN = "#{OUTPUT_FOLDER}main.min.js" | |
VENDORS_MIN = "#{OUTPUT_FOLDER}vendor/vendors.min.js" | |
VENDORS_MERGED = "#{OUTPUT_FOLDER}vendor/merge/vendors.merged.js" | |
VENDORS_TO_MERGE = "#{OUTPUT_FOLDER}vendor/merge" | |
SOURCE_FILES = [ | |
"#{SOURCE_FOLDER}App.coffee" | |
] | |
### | |
# NOTHING TO LOOK FROM HERE # | |
___ | |
_//_\\ | |
," //". | |
/ \ | |
_/ | | |
(.-,--. | | |
/o/ o \ / | |
\_\ / /\/\ | |
(__`--' ._) | |
/ `-. | | |
( ,`-. | | |
`-,--\_ ) |-. | |
_`.__.' ,-' \ | |
|\ ) _.-' | | |
i-\.'\ ,--+. | |
.' .' \,-'/ \ | |
/ / / \ | |
7_| | | | |
|/ "i.___.j" | |
/ | |\ | |
/ | | \ | |
/ | | | | |
| | | | | |
|____ | |-i' | |
| """"----""| | | | |
\ ,-' |/ | |
`. `-, | | |
|`-._ / /| |\ \ | |
| `-. `' | ||`-' | |
| | `-'| | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
)`-.___| | | |
.'`-.____)`-.___.-'( | |
.' .'-._____.-i | |
/ .' | | |
`-------/ . | | |
`--------' "--' | |
###################################################################################################### | |
FILE_ENCODING = 'utf-8' | |
EOL = '\n' | |
### --------- ANSI Terminal Colors ----------- ### | |
bold = '\x1b[0;1m' | |
green = '\x1b[0;32m' | |
reset = '\x1b[0m' | |
red = '\x1b[0;31m' | |
yellow = '\x1b[0;33m' | |
### -------------- DEFINE TASKS --------------- ### | |
option '-o', '--options [PARAMS]', "\n\toptions for #{yellow}cake build #{reset}\n\t\t-w \twatch the folder \n\t\t-m \tminify the build \n\t #{bold}usage:#{reset} cake -o \"-m\" build" | |
task 'build', 'compile source', (options) -> build options, -> log ":) build complete", green | |
task 'build:watch', 'shortcut to build watch', (options) -> build {options:"-w"}, -> log ":) build complete", green | |
task 'minify_vendors', 'minify vendors', -> minify_vendors -> log ":) minify vendors complete", green | |
### ----------- NODEJS REQUIRE ------------ ### | |
pro = require('uglify-js').uglify | |
jsp = require("uglify-js").parser | |
path = require("path") | |
fs = require 'fs' | |
{print} = require 'util' | |
{spawn, exec} = require 'child_process' | |
try | |
which = require('which').sync | |
catch err | |
if process.platform.match(/^win/)? | |
console.log 'WARNING: the which module is required for windows\ntry: npm install which' | |
which = null | |
### --------- LOG ----------- ### | |
log = (message, color = green, explanation = '') -> console.log color + message + reset + ' ' + (explanation or '') | |
### --------- UTILS ---------- ### | |
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 | |
null | |
### --------- TASKS ---------- ### | |
build = (params, callback) -> | |
watch = params.options == "-w" | |
options = ['--join', OUTPUT_JS ,'--compile'] | |
options = options.concat SOURCE_FILES | |
log "watching #{PROJECT_NAME} CoffeeScript files ..." if watch | |
options.unshift '-w' if watch is true | |
launch 'coffee', options, -> | |
if params.options == "-m" | |
minify_build -> | |
log ':) build and minify complete', green | |
else | |
callback() | |
# compress | |
minify_vendors = (callback) -> | |
internalWalk = -> | |
walk VENDORS_TO_MERGE, (err, results) -> | |
log '1. vendors list created', yellow | |
VENDORS_MERGED = mergeFiles results, VENDORS_MERGED | |
log '2. vendors merged', yellow | |
log '3. minifying...', yellow | |
uglifyOptions = ['-o',VENDORS_MIN, VENDORS_MERGED] | |
launch 'uglifyjs', uglifyOptions, -> | |
log '4. vendors minified', yellow | |
launch 'rm', [VENDORS_MERGED], -> | |
log '5. clean up', yellow | |
callback() | |
fs.exists VENDORS_MERGED, (exist) -> | |
if(exist) | |
launch 'rm', [VENDORS_MERGED], internalWalk | |
else | |
internalWalk() | |
null | |
minify_build = (callback) -> | |
options = ['-c', '-o', OUTPUT_JS_MIN, OUTPUT_JS] | |
launch 'uglifyjs', options, callback | |
mergeFiles = (files, dest) -> | |
fileList = files | |
distPath = dest | |
out = fileList.map (filePath) => | |
return fs.readFileSync(filePath, FILE_ENCODING) | |
fs.writeFileSync(distPath, out.join(EOL), FILE_ENCODING) | |
return distPath | |
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 file.indexOf('.svn') == -1 and file.indexOf('.DS_Store') == -1 | |
if stat?.isDirectory() | |
walk file, (err, res) -> | |
results.push name for name in res | |
done(null, results) unless --pending | |
else | |
if file.indexOf('.js') > -1 || file.indexOf('.coffee') | |
results.push file | |
done(null, results) unless --pending | |
else | |
done(null, results) unless --pending | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment