-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create render module which handles rendering a file with running it t…
…hrough all plugins. Move associated tests into new location.
- Loading branch information
Showing
4 changed files
with
189 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import fs from 'fs-extra'; | ||
import Promise from 'bluebird'; | ||
import Plugin from './plugin'; | ||
|
||
/** | ||
* Wrapper for writing to the file system. | ||
* @param {(File|CollectionPage)} file File or CollectionPage object. | ||
* @param {string} content Content of file. | ||
* @param {string} encoding What encoding to use when writing file. | ||
* @return {Promise} | ||
*/ | ||
export async function writeToDiskWithPlugins(file, content, encoding = 'utf8') { | ||
[file, content] = await Plugin.processEventHandlers( | ||
Plugin.Event.collection.beforeWrite, | ||
file, content | ||
); | ||
|
||
let result = await Promise.fromCallback(cb => { | ||
fs.outputFile(file.destination, content, encoding, cb); | ||
}); | ||
|
||
await Plugin.processEventHandlers( | ||
Plugin.Event.collection.afterWrite, | ||
file, content | ||
); | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Write a file to the file system. Calls all plugin events. | ||
* @param {(File|CollectionPage)} file File or CollectionPage object. | ||
* @param {string} template Which template template to use. | ||
* @param {Object} siteData Site wide template data. | ||
* @param {Plugin.Event} eventBefore Which event handler to process before | ||
* rendering the file. | ||
* @param {Plugin.Event} eventAfter Which event handler to process after | ||
* rendering the file. | ||
* @return {Promise} | ||
*/ | ||
export async function renderFileWithPlugins(file, template, siteData, | ||
eventBefore, eventAfter) { | ||
|
||
if (eventBefore) { | ||
await Plugin.processEventHandlers(eventBefore, file); | ||
} | ||
|
||
let renderedFile = file.render(template, siteData); | ||
|
||
if (eventAfter) { | ||
[file, renderedFile] = await Plugin.processEventHandlers( | ||
eventAfter, | ||
file, | ||
renderedFile | ||
); | ||
} | ||
|
||
return [file, renderedFile]; | ||
} | ||
|
||
/** | ||
* Combines renderFileWithPlugins and writeToDiskWithPlugins for conveinience. | ||
* @return {Promise} | ||
*/ | ||
export async function renderAndWriteFileWithPlugins(...args) { | ||
let [file, renderedFile] = await renderFileWithPlugins.apply(undefined, args); | ||
return writeToDiskWithPlugins( | ||
file, | ||
renderedFile | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import assert from 'power-assert'; | ||
import fs from 'fs-extra'; | ||
import sinon from 'sinon'; | ||
import fixture from '../fixture'; | ||
import Plugin from '../../lib/plugin/index.js'; | ||
const PluginAPI = Plugin.API; | ||
import CollectionBase from '../../lib/collection/base.js'; | ||
import { | ||
writeToDiskWithPlugins, | ||
renderAndWriteFileWithPlugins, | ||
} from '../../lib/render.js'; | ||
|
||
describe('render Render', function() { | ||
let sandbox; | ||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
|
||
Plugin._reset(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('renderFileWithPlugins', () => { | ||
it('calls all functions in expected order', async () => { | ||
// Disallow actually writing to disk. | ||
sandbox.stub(fs, 'outputFile', function(...args) { | ||
// Call callback. | ||
args[args.length - 1](); | ||
}); | ||
|
||
let renderContent = 'hello world'; | ||
|
||
let instance = new CollectionBase('name'); | ||
instance.files = fixture.collectionFiles().map(file => { | ||
file.render = sinon.spy(() => { | ||
return renderContent; | ||
}); | ||
return file; | ||
}); | ||
|
||
let file = instance.files[0]; | ||
let beforeSpy = sinon.spy(); | ||
let afterSpy = sinon.spy((val, val2) => [val, val2]); | ||
PluginAPI.event.file.beforeRender(beforeSpy); | ||
PluginAPI.event.file.afterRender(afterSpy); | ||
|
||
try { | ||
await renderAndWriteFileWithPlugins( | ||
file, | ||
file.template, | ||
{}, | ||
Plugin.Event.file.beforeRender, | ||
Plugin.Event.file.afterRender | ||
); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
|
||
assert.equal(beforeSpy.callCount, 1); | ||
assert.ok(beforeSpy.calledWith(file)); | ||
|
||
assert.equal(file.render.callCount, 1); | ||
assert.ok(file.render.calledWith(instance.template, {})); | ||
|
||
assert.equal(afterSpy.callCount, 1); | ||
assert.ok(afterSpy.calledWith(file, renderContent)); | ||
}); | ||
}); | ||
|
||
describe('writeToDiskWithPlugins', () => { | ||
it('calls all functions in expected order', async () => { | ||
sandbox.stub(fs, 'outputFile', function(...args) { | ||
// Call callback. | ||
args[args.length - 1](); | ||
}); | ||
|
||
let mockFile = { | ||
destination: './path/to/write/file' | ||
}; | ||
let content = 'this is the excellent content'; | ||
|
||
let beforeSpy = sinon.spy(); | ||
let afterSpy = sinon.spy((val) => val); | ||
PluginAPI.event.collection.beforeWrite(beforeSpy); | ||
PluginAPI.event.collection.afterWrite(afterSpy); | ||
|
||
try { | ||
await writeToDiskWithPlugins(mockFile, content); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
|
||
assert.equal(beforeSpy.callCount, 1); | ||
assert.ok(beforeSpy.calledWith(mockFile, content)); | ||
|
||
assert.equal(fs.outputFile.callCount, 1); | ||
assert.ok(fs.outputFile.calledWith( | ||
mockFile.destination, | ||
content, | ||
'utf8' | ||
)); | ||
|
||
assert.equal(afterSpy.callCount, 1); | ||
assert.ok(afterSpy.calledWith(mockFile, content)); | ||
|
||
assert.ok(beforeSpy.calledBefore(fs.outputFile)); | ||
assert.ok(fs.outputFile.calledBefore(afterSpy)); | ||
assert.ok(afterSpy.calledBefore(writeToDiskWithPlugins)); | ||
}); | ||
}); | ||
}); |