Skip to content

Commit fdd9f38

Browse files
committed
Finished composing of pages. Removed now dead files. Added more tests.
1 parent b5e97aa commit fdd9f38

File tree

12 files changed

+458
-869
lines changed

12 files changed

+458
-869
lines changed

index.js

Lines changed: 7 additions & 715 deletions
Large diffs are not rendered by default.

lib/composer.js

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var _ = require('lodash');
44
var colors = require('colors');
55
var Promise = require('bluebird');
6+
var debug = require('./helpers/debug');
7+
var save = require('./helpers/save-file');
68

79
/**
810
* @param {SchemaDriver} schemaDriver
@@ -15,38 +17,91 @@ var Composer = function(schemaDriver, templateDriver, options) {
1517
this.schemaDriver = schemaDriver;
1618
this.templateDriver = templateDriver;
1719
this.options = options || {};
20+
this.debugLevel = options.debugLevel || 0;
21+
22+
if (!_.isArray(this.options.pages)) {
23+
throw new TypeError('No pages configured for the composer!');
24+
}
1825
};
1926

2027
/**
21-
* return {Promise}
28+
* Prepares schemas and templates, then call compose.
29+
*
30+
* @return {Promise}
2231
*/
2332
Composer.prototype.build = function() {
2433
return Promise.all([
2534
this.buildSchemas(),
2635
this.buildTemplates()
27-
]).bind(this).then(_.spread(_.bind(this.compose, this)));
36+
]).bind(this).then(_.spread(this.compose));
2837
};
2938

3039
/**
40+
* Transforms page confguration into a fully stringified template
3141
*
32-
* @param schemas
33-
* @param templates
42+
* @param {Object} schemas - keyed by ID
43+
* @param {Object} templates - keyed by file name
3444
* @return {Object}
3545
*/
3646
Composer.prototype.compose = function(schemas, templates) {
37-
// @TODO figure out how to compose pages
47+
return _.transform(this.options.pages, function(compiled, page) {
48+
if (!page.file) {
49+
throw new ReferenceError('You must specify a file for the page "' + page.title + '"');
50+
}
51+
compiled[page.file] = this.composePage(page, schemas, templates)
52+
}, {}, this);
53+
};
54+
55+
/**
56+
* Build a page. Assumes a base template file
57+
*
58+
* @param {Object} page
59+
* @param {Object} schemas
60+
* @param {Object} templates
61+
* @returns {String}
62+
*/
63+
Composer.prototype.composePage = function(page, schemas, templates) {
64+
this._debug(2, 'Building page: %s', page.title);
65+
return templates.base(this.getPageTemplateData.apply(this, arguments));
66+
};
67+
68+
/**
69+
* Build template data object
70+
*
71+
* @param {Object} page
72+
* @param {Object} schemas
73+
* @param {Object} templates
74+
* @returns {{page: {Object}, navigation: {currentPage: {Object}, allPages: {Array}}}
75+
*/
76+
Composer.prototype.getPageTemplateData = function(page, schemas, templates) {
77+
// Map schema IDs to the full schemas for data access in the templates
78+
_.each(page.sections, function(section) {
79+
section.schemas = _.filter(schemas, function(schema) {
80+
return _.contains(section.schemas, schema.id);
81+
});
82+
});
83+
84+
return {
85+
page: page,
86+
navigation: {
87+
currentPage: page,
88+
allPages: this.options.pages
89+
}
90+
};
3891
};
3992

4093
/**
4194
* @param {Object} files
95+
* @returns {Promise}
4296
*/
4397
Composer.prototype.write = function(files) {
44-
// @TODO file contents keyed by path, relatieve to root of executing directory
98+
return Promise.all(_.map(files, _.partial(save, this.options.destination || 'dist')));
4599
}
46100

