| 1 | | // Generated by CoffeeScript 1.4.0 |
| 2 | 1 | var clone, coffeeScript, debug, fs, mkdirp, path, url; |
| 3 | | |
| 4 | 1 | coffeeScript = require('coffee-script'); |
| 5 | | |
| 6 | 1 | fs = require('fs'); |
| 7 | | |
| 8 | 1 | path = require('path'); |
| 9 | | |
| 10 | 1 | url = require('url'); |
| 11 | | |
| 12 | 1 | mkdirp = require('mkdirp'); |
| 13 | | |
| 14 | 1 | debug = require('debug')('connect-coffee-script'); |
| 15 | | |
| 16 | 1 | clone = function(src) { |
| 17 | 2 | var obj, prop, val, _i, _len, _results; |
| 18 | 2 | if (src === Object(src)) { |
| 19 | 2 | if (toString.call(src) === '[object Array]') { |
| 20 | 0 | return src.slice(); |
| 21 | | } else { |
| 22 | 2 | obj = {}; |
| 23 | 2 | _results = []; |
| 24 | 2 | for (val = _i = 0, _len = src.length; _i < _len; val = ++_i) { |
| 25 | 0 | prop = src[val]; |
| 26 | 0 | _results.push(obj[prop] = src[prop]); |
| 27 | | } |
| 28 | 2 | return _results; |
| 29 | | } |
| 30 | | } |
| 31 | | }; |
| 32 | | |
| 33 | | /* |
| 34 | | |
| 35 | | A simple connect middleware to serve CoffeeScript files. |
| 36 | | |
| 37 | | @param {Object} options |
| 38 | | @return {Function} |
| 39 | | @api public |
| 40 | | */ |
| 41 | | |
| 42 | | |
| 43 | 1 | module.exports = function(options) { |
| 44 | 2 | var baseDir, dest, src, _ref; |
| 45 | 2 | if (options == null) { |
| 46 | 0 | options = {}; |
| 47 | | } |
| 48 | 2 | if (typeof options === 'string') { |
| 49 | 0 | options = { |
| 50 | | src: options |
| 51 | | }; |
| 52 | | } |
| 53 | 2 | baseDir = options.baseDir || process.cwd(); |
| 54 | 2 | src = options.src; |
| 55 | 2 | if (!src) { |
| 56 | 0 | throw new Error('Coffeescript middleware requires "src" directory'); |
| 57 | | } |
| 58 | 2 | src = path.resolve(baseDir, src); |
| 59 | 2 | dest = options.dest ? options.dest : src; |
| 60 | 2 | dest = path.resolve(baseDir, dest); |
| 61 | 2 | if ((_ref = options.compile) == null) { |
| 62 | 2 | options.compile = function(str, options) { |
| 63 | 2 | return coffeeScript.compile(str, clone(options)); |
| 64 | | }; |
| 65 | | } |
| 66 | 2 | return function(req, res, next) { |
| 67 | 2 | var coffeePath, compile, error, jsPath, pathname; |
| 68 | 2 | if ('GET' !== req.method && 'HEAD' !== req.method) { |
| 69 | 0 | return next(); |
| 70 | | } |
| 71 | 2 | pathname = url.parse(req.url).pathname; |
| 72 | 2 | if (/\.js$/.test(pathname)) { |
| 73 | 2 | jsPath = path.join(dest, pathname); |
| 74 | 2 | coffeePath = path.join(src, pathname.replace('.js', '.coffee')); |
| 75 | 2 | error = function(err) { |
| 76 | 0 | var arg; |
| 77 | 0 | arg = 'ENOENT' === err.code ? null : err; |
| 78 | 0 | return next(arg); |
| 79 | | }; |
| 80 | 2 | compile = function() { |
| 81 | 2 | debug('read %s', jsPath); |
| 82 | 2 | return fs.readFile(coffeePath, 'utf8', function(err, str) { |
| 83 | 2 | var js; |
| 84 | 2 | if (err) { |
| 85 | 0 | return error(err); |
| 86 | | } |
| 87 | 2 | try { |
| 88 | 2 | js = options.compile(str, options); |
| 89 | | } catch (err) { |
| 90 | 0 | return next(err); |
| 91 | | } |
| 92 | 2 | debug('render %s', coffeePath); |
| 93 | 2 | return mkdirp(path.dirname(jsPath), 0x1c0, function(err) { |
| 94 | 2 | if (err) { |
| 95 | 0 | return error(err); |
| 96 | | } |
| 97 | 2 | return fs.writeFile(jsPath, js, 'utf8', next); |
| 98 | | }); |
| 99 | | }); |
| 100 | | }; |
| 101 | 2 | if (options.force) { |
| 102 | 0 | return compile(); |
| 103 | | } |
| 104 | 2 | return fs.stat(coffeePath, function(err, coffeeStats) { |
| 105 | 2 | if (err) { |
| 106 | 0 | return error(err); |
| 107 | | } |
| 108 | 2 | return fs.stat(jsPath, function(err, jsStats) { |
| 109 | 2 | if (err) { |
| 110 | 2 | if ('ENOENT' === err.code) { |
| 111 | 2 | debug('not found %s', jsPath); |
| 112 | 2 | return compile(); |
| 113 | | } else { |
| 114 | 0 | return next(err); |
| 115 | | } |
| 116 | | } else { |
| 117 | 0 | if (coffeeStats.mtime > jsStats.mtime) { |
| 118 | 0 | debug('modified %s', jsPath); |
| 119 | 0 | return compile(); |
| 120 | | } else { |
| 121 | 0 | return next(); |
| 122 | | } |
| 123 | | } |
| 124 | | }); |
| 125 | | }); |
| 126 | | } else { |
| 127 | 0 | return next(); |
| 128 | | } |
| 129 | | }; |
| 130 | | }; |