Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Created May 4, 2018 22:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonschlinkert/a158365ababd6f6cfcd427e22a8e74a0 to your computer and use it in GitHub Desktop.
Save jonschlinkert/a158365ababd6f6cfcd427e22a8e74a0 to your computer and use it in GitHub Desktop.

collection loader

This an example collection loader for the upcoming, major refactor of templates.

Setup

Create a collection with app.create() or with new Collection().

const Collection = require('templates/lib/collection');
const collection = new Collection();
collection.use(loader());
const fs = require('fs');
const path = require('path');
const Collection = require('../lib/collection');
const collection = new Collection();
collection.use(loader());
const filter = file => file.extname === '.txt';
const contents = file => fs.readFileSync(file.path);
// collection.load(path.join(__dirname, 'fixtures'));
// collection.load(path.join(__dirname, 'fixtures'), { read: true });
// collection.load(path.join(__dirname, 'fixtures'), { recurse: true, read: true });
// collection.load(path.join(__dirname, 'fixtures'), { recurse: true, extname: '.hbs' });
collection.load(path.join(__dirname, 'fixtures'), { recurse: true, contents });
console.log(collection);
/**
* Load a directory of views.
*
* ```js
* collection.load('./foo/bar');
* collection.load('./foo/bar', { recurse: true });
* collection.load('./foo/bar', { recurse: true, extname: '.hbs' });
* const filter = file => /\.hbs$/.test(file.path);
* const contents = file => fs.readFileSync(file.path);
* collection.load('./foo/bar', { recurse: true, filter });
* collection.load('./foo/bar', { recurse: true, filter, contents });
* ```
* @name .load
* @param {object} `dir`
* @param {object} `options`
* @return {object} Returns the view, if found.
* @api public
*/
function loader() {
return function(app) {
Reflect.defineProperty(app, 'load', {
value: function(cwd, options = {}) {
const { extname, filter, contents, recurse, read } = options;
const readdir = base => {
const files = fs.readdirSync(base);
for (const filename of files) {
const fp = path.join(base, filename);
const stat = fs.statSync(fp);
if (stat.isDirectory()) {
if (recurse) readdir(fp);
continue;
}
if (extname && path.extname(filename) !== extname) {
continue;
}
const view = this.view({ path: fp, base, stat });
if (filter && filter(view) === false) {
continue;
}
if (read === true) {
view.contents = fs.readFileSync(view.path);
} else if (typeof contents === 'function') {
view.contents = contents(view);
}
this.set(view);
}
}
readdir(cwd);
return this;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment