Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 88 additions & 107 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ var extname = path.extname;
var join = path.join;
var resolve = path.resolve;

/**
* Module exports.
* @public
*/

module.exports = View;

/**
* Initialize a new `View` with the given `name`.
*
Expand All @@ -50,132 +43,112 @@ module.exports = View;
* @public
*/

function View(name, options) {
var opts = options || {};
class View {
constructor(name, options) {
var opts = options || {};

this.defaultEngine = opts.defaultEngine;
this.ext = extname(name);
this.name = name;
this.root = opts.root;
this.defaultEngine = opts.defaultEngine;
this.ext = extname(name);
this.name = name;
this.root = opts.root;

if (!this.ext && !this.defaultEngine) {
throw new Error('No default engine was specified and no extension was provided.');
}
if (!this.ext && !this.defaultEngine) {
throw new Error('No default engine was specified and no extension was provided.');
}

var fileName = name;
var fileName = name;

if (!this.ext) {
// get extension from default engine name
this.ext = this.defaultEngine[0] !== '.'
? '.' + this.defaultEngine
: this.defaultEngine;
if (!this.ext) {
// get extension from default engine name
this.ext = this.defaultEngine[0] !== '.'
? '.' + this.defaultEngine
: this.defaultEngine;

fileName += this.ext;
}
fileName += this.ext;
}

if (!opts.engines[this.ext]) {
// load engine
opts.engines[this.ext] = require(this.ext.substr(1)).__express;
}
if (!opts.engines[this.ext]) {
// load engine
opts.engines[this.ext] = require(this.ext.substr(1)).__express;
}

// store loaded engine
this.engine = opts.engines[this.ext];
// store loaded engine
this.engine = opts.engines[this.ext];

// lookup path
this.path = this.lookup(fileName);
}
// lookup path
this.path = this.lookup(fileName);
}

/**
* Lookup view by the given `name`
*
* @param {string} name
* @private
*/
/**
* Lookup view by the given `name`
*
* @param {string} name
* @private
*/

View.prototype.lookup = function lookup(name) {
var path;
var roots = [].concat(this.root);
lookup(name) {
var path;
var roots = [].concat(this.root);

debug('lookup "%s"', name);
debug('lookup "%s"', name);

for (var i = 0; i < roots.length && !path; i++) {
var root = roots[i];
for (var i = 0; i < roots.length && !path; i++) {
var root = roots[i];

// resolve the path
var loc = resolve(root, name);
var dir = dirname(loc);
var file = basename(loc);
// resolve the path
var loc = resolve(root, name);
var dir = dirname(loc);
var file = basename(loc);

// resolve the file
path = this.resolve(dir, file);
// resolve the file
path = this.resolve(dir, file);
}

return path;
}

return path;
};
/**
* Render with the given options.
*
* @param {object} options
* @param {function} callback
* @private
*/

render(options, callback) {
debug('render "%s"', this.path);
this.engine(this.path, options, callback);
}

/**
* Render with the given options.
*
* @param {object} options
* @param {function} callback
* @private
*/
/**
* Resolve the file within the given directory.
*
* @param {string} dir
* @param {string} file
* @private
*/

View.prototype.render = function render(options, callback) {
var sync = true;
resolve(dir, file) {
var ext = this.ext;

debug('render "%s"', this.path);
// <path>.<ext>
var path = join(dir, file);
var stat = tryStat(path);

// render, normalizing sync callbacks
this.engine(this.path, options, function onRender() {
if (!sync) {
return callback.apply(this, arguments);
if (stat && stat.isFile()) {
return path;
}

// copy arguments
var args = new Array(arguments.length);
var cntx = this;
// <path>/index.<ext>
path = join(dir, basename(file, ext), 'index' + ext);
stat = tryStat(path);

for (var i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
if (stat && stat.isFile()) {
return path;
}

// force callback to be async
return process.nextTick(function renderTick() {
return callback.apply(cntx, args);
});
});

sync = false;
};

/**
* Resolve the file within the given directory.
*
* @param {string} dir
* @param {string} file
* @private
*/

View.prototype.resolve = function resolve(dir, file) {
var ext = this.ext;

// <path>.<ext>
var path = join(dir, file);
var stat = tryStat(path);

if (stat && stat.isFile()) {
return path;
}

// <path>/index.<ext>
path = join(dir, basename(file, ext), 'index' + ext);
stat = tryStat(path);

if (stat && stat.isFile()) {
return path;
}
};
}

/**
* Return a stat, maybe.
Expand All @@ -194,3 +167,11 @@ function tryStat(path) {
return undefined;
}
}

/**
* Module exports.
* @public
*/

module.exports = View;