Skip to content

Commit

Permalink
major refactor of server behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickarlt committed Jan 7, 2016
1 parent 9aced43 commit 5305f2a
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 47 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,20 @@ This project adheres to [Semantic Versioning](http://semver.org/).

[Upcoming Changes](https://github.com/patrickarlt/acetate/compare/v0.4.0...master)

## [0.4.1] - 2016-01-07

### Changed

* Pages are now always rebuilt every time they are requested when in server mode.
* Server startup is now much faster, as all pages are considered "clean" when the server starts and are rebuilt when requested.
* Small tweaks to logging and log output.


### Fixed

* Fixed memory link when watching data files.
* Pages created with `acetate.output` are no longer added multiple times.

## [0.4.1] - 2016-01-05

### Added
Expand Down
32 changes: 24 additions & 8 deletions index.js
Expand Up @@ -37,20 +37,21 @@ module.exports = function (options, callback) {
}

function buildIndex (callback) {
site.debug('server', 'rebuilding page index');
site.runExtensions(function () {
index = _.indexBy(site.pages, function (page) {
return (page.url[0] !== '/') ? '/' + page.url : page.url;
});

site.info('server', 'URL index of %s pages', Object.keys(index).length);

if (callback) {
callback();
}
});
}

function pageBuilder (request, response, next) {
site.verbose('server', 'request recived for %s', request.url);
site.info('server', 'request recived for %s', request.url);

if (request.method !== 'GET') {
next();
Expand All @@ -64,12 +65,23 @@ module.exports = function (options, callback) {
pathname = pathname + '/';
}

if (index[pathname] && index[pathname].dirty) {
index[pathname].build(function () {
next();
// handle directory indexes
pathname = pathname.replace(/index.html?$/, '');

var page = index[pathname];

if (page) {
page.dirty = true;
site.runExtensions(function () {
page.build(function () {
builtPages[pathname] = true;
next();
return;
});
});
} else {
next();
return;
}
}

Expand Down Expand Up @@ -173,9 +185,13 @@ module.exports = function (options, callback) {
var relativepath = filepath.replace(process.cwd() + path.sep, '');
if (path.basename(filepath)[0] === '_') {
invalidateNunjucksCache(filepath);
_.each(site.pages, function (page) {
page.dirty = true;
});

if(site.options.mode !== 'server') {
_.each(site.pages, function (page) {
page.dirty();
});
}

action();
} else {
site.loadPage(filepath, function (error, page) {
Expand Down
34 changes: 18 additions & 16 deletions lib/extensions/data-loader.js
Expand Up @@ -14,23 +14,25 @@ module.exports = function dataLoader (acetate, callback) {

// loop over each page
_.each(acetate.pages, function (page) {
// each local data definition
_.each(page.metadata.data, function (source, name) {
// this is the first time we have seem this source file
if (!acetate._data[source]) {
acetate._data[source] = {
fullpath: path.join(acetate.src, source),
global: false,
locals: []
};
}
if (page.dirty) {
// each local data definition
_.each(page.metadata.data, function (source, name) {
// this is the first time we have seem this source file
if (!acetate._data[source]) {
acetate._data[source] = {
fullpath: path.join(acetate.src, source),
global: false,
locals: []
};
}

// push a local definition
acetate._data[source].locals.push({
name: name,
page: page
// push a local definition
acetate._data[source].locals.push({
name: name,
page: page
});
});
});
}
});

function loadData (filepath, callback) {
Expand Down Expand Up @@ -177,7 +179,7 @@ module.exports = function dataLoader (acetate, callback) {
bindData();
});

if (acetate.options.mode === 'server' || acetate.options.mode === 'watch') {
if (acetate.options.mode === 'server' || acetate.options.mode === 'watch' && !watcher) {
var files = _.pluck(acetate._data, 'fullpath');

watcher = chokidar.watch(files, {
Expand Down
7 changes: 3 additions & 4 deletions lib/extensions/metadata.js
@@ -1,15 +1,14 @@
var _ = require('lodash');
var Minimatch = require('minimatch').Minimatch;

module.exports = function metadata (pattern, data) {
module.exports = function (pattern, data) {
pattern = new Minimatch(pattern);

return function (acetate, callback) {
acetate.verbose('extension', 'assigning %j to pages matching %s', data, pattern.pattern);
return function metadata (acetate, callback) {
acetate.debug('extension', 'assigning %j to pages matching %s', data, pattern.pattern);

_.each(acetate.pages, function (page) {
if (pattern.match(page.src)) {
acetate.debug('metadata', 'assigning %j to pages matching %s', data, page.src);
_.merge(page, data, page.metadata);
}
});
Expand Down
13 changes: 11 additions & 2 deletions lib/extensions/output.js
Expand Up @@ -4,10 +4,17 @@ var _ = require('lodash');
var async = require('async');
var page = require('../mixins/page-factory.js');

module.exports = function output (pages) {
module.exports = function (pages) {
pages = (_.isArray(pages)) ? pages : [pages];

return function (acetate, next) {
var added = false;

return function output (acetate, next) {
if (added) {
next(null, acetate);
return;
}

var createPage = page(acetate);
var templateCache = {};
var templates = _(pages).pluck('template').uniq().compact().value();
Expand All @@ -21,6 +28,8 @@ module.exports = function output (pages) {
acetate.pages.push(createPage(template, metadata));
});

added = true;

next(null, acetate);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/extensions/query.js
@@ -1,15 +1,15 @@
var _ = require('lodash');
var Minimatch = require('minimatch').Minimatch;

module.exports = function query (name, matcher, builder) {
module.exports = function (name, matcher, builder) {
var usingQuery = []; // cache of pages uses this query

if (_.isString(matcher)) {
var pattern = new Minimatch(matcher);
matcher = function (page) { return pattern.match(page.src); };
}

return function (acetate, callback) {
return function query (acetate, callback) {
acetate.verbose('extension', 'createing query %s form pages matching %s', name, matcher);

var pages = _.filter(acetate.pages, matcher);
Expand Down
2 changes: 1 addition & 1 deletion lib/extensions/transform.js
Expand Up @@ -7,7 +7,7 @@ module.exports = function transform (matcher, transformer) {
matcher = function (page) { return pattern.match(page.src); };
}

return function (acetate, callback) {
return function transform (acetate, callback) {
acetate.verbose('extension', 'transforming pages');

transformer(_.filter(acetate.pages, matcher));
Expand Down
30 changes: 24 additions & 6 deletions lib/mixins/builder.js
Expand Up @@ -5,12 +5,13 @@ module.exports = function (acetate) {
var inProgress;
var extensions = [];
var pendingExtensions;
var runningExtensions;

function use (ext) {
ext = _.isArray(ext) ? ext : [ext];

for (var i = 0; i < ext.length; i++) {
if (inProgress) {
if (inProgress || runningExtensions) {
pendingExtensions.unshift(ext[i]);
} else {
extensions.push(ext[i]);
Expand All @@ -21,7 +22,9 @@ module.exports = function (acetate) {
function build (callback) {
if (inProgress) {
acetate.info('build', 'all ready building, queuing build after current build');
acetate.on('build', build);
acetate.on('build', function () {
build(callback);
});
return;
}

Expand Down Expand Up @@ -85,7 +88,16 @@ module.exports = function (acetate) {
}

function runExtensions (callback) {
if (runningExtensions) {
acetate.info('extensions', 'already running extensions, queuing');
acetate.on('build', function () {
runExtensions(callback);
});
return;
}

acetate.verbose('extension', 'running extensions');
acetate.time('extensions');

pendingExtensions = extensions.slice(0);

Expand All @@ -97,17 +109,23 @@ module.exports = function (acetate) {
acetate.time('extension');
var currentExtension = pendingExtensions.shift();
currentExtension(acetate, function (error, acetate) {
acetate.info('extension', 'extension %s finished in %s', (currentExtension.name || 'unnamed'), acetate.timeEnd('extension'));
acetate.debug('extension', 'extension %s finished in %s', (currentExtension.name || 'unnamed'), acetate.timeEnd('extension'));
cb(error, acetate);
});
};

runningExtensions = true;

async.whilst(checkForExtensions, runNextExtension, function (error) {
if (!error) {
callback();
} else {
acetate.info('extensions', 'extensions finished running in %s', acetate.timeEnd('extensions'));

runningExtensions = false;

if (error) {
acetate.error('extensions', 'error running extensions %s', error);
}

callback();
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/mixins/loader.js
Expand Up @@ -117,7 +117,7 @@ module.exports = function (acetate) {
_metadataLines: parsed._metadataLines,
_isMarkdown: markdown,
metadata: Object.freeze(_.cloneDeep(parsed.metadata)),
dirty: true,
dirty: (acetate.options.mode !== 'server'),
fullpath: filepath,
src: relativepath,
url: dest.split(path.sep).join('/'),
Expand Down
2 changes: 1 addition & 1 deletion lib/mixins/logger.js
Expand Up @@ -27,7 +27,7 @@ function round (x, digits) {
}

module.exports = function (acetate) {
var prefix = chalk.gray('[') + chalk.blue('ACETATE') + chalk.gray(']');
var prefix = chalk.gray('[') + chalk.blue('Acetate') + chalk.gray(']');
var timers = {};

function time (label) {
Expand Down
8 changes: 3 additions & 5 deletions lib/mixins/nunjucks.js
Expand Up @@ -147,9 +147,7 @@ module.exports = function (acetate) {
}

block('highlight', function (context, body, lang) {
var code = body;
var highlighted = (lang) ? hljs.highlight(lang, code) : hljs.highlightAuto(body);

var highlighted = (lang) ? hljs.highlight(lang, body) : hljs.highlightAuto(body);
return new nunjucks.runtime.SafeString('<pre><code class="' + highlighted.language + '">' + highlighted.value.trim() + '</code></pre>');
});

Expand All @@ -163,10 +161,10 @@ module.exports = function (acetate) {

if (variable) {
acetate.info('debugger', '%s: %s is %s', context.src, variable, context[variable]);
template = '<script>if(console && console.info) {console.info("[Acetate] `{{variable}}` is", "`"+({{context | safe}})["{{variable}}"]+"`");}</script>';
template = '<script>if(console && console.info) {console.info("[Acetate] {{variable}}: ", "`"+({{context | safe}})["{{variable}}"]+"`");}</script>';
} else {
acetate.info('debugger', '%s: %s', context.src, JSON.stringify(context, null, 2));
template = '<script>if(console && console.info) {console.info("[Acetate] `{{variable}}` is", ({{context | safe}}));}</script>';
template = '<script>if(console && console.info) {console.info("[Acetate] metadata: ", ({{context | safe}}));}</script>';
}

output = acetate.nunjucks.renderString(template, {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "acetate",
"description": "Layout and templating framework for static websites.",
"version": "0.4.1",
"version": "0.4.2",
"author": "Patrick Arlt",
"bugs": {
"url": "https://github.com/patrickarlt/acetate/issues"
Expand Down

0 comments on commit 5305f2a

Please sign in to comment.