Skip to content

Commit

Permalink
Move the onus of writing a File to the file itself and not on the col…
Browse files Browse the repository at this point in the history
…lection.

Now a collection is only involved in writing its CollectionPages.
  • Loading branch information
hswolff committed Apr 1, 2016
1 parent d4a0897 commit c5a8efe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import log from '../lib/log';
import Yarn from '../lib';

export default function() {
const id = log.startActivity('building\t\t');
const id = log.startActivity('building\t\t\t');
console.log('');

var yarn = new Yarn();
Expand Down
20 changes: 2 additions & 18 deletions lib/collection/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import isNumber from 'lodash/isNumber';
import isString from 'lodash/isString';
import sortBy from 'lodash/sortBy';
import some from 'lodash/some';
import each from 'lodash/each';

import log from '../log';
import filter from './filter';
Expand Down Expand Up @@ -281,29 +280,14 @@ export default class CollectionBase {
}

/**
* Writes both files and pages that are in this collection.
* Writes every page in this collection.
* @param {Object} siteData Site wide data that is shared on all rendered
* files.
* pages.
* @return {Promise}
*/
async write(siteData) {
let result;

let filePromises = [];
// If we're writing individual files then write them.
if (this.files) {
each(this.files, file => {
filePromises.push(this.writeFile(file, siteData));
});
}

try {
result = await Promise.all(filePromises);
} catch (e) {
log.error(e.stack);
throw e;
}

let pagePromises = [];
// Write CollectionPage files.
if (this.pages.length) {
Expand Down
33 changes: 33 additions & 0 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import isString from 'lodash/isString';
import log from './log';
import Url from './url';
import Parse from './parse';
import Plugin from './plugin';
import {
renderMarkdown,
} from './markdown';
Expand All @@ -16,6 +17,9 @@ import {
renderTemplateString,
TemplateErrorMessage,
} from './template';
import {
renderAndWriteFileWithPlugins,
} from './render';

export default class File {
constructor(filePath = '', getConfig = () => {}) {
Expand Down Expand Up @@ -220,4 +224,33 @@ export default class File {
return result;
}

/**
* Writes a given File object to the file system.
* @param {Object} siteData Site wide data.
* @return {Array.<Promise>} Array of promises.
*/
write(siteData) {
let promises = [];

for (let collection of this.collections) {
if (isUndefined(collection.template) && !collection.metadataFiles) {
log.error('No template found when trying to write file in Collection ' +
`${collection.id} for ${this.id}`);
return;
}

let promise = renderAndWriteFileWithPlugins(
this,
collection.template,
siteData,
Plugin.Event.file.beforeRender,
Plugin.Event.file.afterRender
);

promises.push(promise);
}

return promises;
}

}
30 changes: 21 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Promise from 'bluebird';
import values from 'lodash/values';
import each from 'lodash/each';
import reduce from 'lodash/reduce';
import map from 'lodash/map';
import isNil from 'lodash/isNil';
import moment from 'moment';
Expand Down Expand Up @@ -121,7 +122,7 @@ export default class Yarn {
}

loadPlugins() {
const id = log.startActivity('Loading plugins.\t');
const id = log.startActivity('Loading plugins.\t\t');

// Built-in plugins.
Plugin.loadFromPackageJson(
Expand All @@ -137,7 +138,7 @@ export default class Yarn {
}

async readFiles() {
const id = log.startActivity('Reading files.\t\t');
const id = log.startActivity('Reading files.\t\t\t');

const configPathSource = this.config.get('path.source');

Expand Down Expand Up @@ -201,7 +202,7 @@ export default class Yarn {
let promise;

// Reading theme files.
const id = log.startActivity('Reading theme files.\t');
const id = log.startActivity('Reading theme files.\t\t');
try {
promise = await this.theme.read();
} catch (e) {
Expand Down Expand Up @@ -372,9 +373,7 @@ export default class Yarn {
async cleanDestination() {
let promise;

const id = log.startActivity(
'Cleaning destination.\t'
);
const id = log.startActivity('Cleaning destination.\t\t');
try {
promise = await Promise.fromCallback(cb => {
rimraf(this.config.get('path.destination'), cb);
Expand All @@ -396,7 +395,7 @@ export default class Yarn {
let promise;

// Write theme static files.
const id = log.startActivity('Writing theme files.\t');
const id = log.startActivity('Writing theme files.\t\t');
try {
promise = await this.theme.write();
} catch (e) {
Expand All @@ -422,15 +421,28 @@ export default class Yarn {

await this.writeThemeFiles();

const id = log.startActivity('Writing files.\t\t');
let fileActivityId = log.startActivity('Writing files.\t\t\t');
let filePromises;
try {
filePromises = reduce(this.files, (result, file) => {
return result.concat(file.write(this.data));
}, []);

promise = await Promise.all(filePromises);
} catch (e) {
log.error(e.stack);
}
log.endActivity(fileActivityId);

let collectionActivityId = log.startActivity('Writing collection pages.\t');
try {
promise = await Promise.all(map(this.collections, collection => {
return collection.write(this.data);
}));
} catch (e) {
log.error(e.stack);
}
log.endActivity(id);
log.endActivity(collectionActivityId);

return promise;
}
Expand Down

0 comments on commit c5a8efe

Please sign in to comment.