Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Make a sorted array of components available in project metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Jul 29, 2011
1 parent 2f60951 commit 0fe2847
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
13 changes: 9 additions & 4 deletions bin/selleck
Expand Up @@ -264,7 +264,12 @@ if (options.server) {
});
});

project.components = components;
project.meta.components = util.values(components).sort(function (a, b) {
var aName = a.meta.displayName.toLowerCase(),
bName = b.meta.displayName.toLowerCase();

return aName < bName ? -1 : (aName > bName ? 1 : 0);
});

generateProject();
});
Expand All @@ -291,12 +296,12 @@ if (options.server) {
function generateComponents(err) {
if (err) { return finish(err); }

var toGenerate = util.size(project.components);
var toGenerate = util.size(components);

if (!toGenerate) { return finish(); }

// Generate component docs.
util.each(project.components, function(component) {
util.each(components, function(component) {
var path = component.path;

log('Generating component docs for ' + path, 'info');
Expand Down Expand Up @@ -329,7 +334,7 @@ if (options.server) {
project : util.merge(project.meta, options.overrideMeta)
};

util.each(project.components, function (component) {
util.each(components, function (component) {
var data = util.merge(component.meta);

delete data.componentDefaults;
Expand Down
12 changes: 11 additions & 1 deletion lib/selleck.js
Expand Up @@ -93,6 +93,12 @@ function findDocs(dir, docs) {
return docs;
}

// TODO: implement a proper ignore list someday.
if (path.basename(dir) === 'node_modules') {
log('Ignoring node_modules dir: ' + dir, 'info');
return docs;
}

if (isComponentDirectory(dir)) {
docs.components.push({path: dir});
} else if (isProjectDirectory(dir)) {
Expand Down Expand Up @@ -327,7 +333,7 @@ function prepare(inDir, options, callback) {
// - project metadata
// - theme metadata (lowest precedence)

if (options.component && options.meta.componentDefaults) {
if (options.component && options.meta && options.meta.componentDefaults) {
meta = options.meta.componentDefaults;
}

Expand All @@ -353,6 +359,10 @@ function prepare(inDir, options, callback) {
// else.
util.mix(options.meta, options.overrideMeta);

if (options.overrideMeta && options.overrideMeta.componentDefaults) {
util.mix(options.meta, options.overrideMeta.componentDefaults);
}

// Set a default asset path if one isn't specified in the metadata.
if (!options.meta.projectAssets) {
options.meta.projectAssets = options.component ? '../assets' : 'assets';
Expand Down
39 changes: 29 additions & 10 deletions lib/server.js
Expand Up @@ -20,14 +20,23 @@ module.exports = function (config) {
docs = selleck.findDocs(config.rootPath, docs);
projectPath = docs.project && docs.project.path;

// Gather the names of all components.
docs.components.forEach(function (component) {
var path = component.path,
meta = selleck.getMetadata(path, 'component');
// Initial pass to load all component info.
docs.components.forEach(function(component, index) {
var path = component.path;

selleck.prepare(path, {
component: true,
index : index,
path : path
}, function (err, result) {
if (err) { throw err; }

if (result.meta.examples) {
result.meta.hasOwnExamples = true;
}

if (meta.name) {
components[meta.name] = path;
}
components[result.meta.name] = result;
});
});

app.configure(function () {
Expand All @@ -53,7 +62,6 @@ module.exports = function (config) {
var component = req.params[1],
filePath = req.params[2],
fullPath = req.params[0],
root = component ? components[component] || projectPath : projectPath,
paths = [],
foundPath, testPath;

Expand All @@ -63,7 +71,7 @@ module.exports = function (config) {
// (if any), then project, and finally theme. First one that matches
// wins.
if (component && components[component]) {
paths.push(fsPath.join(components[component], 'assets', filePath));
paths.push(fsPath.join(components[component].path, 'assets', filePath));
}

paths.push(fsPath.join(projectPath, 'assets', fullPath));
Expand Down Expand Up @@ -99,6 +107,13 @@ module.exports = function (config) {
selleck.prepare(projectPath, options, function (err, options, compiled) {
if (err) { return next(err); }

options.meta.components = util.values(components).sort(function (a, b) {
var aName = a.meta.displayName.toLowerCase(),
bName = b.meta.displayName.toLowerCase();

return aName < bName ? -1 : (aName > bName ? 1 : 0);
});

var view = new selleck.View(options.meta, pageName),
layout = options.layouts[view.layout];

Expand All @@ -115,7 +130,7 @@ module.exports = function (config) {
// Component docs.
app.get(/^\/([\w\-]+)(?:\/|\/([\w\-]+)\.html)$/, function (req, res, next) {
var component = req.params[0],
docPath = components[component],
docPath = components[component] && components[component].path,
options = {component: true},
pageName = req.params[1] || 'index';

Expand Down Expand Up @@ -143,6 +158,10 @@ module.exports = function (config) {
selleck.prepare(docPath, options, function (err, options, compiled) {
if (err) { return next(err); }

if (options.meta.examples) {
options.meta.hasOwnExamples = true;
}

var view = new selleck.ComponentView(options.meta, pageName),
layout = options.layouts[view.layout];

Expand Down
18 changes: 18 additions & 0 deletions lib/util.js
Expand Up @@ -119,6 +119,24 @@ function size(obj) {
}
exports.size = size;

/**
Returns an array of values in _obj_.
@method values
@param {Array|Object} obj Array or object.
@return {Array} Values.
**/
function values(obj) {
var result = [];

each(obj, function (value) {
result.push(value);
});

return result;
}
exports.values = values;

/**
Creates a stack for multiple callback management:
Expand Down

0 comments on commit 0fe2847

Please sign in to comment.