From 29754e49e59e950b3d59badb362f51fc8913c8a2 Mon Sep 17 00:00:00 2001 From: dsc Date: Mon, 20 Feb 2012 02:28:14 -0800 Subject: [PATCH] Dev server now serves .jade, .co, .styl as text/plain (so browsers will display them, instead of downloading). --- Cokefile | 26 +++++++++++--- lib/server/index.js | 3 ++ lib/server/server.co | 43 ++++++++++++++++--------- lib/underscore/array.co | 47 +++++++++++++++++++++++++++ lib/underscore/index.co | 6 ++- lib/underscore/object.co | 60 +++++++++++++++++++++++++++++++++++ lib/underscore/string.co | 36 +++++++++++++++++++++ lib/underscore/underscore.array.co | 47 --------------------------- lib/underscore/underscore.object.co | 60 ----------------------------------- 9 files changed, 198 insertions(+), 130 deletions(-) create mode 100644 lib/underscore/array.co create mode 100644 lib/underscore/object.co create mode 100644 lib/underscore/string.co delete mode 100644 lib/underscore/underscore.array.co delete mode 100644 lib/underscore/underscore.object.co diff --git a/Cokefile b/Cokefile index 5384371..a663a84 100644 --- a/Cokefile +++ b/Cokefile @@ -2,7 +2,9 @@ require 'buildtools' require 'buildtools/tasks' -MODULE_LINK = "node_modules/kraken" +MODULE_LINK = 'node_modules/kraken' +EXPRESS_DEP_MIME = 'node_modules/express/node_modules/mime' + task \link 'Link package source to node_modules so the name resolves correctly' -> # Browser-based require doens't support relative requires, but things # like `require 'kraken/utils'` rarely work in node without this hack. @@ -11,18 +13,30 @@ task \link 'Link package source to node_modules so the name resolves correctly' say "Creating #{basename MODULE_LINK} symlink..." fs.symlinkSync "../lib", MODULE_LINK -task \server 'Start dev server' -> +task \install 'Install project dependencies.' -> + <- sh 'npm install', {-verbose, +errors, +die} + + # Remove express's copy of node-mime so we can add our own extensions + # and have it pick them up by default. + remove EXPRESS_DEP_MIME, true if exists EXPRESS_DEP_MIME + +task \setup 'Ensure project is set up for development.' -> + invoke \install invoke \link invoke \update_version + + +task \server 'Start dev server' -> + invoke \setup + say '' run 'lib/server/server.co' task \build 'Build coco sources' -> - invoke \link - invoke \update_version + invoke \setup coco <[ -bjc package.co ]> task \test 'Rebuild test files and run tests' -> - invoke \link + invoke \setup tests = glob.globSync 'test/*.co' tests.forEach (file) -> js = file.replace '.co', '.js' @@ -31,6 +45,6 @@ task \test 'Rebuild test files and run tests' -> sh 'expresso' task \clean 'Clean up environment and artifacts' -> - remove [MODULE_LINK, 'var', 'tmp/dest'], true + remove [MODULE_LINK, EXPRESS_DEP_MIME, 'var', 'tmp/dest'], true diff --git a/lib/server/index.js b/lib/server/index.js index 0f3d9c4..37f5743 100755 --- a/lib/server/index.js +++ b/lib/server/index.js @@ -3,3 +3,6 @@ var coco = require('coco') , coffee = require('coffee-script') , server = require('./server') ; + +module.exports = exports = server; + diff --git a/lib/server/server.co b/lib/server/server.co index a7dc7ab..d21c21d 100755 --- a/lib/server/server.co +++ b/lib/server/server.co @@ -5,25 +5,23 @@ path = require 'path' {exec, spawn} = require 'child_process' _ = require 'underscore' +mime = require 'mime' express = require 'express' compiler = require 'connect-compiler-extras' -stylus = require 'stylus' -nib = require 'nib' - - ### Config PORT = 8081 -CWD = process.cwd() +CWD = process.cwd() WWW = "#CWD/www" VAR = "#CWD/var" STATIC = "#CWD/static" -NODE_ENV = process.env.NODE_ENV or 'development' +NODE_ENV = process.env.NODE_ENV or 'development' +LOG_LEVEL = if NODE_ENV is 'development' then 'INFO' else 'WARN' VERSION = 'dev' @@ -37,13 +35,16 @@ VERSION = stdout.trim! ### Setup -exports.app = app = express.createServer() -app.listen PORT +app = express.createServer() -console.log "starting Kraken dev server (port=#PORT, env=#NODE_ENV, version=#VERSION)" -console.log "========================================================================" +app.start = -> + console.log "starting Kraken dev server (port=#PORT, env=#NODE_ENV, version=#VERSION)" + console.log "========================================================================" + app.listen PORT app.configure -> + mime.define 'text/plain' : <[ jade co styl stylus ]> + app.set 'views', WWW app.set 'view engine', 'jade' app.set 'view options', { @@ -55,18 +56,16 @@ app.configure -> STATIC : STATIC } import require './view-helpers' - # app.use express.logger() + app.use express.logger() if LOG_LEVEL is 'DEBUG' app.use express.bodyParser() app.use express.methodOverride() - log_level = 'INFO' - app.use compiler do src : WWW dest : VAR enabled : <[ stylus coco pyyaml ]> options : stylus : { nib:true, include:"#WWW/css" } - log_level : log_level + log_level : LOG_LEVEL # wrap modules in commonjs closure for browser app.use compiler do @@ -74,7 +73,7 @@ app.configure -> src : [ VAR, WWW, STATIC ] dest : VAR options : commonjs : drop_path_parts:1 - log_level : log_level + log_level : LOG_LEVEL app.use require('browserify') do mount : '/vendor/browserify.js' @@ -83,7 +82,9 @@ app.configure -> app.use express.static WWW app.use express.static VAR app.use express.static STATIC + app.use app.router + app.use express.errorHandler do dumpExceptions : true showStack : true @@ -98,3 +99,15 @@ app.get '/:page', (req, res) -> res.render req.params.page + +exports import { + CWD, WWW, VAR, STATIC, + PORT, LOG_LEVEL, NODE_ENV, VERSION, + app, module, require, +} + + +mainfile = path.basename require.main?.filename +if require.main is module or 'Cokefile' is mainfile + app.start() + diff --git a/lib/underscore/array.co b/lib/underscore/array.co new file mode 100644 index 0000000..0d5f1d3 --- /dev/null +++ b/lib/underscore/array.co @@ -0,0 +1,47 @@ +_ = require 'underscore' + +I = -> it +defined = (o) -> o? + +_array = do + /** + * Transforms an Array of tuples (two-element Arrays) into an Object, such that for each + * tuple [k, v]: + * result[k] = v if filter(v) + * @param {Array} o A collection. + * @param {Function} [filter=defined] Optional filter function. If omitted, will + * exclude `undefined` and `null` values. + * @return {Object} Transformed result. + */ + generate : (o, filter=defined) -> + _.reduce do + o + (acc, [k, v], idx) -> + if k and filter(v, k) + acc[k] = v + acc + {} + + /** + * As {@link _.generate}, but first transforms the collection using `fn`. + * @param {Array} o A collection. + * @param {Function} [fn=I] Transformation function. Defaults to the identity transform. + * @param {Function} [filter=defined] Optional filter function. If omitted, will + * exclude `undefined` and `null` values. + * @return {Object} Transformed result. + */ + synthesize : (o, fn=I, filter=defined) -> + _array.generate _.map(o, fn), filter + + + /** + * Symmetric Difference + */ + xor : (a, b) -> + a = _.values a + b = _.values b + return _.union _.difference(a,b), _.difference(b,a) + + + +exports import _array diff --git a/lib/underscore/index.co b/lib/underscore/index.co index 2f3bc94..5ea04c0 100644 --- a/lib/underscore/index.co +++ b/lib/underscore/index.co @@ -1,8 +1,10 @@ _ = require 'underscore' _.str = require 'underscore.string' _.mixin _.str.exports() -_.mixin require 'kraken/underscore/underscore.array' -_.mixin require 'kraken/underscore/underscore.object' + +_.mixin require 'kraken/underscore/array' +_.mixin require 'kraken/underscore/object' +_.mixin require 'kraken/underscore/string' module.exports = exports = _ diff --git a/lib/underscore/object.co b/lib/underscore/object.co new file mode 100644 index 0000000..621fa14 --- /dev/null +++ b/lib/underscore/object.co @@ -0,0 +1,60 @@ +_ = require 'underscore' + +_obj = do + /** + * Searches a heirarchical object for a given subkey specified in dotted-property syntax. + * @param {Object} base The object to serve as the root of the property-chain. + * @param {Array|String} chain The property-chain to lookup. + * @retruns {null|Object} If found, the object is of the form `{ key: Qualified key name, obj: Parent object of key, val: Value at obj[key] }`. Otherwise `null`. + */ + getNestedMeta = (obj, chain) -> + chain = chain.split('.') if typeof chain is 'string' + return _.reduce do + chain + (current, key, idx, chain) -> + return null unless current? + + if idx is chain.length-1 + return + obj : current + key : key + val : current[key] + + if key in current + current[key] + else + null + obj + + /** + * Searches a heirarchical object for a given subkey specified in dotted-property syntax. + * @param {Object} obj The object to serve as the root of the property-chain. + * @param {Array|String} chain The property-chain to lookup. + * @param {Any} def Value to return if lookup fails. + * @retruns {null|Object} If found, returns the value, and otherwise `default`. + */ + getNested = (obj, chain, def=null) -> + meta = _obj.getNestedMeta obj, chain + if meta + meta.val + else + def + + /** + * Searches a heirarchical object for a given subkey specified in + * dotted-property syntax, setting it with the provided value if found. + * @param {Object} obj The object to serve as the root of the property-chain. + * @param {Array|String} chain The property-chain to lookup. + * @param {Any} value The value to set. + * @retruns {null|Object} If found, returns the old value, and otherwise `null`. + */ + setNested = (obj, chain, value) -> + meta = _obj.getNestedMeta obj, chain + if meta + meta.obj[meta.key] = value + meta.val + else + null + + +exports import _obj diff --git a/lib/underscore/string.co b/lib/underscore/string.co new file mode 100644 index 0000000..122d492 --- /dev/null +++ b/lib/underscore/string.co @@ -0,0 +1,36 @@ +_ = require 'underscore' +_str = require 'underscore.string' + +_string = do + + drop : (s, ...parts) -> + do + starting = s + for part of parts + s .= slice part.length if _str.startsWith s, part + s .= slice 0, s.length-part.length if _str.endsWith s, part + while s and s is not starting + s + + ldrop : (s, ...parts) -> + do + starting = s + for part of parts + s .= slice part.length if _str.startsWith s, part + while s and s is not starting + s + + rdrop : (s, ...parts) -> + do + starting = s + for part of parts + s .= slice 0, s.length-part.length if _str.endsWith s, part + while s and s is not starting + s + + +_string import do + dropLeft : _string.ldrop + dropRight : _string.rdrop + +exports import _string diff --git a/lib/underscore/underscore.array.co b/lib/underscore/underscore.array.co deleted file mode 100644 index 0d5f1d3..0000000 --- a/lib/underscore/underscore.array.co +++ /dev/null @@ -1,47 +0,0 @@ -_ = require 'underscore' - -I = -> it -defined = (o) -> o? - -_array = do - /** - * Transforms an Array of tuples (two-element Arrays) into an Object, such that for each - * tuple [k, v]: - * result[k] = v if filter(v) - * @param {Array} o A collection. - * @param {Function} [filter=defined] Optional filter function. If omitted, will - * exclude `undefined` and `null` values. - * @return {Object} Transformed result. - */ - generate : (o, filter=defined) -> - _.reduce do - o - (acc, [k, v], idx) -> - if k and filter(v, k) - acc[k] = v - acc - {} - - /** - * As {@link _.generate}, but first transforms the collection using `fn`. - * @param {Array} o A collection. - * @param {Function} [fn=I] Transformation function. Defaults to the identity transform. - * @param {Function} [filter=defined] Optional filter function. If omitted, will - * exclude `undefined` and `null` values. - * @return {Object} Transformed result. - */ - synthesize : (o, fn=I, filter=defined) -> - _array.generate _.map(o, fn), filter - - - /** - * Symmetric Difference - */ - xor : (a, b) -> - a = _.values a - b = _.values b - return _.union _.difference(a,b), _.difference(b,a) - - - -exports import _array diff --git a/lib/underscore/underscore.object.co b/lib/underscore/underscore.object.co deleted file mode 100644 index 621fa14..0000000 --- a/lib/underscore/underscore.object.co +++ /dev/null @@ -1,60 +0,0 @@ -_ = require 'underscore' - -_obj = do - /** - * Searches a heirarchical object for a given subkey specified in dotted-property syntax. - * @param {Object} base The object to serve as the root of the property-chain. - * @param {Array|String} chain The property-chain to lookup. - * @retruns {null|Object} If found, the object is of the form `{ key: Qualified key name, obj: Parent object of key, val: Value at obj[key] }`. Otherwise `null`. - */ - getNestedMeta = (obj, chain) -> - chain = chain.split('.') if typeof chain is 'string' - return _.reduce do - chain - (current, key, idx, chain) -> - return null unless current? - - if idx is chain.length-1 - return - obj : current - key : key - val : current[key] - - if key in current - current[key] - else - null - obj - - /** - * Searches a heirarchical object for a given subkey specified in dotted-property syntax. - * @param {Object} obj The object to serve as the root of the property-chain. - * @param {Array|String} chain The property-chain to lookup. - * @param {Any} def Value to return if lookup fails. - * @retruns {null|Object} If found, returns the value, and otherwise `default`. - */ - getNested = (obj, chain, def=null) -> - meta = _obj.getNestedMeta obj, chain - if meta - meta.val - else - def - - /** - * Searches a heirarchical object for a given subkey specified in - * dotted-property syntax, setting it with the provided value if found. - * @param {Object} obj The object to serve as the root of the property-chain. - * @param {Array|String} chain The property-chain to lookup. - * @param {Any} value The value to set. - * @retruns {null|Object} If found, returns the old value, and otherwise `null`. - */ - setNested = (obj, chain, value) -> - meta = _obj.getNestedMeta obj, chain - if meta - meta.obj[meta.key] = value - meta.val - else - null - - -exports import _obj -- 1.7.0.4