Last active
December 20, 2015 06:39
-
-
Save jibone/6087019 to your computer and use it in GitHub Desktop.
The current cake file I'm using at the moment.
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
### | |
# | |
# Build Script | |
# Compiles Sass and Coffee files to CSS and JavaScript | |
# | |
### | |
# ------------------------------------------------------ | |
# -- Load dependencies --------------------------------- | |
fs = require "fs" | |
{ spawn, exec } = require "child_process" | |
# ------------------------------------------------------ | |
# -- console colors ------------------------------------ | |
red = "\u001b[31m" | |
blue = "\u001b[34m" | |
green = "\u001b[32m" | |
reset = "\u001b[0m" | |
# ------------------------------------------------------ | |
# -- Define directory path ----------------------------- | |
coffee = | |
srcDir: '_src/coffee' | |
outDir: 'js' | |
inFiles: ['app'] | |
outFile: 'app' | |
joinedFiles = ("#{coffee.srcDir}/#{file}.coffee" for file in coffee.inFiles).join ' ' | |
jsOutput = "#{coffee.outDir}/#{coffee.outFile}" | |
sass = | |
srcDir: '_src/sass' | |
outDir: 'css' | |
file: 'style' | |
# ------------------------------------------------------ | |
# -- time function ------------------------------------- | |
gettime = () -> | |
d = new Date() | |
curr_hour = d.getHours() | |
if curr_hour < 12 | |
a_p = "AM" | |
else | |
a_p = "PM" | |
curr_hour = 12 if curr_hour is 0 | |
curr_hour = curr_hour - 12 if curr_hour > 12 | |
curr_min = d.getMinutes() | |
curr_min = curr_min + "" | |
curr_min = "0" + curr_min if curr_min.length is 1 | |
curr_hour + ":" + curr_min + " " + a_p | |
# ------------------------------------------------------ | |
# -- Deal with the errors ------------------------------ | |
exerr = (err, sout, serr) -> | |
process.stdout.write "[#{red}#{gettime()}#{reset}] " + err if err | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + sout if sout | |
process.stdout.write "[#{red}#{gettime()}#{reset}] " + serr if serr | |
# ------------------------------------------------------ | |
# -- Compile & Watch functions ------------------------- | |
compileCoffee = () -> | |
exec "coffee -j #{jsOutput}.js -c #{joinedFiles}", exerr | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + green + "compiled" + reset + " #{joinedFiles}\n" | |
compileSass = () -> | |
exec "sass -t compressed #{sass.srcDir}/#{sass.file}.scss #{sass.outDir}/#{sass.file}.css", exerr | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + green + "compiled" + reset + " #{sass.srcDir}/#{sass.file}.scss\n" | |
watchCoffeeFiles = (filename) -> | |
unless filename.charAt(0) is "." | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + "#{blue}watching#{reset}: " + "#{coffee.srcDir}/" + filename + "\n" | |
fs.watchFile "#{coffee.srcDir}" + filename, (curr, prev) -> | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + "#{blue}changed!#{reset}: " + filename + "\n" | |
compileCoffee() | |
watchSassFiles = (filename) -> | |
unless filename.charAt(0) is "." | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + "#{blue}watching#{reset}: " + "#{sass.srcDir}/" + filename + "\n" | |
fs.watchFile "#{sass.srcDir}/" + filename, (curr, prev) -> | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + "#{blue}changed!#{reset}: " + filename + "\n" | |
compileSass() | |
# ------------------------------------------------------ | |
# -- All the tasks ------------------------------------- | |
task 'build', 'join and compile *.coffee and *.scss files', -> | |
compileCoffee() | |
compileSass() | |
task 'build:js', 'join and compile *.coffee', -> | |
compileCoffee() | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + green + "compiled" + reset + " #{strFiles}\n" | |
task 'build:css', 'compile *.scss files', -> | |
compileSass() | |
process.stdout.write "[#{blue}#{gettime()}#{reset}] " + green + "compiled" + reset + " #{sass.file}.css\n" | |
task 'watch', 'watch coffee and sass directory for changes and compile', -> | |
filearrayCoffee = fs.readdirSync coffee.srcDir | |
watchCoffeeFiles filename for filename in filearrayCoffee | |
filearraySass = fs.readdirSync sass.srcDir | |
watchSassFiles filename for filename in filearraySass | |
task 'watch:js', 'watch coffee directory for changes and compile', -> | |
filearray = fs.readdirSync coffee.srcDir | |
watchCoffeeFiles filename for filename in filearray | |
task 'watch:css', 'watch sass directory for changes and compile', -> | |
filearray = fs.readdirSync sass.srcDir | |
watchSassFiles filename for filename in filearray |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment