Permalink
Browse files

More asset refactoring and a fix for #251

  • Loading branch information...
1 parent 3c108eb commit 9396e598d5306ae0c185ccbbc7f2b4d28fa1b128 Owen Barnes committed Jun 23, 2012
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -2,11 +2,11 @@
# ---------------
# Loads default code formatters and presents an API for loading custom formatters
-mods = []
+module.exports = (ss) ->
-exports.init = (root) ->
+ mods = []
- add = (nameOrModule, config = {}) ->
+ add: (nameOrModule, config = {}) ->
mod = if typeof(nameOrModule) == 'object'
nameOrModule
else
@@ -16,21 +16,12 @@ exports.init = (root) ->
else
throw new Error("The #{nameOrModule} formatter is not supported by SocketStream internally. Please pass a compatible module instead")
- formatter = mod.init(root, config)
+ formatter = mod.init(ss.root, config)
mods.push(formatter)
- # Load system defaults
- add('javascript')
- add('css')
- add('html')
-
- ### RETURN API ###
- return {add: add}
-
-
-exports.load = ->
- byExtension = {}
- mods.forEach (mod) ->
- mod.extensions.forEach (extension) ->
- byExtension[extension] = mod
- byExtension
+ load: ->
+ byExtension = {}
+ mods.forEach (mod) ->
+ mod.extensions.forEach (extension) ->
+ byExtension[extension] = mod
+ byExtension
View
@@ -8,8 +8,6 @@
fs = require('fs')
path = require('path')
systemAssets = require('./system')
-templateEngine = require('./template_engine')
-formatters = require('./formatters')
# Determine if assets should be (re)packed on startup
packAssets = process.env['SS_PACK']
@@ -33,6 +31,16 @@ clients = {}
module.exports = (ss, router) ->
+ # Require sub modules
+ templateEngine = require('./template_engine')(ss)
+ formatters = require('./formatters')(ss)
+ http = require('./http')(ss, clients, options)
+
+ # Load default code formatters
+ formatters.add('javascript')
+ formatters.add('css')
+ formatters.add('html')
+
# Very basic check to see if we can find pre-packed assets
# TODO: Improve to test for complete set
determineLatestId = (client) ->
@@ -45,14 +53,12 @@ module.exports = (ss, router) ->
catch e
false
-
- http = require('./http')(ss, clients, options)
systemAssets.load()
# Return API
- formatters: formatters.init(ss.root)
- templateEngine: templateEngine(ss)
+ formatters: formatters
+ templateEngine: templateEngine
assets: systemAssets
options: options
@@ -91,7 +97,13 @@ module.exports = (ss, router) ->
# Listen and serve incoming asset requests
load: ->
+
+ # Cache instances of code formatters and template engines here
+ # This may change in the future as I don't like hanging system objects
+ # on the 'ss' internal API object, but for now it solves a problem
+ # we were having when repl.start() would erase vars cached inside a module
ss.client.formatters = formatters.load()
+ ss.client.templateEngines = templateEngine.load()
# Code to execute once everything is loaded
systemAssets.send('code', 'init', "require('/entry');")
@@ -11,12 +11,11 @@ pathlib = require('path')
formatters = require('./formatters')
client = require('./system')
-templateEngines = {}
-defaultEngine = null
-
# Allow Template Engine to be configured
module.exports = (ss) ->
+ mods = []
+
# Set the Default Engine - simply wraps each template in a <script> tag
defaultEngine = require('./template_engines/default').init(ss.root)
@@ -33,13 +32,18 @@ module.exports = (ss) ->
else
throw new Error("The #{nameOrModule} template engine is not supported by SocketStream internally. Please pass a compatible module instead")
+ dirs = [dirs] unless dirs instanceof Array
engine = mod.init(ss, config)
+ mods.push({engine: engine, dirs: dirs})
- dirs = [dirs] unless dirs instanceof Array
- dirs.forEach (dir) ->
- unless dir.substring(0,1) == '/'
- throw new Error("Directory name '#{dir}' passed to second argument of ss.client.templateEngine.use() command must start with /")
- templateEngines[dir] = engine
+ load: ->
+ templateEngines = {}
+ mods.forEach (mod) ->
+ mod.dirs.forEach (dir) ->
+ unless dir.substring(0,1) == '/'
+ throw new Error("Directory name '#{dir}' passed to second argument of ss.client.templateEngine.use() command must start with /")
+ templateEngines[dir] = mod.engine
+ templateEngines
# Generate output (as a string) from Template Engines
@@ -54,7 +58,7 @@ module.exports = (ss) ->
fullPath = pathlib.join(dir, path)
# Work out which template engine to use, based upon the path
- engine = selectEngine(templateEngines, path.split('/')) || defaultEngine
+ engine = selectEngine(ss.client.templateEngines, path.split('/')) || defaultEngine
# Try and guess the correct formatter to use BEFORE the content is sent to the template engine
extension = pathlib.extname(path)

0 comments on commit 9396e59

Please sign in to comment.