Coverage

72%
76
55
21

middleware.js

72%
76
55
21
LineHitsSource
1// Generated by CoffeeScript 1.4.0
21var clone, coffeeScript, debug, fs, mkdirp, path, url;
3
41coffeeScript = require('coffee-script');
5
61fs = require('fs');
7
81path = require('path');
9
101url = require('url');
11
121mkdirp = require('mkdirp');
13
141debug = require('debug')('connect-coffee-script');
15
161clone = function(src) {
172 var obj, prop, val, _i, _len, _results;
182 if (src === Object(src)) {
192 if (toString.call(src) === '[object Array]') {
200 return src.slice();
21 } else {
222 obj = {};
232 _results = [];
242 for (val = _i = 0, _len = src.length; _i < _len; val = ++_i) {
250 prop = src[val];
260 _results.push(obj[prop] = src[prop]);
27 }
282 return _results;
29 }
30 }
31};
32
33/*
34
35A simple connect middleware to serve CoffeeScript files.
36
37@param {Object} options
38@return {Function}
39@api public
40*/
41
42
431module.exports = function(options) {
442 var baseDir, dest, src, _ref;
452 if (options == null) {
460 options = {};
47 }
482 if (typeof options === 'string') {
490 options = {
50 src: options
51 };
52 }
532 baseDir = options.baseDir || process.cwd();
542 src = options.src;
552 if (!src) {
560 throw new Error('Coffeescript middleware requires "src" directory');
57 }
582 src = path.resolve(baseDir, src);
592 dest = options.dest ? options.dest : src;
602 dest = path.resolve(baseDir, dest);
612 if ((_ref = options.compile) == null) {
622 options.compile = function(str, options) {
632 return coffeeScript.compile(str, clone(options));
64 };
65 }
662 return function(req, res, next) {
672 var coffeePath, compile, error, jsPath, pathname;
682 if ('GET' !== req.method && 'HEAD' !== req.method) {
690 return next();
70 }
712 pathname = url.parse(req.url).pathname;
722 if (/\.js$/.test(pathname)) {
732 jsPath = path.join(dest, pathname);
742 coffeePath = path.join(src, pathname.replace('.js', '.coffee'));
752 error = function(err) {
760 var arg;
770 arg = 'ENOENT' === err.code ? null : err;
780 return next(arg);
79 };
802 compile = function() {
812 debug('read %s', jsPath);
822 return fs.readFile(coffeePath, 'utf8', function(err, str) {
832 var js;
842 if (err) {
850 return error(err);
86 }
872 try {
882 js = options.compile(str, options);
89 } catch (err) {
900 return next(err);
91 }
922 debug('render %s', coffeePath);
932 return mkdirp(path.dirname(jsPath), 0x1c0, function(err) {
942 if (err) {
950 return error(err);
96 }
972 return fs.writeFile(jsPath, js, 'utf8', next);
98 });
99 });
100 };
1012 if (options.force) {
1020 return compile();
103 }
1042 return fs.stat(coffeePath, function(err, coffeeStats) {
1052 if (err) {
1060 return error(err);
107 }
1082 return fs.stat(jsPath, function(err, jsStats) {
1092 if (err) {
1102 if ('ENOENT' === err.code) {
1112 debug('not found %s', jsPath);
1122 return compile();
113 } else {
1140 return next(err);
115 }
116 } else {
1170 if (coffeeStats.mtime > jsStats.mtime) {
1180 debug('modified %s', jsPath);
1190 return compile();
120 } else {
1210 return next();
122 }
123 }
124 });
125 });
126 } else {
1270 return next();
128 }
129 };
130};