Created
November 14, 2013 03:55
-
-
Save chenhunghan/7461100 to your computer and use it in GitHub Desktop.
a small coffescript for building coffeecup-html with error logging, file-watch and helpers
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
#!/usr/bin/env coffee | |
fs = require 'fs' | |
ck = require 'coffeecup' | |
path = require 'path' | |
viewhelpers = {} | |
viewhelper_merge = (vhs)-> | |
for k, v of vhs | |
viewhelpers[k] = v | |
vhdir = './viewhelpers/' | |
for hfn in ['base', 'bootstrap'] | |
viewhelper_merge require( vhdir+"#{hfn}.coffee") | |
viewhelpers._partial = (arg)-> | |
text '@partial '+arg | |
viewhelpers._body = (arg)-> | |
text '@body ' | |
compilng = (cfile) -> | |
ext = path.extname(cfile) | |
if ext == '.coffee' | |
rendering = () -> | |
htmlpath = path.basename(cfile, '.coffee') + '.html' | |
options = { hardcode: viewhelpers, format: true} | |
contents = fs.readFileSync cfile, 'utf-8' | |
try | |
renderhtml = ck.render contents, options | |
catch e | |
console.log path.basename(cfile) + ' has an error.' | |
if e.location? | |
console.log "error is " + "' " + e.message + " ' -> " + 'first line: ' + (e.location.first_line) + ' ,last line: ' + (e.location.last_line) + '. first column: ' + (e.location.first_column) + ' ,last column: ' + (e.location.last_column) | |
else console.log e | |
if renderhtml? | |
fs.writeFileSync htmlpath, renderhtml, 'utf-8' | |
console.log path.basename(cfile) + ' recompiled.' | |
watching = () -> | |
fs.watch cfile, (event, filename) -> | |
if event is 'change' | |
rendering() | |
if event is 'rename' | |
console.log 'error: file has been renamed.' | |
watching() | |
else | |
console.log 'error: filetype ' + ext + ' not supported.' | |
main = (argv)-> | |
files = argv[2..] | |
console.log files | |
for file in files | |
compilng(file) | |
main(process.argv) if require.main==module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment