Skip to content
Myr

Merge branch 'view-reloading' into connect

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents 95e6fff, 5d94ab0 · Browse files · 4 files+82 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+82 added−2 removed

  • other:+82 added−2 removed

4 files+82 added−2 removed

modified:examples/jade/app.js+12 added
modified:lib/express/server.js+7 added
9 unmodified lines
10 */1112var sys = require('sys'),13 url = require('url'),14 view = require('./view'),15 connect = require('connect'),16 queryString = require('querystring'),17 router = require('connect/middleware/router');18
48 unmodified lines
6768Server.prototype.listen = function(port){69 this.set('env', process.env.EXPRESS_ENV || process.connectEnv.name);70 this.runConfig('any', this.set('env'));7172 // Setup view reloading73 if (this.set('reload views')) {74 view.watcher.call(this, this.set('reload views'));75 }76 77 connect.Server.prototype.listen.call(this, port);78};7980/**
85 unmodified lines
modified:lib/express/view.js+63 added-2 removed
11 unmodified lines
12var extname = require('path').extname,13 mime = require('connect/utils').mime,14 utils = require('connect/utils'),15 http = require('http'),16 sys = require('sys'),17 fs = require('fs');1819/**20 * Cache supported template engine exports to
23 unmodified lines
44 viewCache = {};45};4647/**48 * Cache view at the given `path`.49 *50 * @param {String} padth51 * @return {String}52 * @api private53 */5455function cacheView(path) {56 return viewCache[path] = fs.readFileSync(path, 'utf8');57}5859/**60 * Return view root path for the given `app`.61 *62 * @param {express.Server} app63 * @return {String}64 * @api private65 */6667function viewRoot(app) {68 return app.set('views') || process.cwd() + '/views';69}7071/**72 * Setup view reloading, checking at the given `interval`73 * which defaults to 5 minutes.74 *75 * @param {Numbers} interval76 * @api private77 */7879exports.watcher = function(interval){80 interval = interval === true81 ? 30000082 : interval;83 (function cache(dir){84 fs.readdir(dir, function(err, files){85 if (!err) {86 files.forEach(function(file){87 file = dir + '/' + file;88 fs.stat(file, function(err, stats){89 if (!err) {90 if (stats.isFile()) {91 fs.watchFile(file, { interval: interval }, function(curr, prev){92 if (curr.mtime > prev.mtime) {93 cacheView(file);94 }95 });96 } else if (stats.isDirectory()) {97 cache(file);98 }99 }100 });101 });102 }103 });104 })(viewRoot(this));105};106107/**108 * Render `view` partial with the given `options`.109 *110 * Options:111 * - `as` Variable name for each `collection` value, defaults to the view name.
70 unmodified lines
182 options = options || {};183184 // Defaults185 var self = this,186 root = viewRoot(this.app),187 ext = extname(view),188 partial = options.partial,127 root = options.root || this.app.set('views') || process.cwd() + '/views',189 layout = options.layout === undefined ? true : options.layout,190 layout = layout === true191 ? 'layout' + ext192 : layout;
38 unmodified lines
231 }232233 // Cache contents234 try {174 var str = viewCache[path] || (viewCache[path] = fs.readFileSync(path, 'utf8'));235 var str = viewCache[path] || cacheView(path);236 } catch (err) {237 return error(err);238 }239
23 unmodified lines
modified:support/jadenot shown

Its text is not shown, modified.