Permalink
Browse files

first version

  • Loading branch information...
1 parent 1d41cdd commit e4afd408192961d136a9b6d733b0bf09d6c823e7 @LinusU committed Mar 13, 2013
Showing with 109 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +4 −0 index.js
  3. +16 −0 lib/assman.coffee
  4. +77 −0 lib/compiler.coffee
  5. +10 −0 package.json
View
@@ -12,3 +12,5 @@ logs
results
npm-debug.log
+node_modules
+
View
@@ -0,0 +1,4 @@
+
+require('coffee-script');
+
+module.exports = exports = require('./lib/assman');
View
@@ -0,0 +1,16 @@
+
+path = require 'path'
+compiler = require './compiler'
+
+state =
+ top: ''
+ map: {}
+
+module.exports = exports =
+ top: (top) -> state.top = top
+ register: (type, name, assets) ->
+ state.map['/' + name + '.' + type] = { type: type, name: name, assets: assets.map (e) -> path.join state.top, e }
+ middleware: (req, res, next) ->
+ if state.map[req.url]
+ compiler res, state.map[req.url].type, state.map[req.url].assets
+ else do next
View
@@ -0,0 +1,77 @@
+
+fs = require 'fs'
+nib = require 'nib'
+path = require 'path'
+glob = require 'glob'
+stylus = require 'stylus'
+coffee = require 'coffee-script'
+
+handleAsset = (res, asset, cb) ->
+
+ m = asset.match /\.[a-z]+$/
+ if m is null then throw new Error "Cannot find extension (#{asset})"
+
+ done = (err, data) ->
+ if err then cb err else
+ res.write data
+ cb null
+
+ switch m[0]
+ when '.js' then fs.readFile asset, done
+ when '.css' then fs.readFile asset, done
+ when '.styl'
+ fs.readFile asset, (err, data) ->
+ if err then done err else
+ stylus(data.toString())
+ .set('filename', asset)
+ .set('paths', [ path.dirname asset ])
+ .set('compress', true)
+ .use(nib())
+ .import('nib')
+ .render(done)
+ when '.coffee'
+ fs.readFile asset, (err, data) ->
+ if err then done err else
+ done null, coffee.compile data.toString()
+ else
+ throw new Error "Cannot handle extension (#{m[0]})"
+
+expandAssets = (assets, cb) ->
+ result = []
+
+ # Clone the array
+ assets = assets.map (e) -> e
+
+ do next = (err = null) ->
+ if err
+ cb err
+ else if assets.length is 0
+ cb null, result
+ else
+ a = assets.shift()
+ if a.indexOf('*') is -1
+ result.push a
+ next null
+ else
+ glob a, (err, files) ->
+ if err then next err else
+ result = result.concat files
+ next null
+
+module.exports = exports = (res, type, assets) ->
+
+ switch type
+ when 'js' then res.setHeader 'Content-Type', 'text/javascript'
+ when 'css' then res.setHeader 'Content-Type', 'text/css'
+ else throw new Error "Unknown type (#{type})"
+
+ expandAssets assets, (err, assets) ->
+ do next = (err) ->
+ if err
+ console.log err.name + ':', err.message
+ console.log err.stack
+ if assets.length is 0
+ res.end()
+ else
+ a = assets.shift()
+ handleAsset res, a, next
View
@@ -0,0 +1,10 @@
+{
+ "name": "assman",
+ "version": "0.1.0",
+ "dependencies": {
+ "glob": "3.1.x",
+ "coffee-script": "1.6.x",
+ "stylus": "0.32.x",
+ "nib": "0.9.x"
+ }
+}

0 comments on commit e4afd40

Please sign in to comment.