       require('watch').watchTree dir, { persistent: true, interval: 1000 },  (file, curr, prev) =>
         @build() if curr and (curr.nlink is 0 or +curr.mtime isnt +prev?.mtime)
 
-  # TODO: move this to a separate middleware class, pass in package to call compile and content type on..
   middleware: (req, res, next) =>
     str = @compile()
     res.charset = 'utf-8'

          _path  = require.resolve(path.resolve(_path))
          delete require.cache[_path]
          result.push require(_path)
        result.join("\n")
    catch ex
      console.trace ex
      # only return when in server mode, otherwise exit
      # TODO: pass in mode as argument instead of checking process.argv??
      command = process.argv[2]
      switch command
        when "server" then return "console.log(\"#{ex}\");"
        when "watch"  then return ""
        when "build"  then process.exit(1)

  isJavascript: ->
    @contentType is "application/javascript"

fs           = require('fs')
path         = require('path')
uglify       = require('uglify-js')
stitchFile   = require('../assets/stitch')
Dependency   = require('./dependency')
Stitch       = require('./stitch')
{toArray}    = require('./utils')
mime         = require('connect').static.mime

class Package
  constructor: (name, config = {}, argv = {}) ->
    @name        = name
    @argv        = argv
    # set config values
    @require     = config.require
    @target      = config.target
    @libs        = toArray(config.libs || [])
    @paths       = toArray(config.paths || [])
    @modules     = toArray(config.modules || [])
    @jsAfter     = config.jsAfter or ""
    @url         = config.url or ""
    # TODO: sanity checkes on config values??
    # determine content type based on target file name
    @contentType = mime.lookup(@target)

  compileModules: ->
    @depend or= new Dependency(@modules)
    _stitch   = new Stitch(@paths)
    _modules  = @depend.resolve().concat(_stitch.resolve())
    stitchFile(identifier: @require, modules: _modules)

  compileLibs: ->
    # TODO: be able to handle being given a folder and loading each file...can this compile coffeescript??
    (fs.readFileSync(lib, 'utf8') for lib in @libs).join("\n")

  compile: (minify = false) ->