47101
/**
102+
* Reads and resolves schemas, then applies transformations
48103
*
49-
* @param {Object} [options]
104+
* @returns {Object}
50105
*/
51106
Composer.prototype.buildSchemas = function () {
52107
return this.schemaDriver.fetch()
@@ -64,6 +119,9 @@ Composer.prototype.addTransform = function(Transformer) {
64119
};
65120

66121
/**
122+
* Runs the parsed schemas through transformers. Expects
123+
* the transformer to implement a `transform` method that returns
124+
* the modified schemas
67125
*
68126
* @param {Object} schemas
69127
* @returns {Object}
@@ -76,6 +134,7 @@ Composer.prototype.applyTransforms = function(schemas) {
76134
};
77135

78136
/**
137+
* Read and prepare the template files
79138
*
80139
* @param {Object} schemas
81140
* @returns {Promise}
@@ -84,9 +143,14 @@ Composer.prototype.buildTemplates = function(schemas) {
84143
return this.templateDriver.fetch();
85144
};
86145

146+
/**
147+
* @private
148+
*/
149+
Composer.prototype._debug = debug;
150+
87151
/**
88152
* @module lib/composer
89153
* @class Composer
90154
* @type {Function}
91155
*/
92-
modules.exports = Composer;
156+
module.exports = Composer;

lib/helpers/debug.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var _ = require('lodash');
4+
var DEBUG_PREFIX = 'JSON Docs: ';
5+
6+
/**
7+
* Print a message to the console if the object
8+
* this method was mixed into was configured for it.
9+
*
10+
* @module helpers/debug
11+
* @type {Function}
12+
* @param level
13+
*/
14+
module.exports = function(level) {
15+
var args = _.rest(arguments);
16+
var debugLevel = this.debugLevel;
17+
//var args = [].slice.call(arguments, 1);
18+
if (debugLevel && level <= debugLevel ) {
19+
args[0] = DEBUG_PREFIX + args[0];
20+
global.console.log.apply(global.console.log, args);
21+
}
22+
};

lib/helpers/generate-flow.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

lib/helpers/save-file.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var mkdir = require('mkdirp-then');
5+
var color = require('colors');
6+
var Promise = require('bluebird');
7+
var writeFile = Promise.promisify(require('fs').writeFile);
8+
9+
/**
10+
* Save a file
11+
*
12+
* @param {String} [destination=dist]
13+
* @param {String} contents
14+
* @param {String} fileName
15+
*/
16+
module.exports = function (destination, contents, filepath){
17+
var directory = destination + '/' + path.dirname(filepath);
18+
var fullPath = destination + '/' + filepath;
19+
20+
return mkdir(directory).then(function() {
21+
writeFile(fullPath, contents).then(function() {
22+
global.console.log('Wrote file: ' + fullPath.green);
23+
});
24+
});
25+
};

lib/helpers/save-files.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/object-definition.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ObjectDefinition.prototype.build = function(object) {
5252
if (_.isArray(object.allOf)) {
5353
_.each(object.allOf, function(schema) {
5454
// Deep extend all properties
55-
_.merge(self, this.build(schema), function(a, b) {
55+
_.merge(self, _.omit(this.build(schema), ['_original']), function(a, b) {
5656
if (_.isArray(a)) {
5757
return a.concat(b);
5858
}
@@ -80,7 +80,12 @@ ObjectDefinition.prototype.build = function(object) {
8080

8181
self.requiredProps = _.pick(self.allProps, required);
8282
self.optionalProps = _.omit(self.allProps, required);
83-
self.example = this._formatter.format(exampleExtractor.extract(object));
83+
84+
try {
85+
self.example = this._formatter.format(exampleExtractor.extract(object));
86+
} catch (e) {
87+
throw new Error('Error preparing data for object: ' + JSON.stringify(object));
88+
}
8489

8590
return self;
8691
};

lib/resolver.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var _ = require('lodash');
1010
var pointer = require('./pointer');
1111
var colors = require('colors');
1212
var deep = require('deep-get-set');
13+
var debug = require('./helpers/debug');
1314
var Resolver;
1415

1516
var INTERNAL_SCHEMA_REFERENCE_SEPARATOR = '#';
@@ -46,15 +47,7 @@ Resolver = function(schemas, options) {
4647
* @param {Number} level
4748
* @private
4849
*/
49-
Resolver.prototype._debug = function(level) {
50-
var args = _.rest(arguments);
51-
var debugLevel = this.debugLevel;
52-
//var args = [].slice.call(arguments, 1);
53-
if (debugLevel && level <= debugLevel ) {
54-
args[0] = DEBUG_PREFIX + args[0];
55-
global.console.log.apply(global.console.log, args);
56-
}
57-
}
50+
Resolver.prototype._debug = debug;
5851

5952
/**
6053
* The heavy lifter. This is a recursive method that will traverse over each key in

0 commit comments

Comments
 (0)