From 08e345e9e95b094ee65339d9a4825ab415ba3d7a Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Fri, 9 Oct 2015 02:39:03 -0400 Subject: [PATCH 1/2] remove load-templates, `loadView` --- example.js | 17 ++++++++++++++++- lib/item.js | 8 +++----- lib/utils/common.js | 12 ------------ lib/utils/lazy.js | 9 +++++---- lib/utils/proto.js | 5 +++++ lib/view.js | 4 ++-- lib/views.js | 33 ++------------------------------- package.json | 4 ++-- test/app.js | 1 + test/views.js | 20 -------------------- 10 files changed, 36 insertions(+), 77 deletions(-) diff --git a/example.js b/example.js index 05dbca6..567684e 100644 --- a/example.js +++ b/example.js @@ -1,9 +1,11 @@ +var path = require('path'); var templates = require('./'); var app = templates(); var green = require('ansi-green'); var yellow = require('ansi-yellow'); var red = require('ansi-red'); + app.use(function (app) { app.on('error', function (err) { console.log('app:', red(err)); @@ -75,7 +77,6 @@ app.section('articles') console.log(res.content) }) -// console.log(app.views.articles) /** * Events @@ -87,4 +88,18 @@ posts.engine('html', require('engine-base')); posts.addView('home.html', {content: 'The <%= title %> page'}) .render({title: 'Home'}, function (err, res) { console.log(res.content); + }); + + + +/** + * Loading templates + */ + +app.create('docs', {cwd: 'test/fixtures'}) + .option('renameKey', function (key) { + return path.basename(key); }) + .loadViews('templates/*.tmpl') + +console.log(app.views.docs) diff --git a/lib/item.js b/lib/item.js index 4469950..4c0f93d 100644 --- a/lib/item.js +++ b/lib/item.js @@ -39,9 +39,6 @@ function Item(item) { this.locals = item.locals || {}; this.data = item.data || {}; - this.define('_name', item._name || 'Item'); - this.define('_contents', null); - this.define('_content', null); this.define('contents', { configurable: true, enumerable: false, @@ -89,7 +86,7 @@ Item.prototype.clone = function (opts) { opts = opts || {}; if (typeof opts === 'boolean') { - opts = {deep: true}; + opts = { deep: true }; } opts.deep = opts.deep === true; @@ -126,6 +123,7 @@ Item.prototype.clone = function (opts) { */ Item.prototype.inspect = function () { + var name = this._name || 'Item'; var inspect = []; // use relative path if possible @@ -142,7 +140,7 @@ Item.prototype.inspect = function () { if (this.isStream()) { inspect.push(inspectStream(this.contents)); } - return '<' + this._name + ' ' + inspect.join(' ') + '>'; + return '<' + name + ' ' + inspect.join(' ') + '>'; }; /** diff --git a/lib/utils/common.js b/lib/utils/common.js index 095f50e..251921a 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -1,6 +1,5 @@ 'use strict'; -var fs = require('fs'); var keys = require('./keys'); /** @@ -58,17 +57,6 @@ utils.isObject = function(val) { || typeof val === 'object'; }; -/** - * Try to read a file, fail gracefully - */ - -utils.tryRead = function(fp) { - try { - return fs.readFileSync(fp); - } catch(err) {} - return null; -}; - /** * Return true if the given value is a stream. */ diff --git a/lib/utils/lazy.js b/lib/utils/lazy.js index 9718af5..5b8cb31 100644 --- a/lib/utils/lazy.js +++ b/lib/utils/lazy.js @@ -4,14 +4,14 @@ * Lazily required module dependencies */ -var lazy = require('lazy-cache')(require); +var utils = require('lazy-cache')(require); var fn = require; /** * Common utils */ -require = lazy; +require = utils; require('clone'); require('is-buffer'); require('paginationator'); @@ -33,16 +33,17 @@ require('en-route', 'router'); * Engines, templates and helpers utils */ +require('load-templates', 'load'); require('load-helpers', 'loader'); require('engine-base', 'engine'); require('engine-cache', 'Engines'); require('template-error', 'rethrow'); require('inflection', 'inflect'); require('layouts'); +require = fn; /** * Expose utils */ -require = fn; -module.exports = lazy; +module.exports = utils; diff --git a/lib/utils/proto.js b/lib/utils/proto.js index 8b9432c..e20f91b 100644 --- a/lib/utils/proto.js +++ b/lib/utils/proto.js @@ -40,6 +40,11 @@ utils.isItem = utils.isView = function(val) { */ utils.syncContents = function(view, contents) { + if (typeof view._contents === 'undefined') { + view.define('_contents', null); + view.define('_content', null); + } + if (contents === null) { view._contents = null; view._content = null; diff --git a/lib/view.js b/lib/view.js index 687ee56..b2bbece 100644 --- a/lib/view.js +++ b/lib/view.js @@ -25,8 +25,8 @@ module.exports = View; function View(view) { Item.call(this, view); - this.is('View'); - this.define('_name', 'View'); + this.define('isView', true); + this._name = 'View'; delete this.isItem; } diff --git a/lib/views.js b/lib/views.js index 56b6a88..956fe07 100644 --- a/lib/views.js +++ b/lib/views.js @@ -1,6 +1,5 @@ 'use strict'; -var path = require('path'); var plugin = require('./plugins'); var utils = require('./utils'); var Base = require('./base'); @@ -52,7 +51,8 @@ plugin.errors(Views.prototype, 'Views'); */ Views.prototype.init = function(opts) { - this.is('Collection'); + this.define('isCollection', true); + this.define('isViews', true); // decorate the instance this.use(plugin.init); @@ -196,35 +196,6 @@ Views.prototype.addList = function(list, fn) { return this; }; -/** - * Loads and create a new `View` from the file system. - * - * @param {String} `filename` - * @param {Object} `options` - * @return {Object} Returns view object - * @api public - */ - -Views.prototype.loadView = function(filename, options) { - var opts = utils.merge({cwd: ''}, this.options, options); - var View = this.get('View'); - - var extname = path.extname(filename); - var name = path.basename(filename, extname); - var fp = path.resolve(opts.cwd, name); - var ext = opts.ext || extname || ''; - var str = utils.tryRead(fp + ext) || utils.tryRead(fp); - - var view = new View({ - path: fp + ext, - name: name, - ext: ext, - content: str - }); - - return this.addView(view); -}; - /** * Get a view from the collection. * diff --git a/package.json b/package.json index 9a8b9e8..c58f6ab 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "layouts": "^0.10.6", "lazy-cache": "^0.2.3", "load-helpers": "^0.2.8", + "load-templates": "^0.9.1", "mixin-deep": "^1.1.3", "paginationator": "^0.1.2", "resolve-glob": "^0.1.2", @@ -57,7 +58,6 @@ "engine-handlebars": "^0.8.0", "event-stream": "^3.3.1", "get-value": "^1.2.1", - "globby": "^2.0.0", "gulp": "^3.9.0", "gulp-istanbul": "^0.10.0", "gulp-jshint": "^1.11.2", @@ -103,4 +103,4 @@ "paginationator" ] } -} +} \ No newline at end of file diff --git a/test/app.js b/test/app.js index be6b400..ef8e463 100644 --- a/test/app.js +++ b/test/app.js @@ -1,3 +1,4 @@ +/* deps: coveralls istanbul */ require('mocha'); require('should'); var assert = require('assert'); diff --git a/test/views.js b/test/views.js index 7057a7a..a7b8a82 100644 --- a/test/views.js +++ b/test/views.js @@ -163,26 +163,6 @@ describe('views', function () { }); }); - describe('loadView', function() { - beforeEach(function() { - collection = new Views({ - renameKey: function (key) { - return path.basename(key); - } - }); - }); - - it('should load a file and add it to `views`:', function () { - collection.loadView('test/fixtures/templates/a.tmpl'); - collection.views.should.have.property('a.tmpl'); - }); - - it('should handle files with no extension:', function () { - collection.loadView('test/fixtures/noext/license'); - collection.views.should.have.property('license'); - }); - }); - describe('addViews', function() { beforeEach(function() { collection = new Views(); From beb86e5e737d18d3db8e1ef43afa827ff187d36e Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Fri, 9 Oct 2015 02:39:33 -0400 Subject: [PATCH 2/2] 0.3.0 --- README.md | 6 +++--- package.json | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d695eb7..1b5e670 100644 --- a/README.md +++ b/README.md @@ -475,7 +475,7 @@ var item = new Item({ }); ``` -### [.clone](lib/item.js#L88) +### [.clone](lib/item.js#L85) Re-decorate Item methods after calling vinyl's `.clone()` method. @@ -1194,7 +1194,7 @@ $ npm i -d && npm test ## Code coverage -As of October 08, 2015, code coverage is 100%. +As of October 09, 2015, code coverage is 100%. ```sh Statements : 100% (1162/1162) @@ -1223,4 +1223,4 @@ Released under the MIT license. *** -_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 08, 2015._ \ No newline at end of file +_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 09, 2015._ \ No newline at end of file diff --git a/package.json b/package.json index c58f6ab..9214d96 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "templates", "description": "System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.", - "version": "0.2.8", + "version": "0.3.0", "homepage": "https://github.com/jonschlinkert/templates", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", "repository": "jonschlinkert/templates", @@ -103,4 +103,4 @@ "paginationator" ] } -} \ No newline at end of file +}