Skip to content

Commit cd8a81a

Browse files
Merge branch 'master' into disable-cache-by-default
2 parents 0b96967 + 85f61b6 commit cd8a81a

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
Sourcebit connects to multiple data sources, for example data in a headless CMS like Contentful or Sanity, to a destination, such as a JAMstack site built using Jekyll or Hugo. The easiest way to understand how this works is to see it in action via our [video demo on YouTube](https://www.youtube.com/watch?v=BrZbWMXB4TQ).
2828

29-
[![](https://img.youtube.com/vi/fPvfeP1lzTY/0.jpg)](https://www.youtube.com/watch?v=fPvfeP1lzTY)
29+
[![](http://i3.ytimg.com/vi/fPvfeP1lzTY/maxresdefault.jpg)](https://www.youtube.com/watch?v=fPvfeP1lzTY)
3030

3131
Sourcebit works through the use of two types of plugins:
3232

lib/sourcebit.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ class Sourcebit {
200200
objects: []
201201
};
202202
const contextSnapshot = cloneDeep(this.context);
203+
const onTransformEndCallbacks = [];
203204
const queue = this.pluginBlocks.reduce((queue, pluginBlock, index) => {
204205
// If the plugin hasn't been bootstrapped, we don't want to run its
205206
// transform method just yet.
@@ -215,6 +216,18 @@ class Sourcebit {
215216
return data;
216217
}
217218

219+
if (typeof plugin.onTransformEnd === "function") {
220+
onTransformEndCallbacks.push({
221+
args: {
222+
debug: this.getDebugMethodForPlugin(pluginName),
223+
getPluginContext: () => contextSnapshot[pluginName] || {},
224+
log: this.logFromPlugin.bind(this),
225+
options: this.parsePluginOptions(plugin, pluginBlock.options)
226+
},
227+
callback: plugin.onTransformEnd
228+
});
229+
}
230+
218231
return plugin.transform({
219232
data,
220233
debug: this.getDebugMethodForPlugin(pluginName),
@@ -244,6 +257,10 @@ class Sourcebit {
244257
await this.writeFiles(data.files);
245258
}
246259

260+
onTransformEndCallbacks.forEach(({ args, callback }) => {
261+
callback({ ...args, data });
262+
});
263+
247264
if (typeof this.onTransform === "function") {
248265
this.onTransform(null, data);
249266
}

lib/sourcebit.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,71 @@ describe("`transform()`", () => {
909909
expect(error).toBeInstanceOf(Error);
910910
expect(callbackData).not.toBeDefined();
911911
});
912+
913+
test("calls the `onTransformEndFn` callback of each plugin, if defined, when `transform` has finished running", async () => {
914+
const onTransformEndFn = jest.fn();
915+
const plugins = [
916+
{
917+
module: {
918+
name: "sourcebit-test1",
919+
onTransformEnd: onTransformEndFn,
920+
transform: async ({ data }) => {
921+
return {
922+
...data,
923+
elements: [{ name: "Lead" }]
924+
};
925+
}
926+
}
927+
},
928+
{
929+
module: {
930+
name: "sourcebit-test2",
931+
onTransformEnd: onTransformEndFn,
932+
transform: ({ data }) => {
933+
return {
934+
...data,
935+
elements: data.elements.concat({
936+
name: "Rhenium"
937+
})
938+
};
939+
}
940+
}
941+
},
942+
{
943+
module: {
944+
name: "sourcebit-test3",
945+
onTransformEnd: onTransformEndFn,
946+
transform: ({ data }) => {
947+
return {
948+
...data,
949+
elements: data.elements.concat({
950+
name: "Osmium"
951+
})
952+
};
953+
}
954+
}
955+
}
956+
];
957+
const callback = jest.fn();
958+
const sourcebit = new Sourcebit();
959+
960+
sourcebit.onTransform = callback;
961+
sourcebit.loadPlugins(plugins);
962+
963+
await sourcebit.bootstrapAll();
964+
965+
const data = await sourcebit.transform();
966+
967+
expect(onTransformEndFn).toHaveBeenCalledTimes(3);
968+
969+
onTransformEndFn.mock.calls.forEach(call => {
970+
expect(call[0].debug).toBeInstanceOf(Function);
971+
expect(call[0].getPluginContext).toBeInstanceOf(Function);
972+
expect(call[0].log).toBeInstanceOf(Function);
973+
expect(call[0].options).toEqual({});
974+
expect(call[0].data).toEqual(data);
975+
});
976+
});
912977
});
913978

914979
describe("writing files", () => {

0 commit comments

Comments
 (0)