Skip to content

Commit

Permalink
Made watch compile coffeescript and .jst files more reliably. Coffees…
Browse files Browse the repository at this point in the history
…cript files are now compiled as so:

/app/model/post.coffee -> /app/model/.js/post.js

And the correct files are included in the .jst files. Gives better stack traces and don't have to compile coffeescript in the browser.
  • Loading branch information
bnolan committed Apr 20, 2011
1 parent b447d4d commit f0c36bc
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 37 deletions.
53 changes: 31 additions & 22 deletions src/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,37 @@ task 'watch', 'watch files and regenerate test.html and index.html as needed', (
project = new Project(process.cwd())
project.targets = arguments

timer = null

doRebuild = ->
sys.puts "Rebuilt project..."

ejs = fs.readFileSync("#{project.root}/index.jst") + ""
fs.writeFileSync("#{project.root}/index.html", _.template(ejs, { project : project }))

ejs = fs.readFileSync("#{project.root}/spec/index.jst") + ""
fs.writeFileSync("#{project.root}/spec/index.html", _.template(ejs, { project : project }))

rebuild = ->
if timer
clearTimeout timer

timer = setTimeout(doRebuild, 25)

for script in project.getFilesToWatch()
fs.watchFile Path.join(project.root, script), ->
rebuild()

rebuild()
# timer = null

watch = (source) ->
fs.watchFile Path.join(project.root, source), {persistent: true, interval: 500}, (curr, prev) ->
return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
project.compileFile(source)

for source in project.getWatchables()
watch(source)
project.compileFile(source)

# doRebuild = ->
# sys.puts "Rebuilt project..."
#
# ejs = fs.readFileSync("#{project.root}/index.jst") + ""
# fs.writeFileSync("#{project.root}/index.html", _.template(ejs, { project : project }))
#
# ejs = fs.readFileSync("#{project.root}/spec/index.jst") + ""
# fs.writeFileSync("#{project.root}/spec/index.html", _.template(ejs, { project : project }))
#
# rebuild = ->
# if timer
# clearTimeout timer
#
# timer = setTimeout(doRebuild, 25)
#
# for script in project.getFilesToWatch()
# fs.watchFile Path.join(project.root, script), ->
# rebuild()
#
# rebuild()

task 'new', 'create a new project', (arguments) ->
project = arguments[0] or raise("Must supply a name for new project.")
Expand Down
89 changes: 74 additions & 15 deletions src/project.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Path = require("path")
Glob = require("glob").globSync
exec = require('child_process').exec
_ = require("#{root}/lib/underscore")
CoffeeScript = require 'coffee-script'

String.prototype.capitalize = ->
this.charAt(0).toUpperCase() + this.substring(1).toLowerCase()
Expand All @@ -28,23 +29,17 @@ class Project
Path.join(@cwd, "config.yml")

getScriptTagFor: (path) ->
if path.match(/coffee$/)
"<script src='#{path}' type='text/coffeescript'></script>"
if Path.extname(path) == '.coffee'
jspath = Path.join(Path.dirname(path), ".js", Path.basename(path, '.coffee') + '.js')
"<script src='#{jspath}' type='text/javascript'></script>"
else
"<script src='#{path}' type='text/javascript'></script>"

getStyleTagFor: (path) ->
if path.match(/less$/)
"<link href='#{path}' rel='stylesheet/less' type='text/css' />"
if Path.extname(path) == '.less'
"<link href='#{path}' rel='stylesheet/less' type='text/css' />"
else
"<link href='#{path}' media='screen' rel='stylesheet' type='text/css' />"

testScriptIncludes: ->
tags = for path in Glob(Path.join(@cwd, "test", "**", "*.#{@language()}"))
script = path.replace(@cwd, '')
@getScriptTagFor script

tags.join("\n")
"<link href='#{path}' media='screen' rel='stylesheet' type='text/css' />"

bundleStylesheet : (filename) ->
index = 0
Expand Down Expand Up @@ -109,6 +104,9 @@ class Project
scripts.unique()

getDependencies: (section) ->
if !@yaml[section]
throw "[ERROR] Unable to find section '#{section}' in config.yml"

result = _([])

for pathspec in @yaml[section]
Expand All @@ -132,7 +130,7 @@ class Project
tags = for css in @getStylesheetDependencies()
@getStyleTagFor css

tags.join("\n")
tags.join("\n ")

specIncludes : ->
tags = for script in @getScriptDependencies()
Expand All @@ -141,13 +139,74 @@ class Project
for script in @getDependencies('specs')
tags.push @getScriptTagFor script

tags.join("\n")
tags.join("\n ")

scriptIncludes : ->
tags = for script in @getScriptDependencies()
@getScriptTagFor script

tags.join("\n")
tags.join("\n ")

compileFile : (file) ->
extension = Path.extname(file)

if extension == ".coffee"
@_compileCoffee(file)
else if extension == ".less"
@_compileLess(file)
else if extension == ".jst"
@_compileJst(file)
else
# do nothing...

getWatchables : ->
['/index.jst', '/spec/index.jst'].concat(
@getDependencies('specs')
@getScriptDependencies()
@getStylesheetDependencies()
)

_compileLess : ->
sys.puts "project#_compileLess not implemented..."

_compileCoffee : (file) ->
fs.readFile Path.join(@root, file), (err, code) =>
throw err if err

path = Path.join(Path.dirname(file), ".js")
outpath = Path.join(path, Path.basename(file, ".coffee") + ".js")

try
fs.mkdirSync Path.join(@root, path), 0755
catch e
# .. ok ..

try
output = CoffeeScript.compile(new String(code))
catch err
sys.puts " * Error compiling #{file}"
sys.puts err.message
return

sys.puts " * Compiled " + outpath
fs.writeFileSync Path.join(@root, outpath), output


_compileJst : (file) ->
fs.readFile Path.join(@root, file), (err, code) =>
throw err if err

outpath = Path.join(Path.dirname(file), Path.basename(file, '.jst') + ".html")

try
output = _.template(new String(code), { project : this })
catch err
sys.puts " * Error compiling #{file}"
sys.puts err.message
return

sys.puts " * Compiled " + outpath
fs.writeFileSync Path.join(@root, outpath), output


exports.Project = Project

0 comments on commit f0c36bc

Please sign in to comment.