Skip to content

Commit 8f94676

Browse files
committed
Add onTransformEnd method
1 parent 840dec0 commit 8f94676

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

lib/sourcebit.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ class Sourcebit {
194194
objects: []
195195
};
196196
const contextSnapshot = cloneDeep(this.context);
197+
const onTransformEndCallbacks = [];
197198
const queue = this.pluginBlocks.reduce((queue, pluginBlock, index) => {
198199
// If the plugin hasn't been bootstrapped, we don't want to run its
199200
// transform method just yet.
@@ -209,6 +210,18 @@ class Sourcebit {
209210
return data;
210211
}
211212

213+
if (typeof plugin.onTransformEnd === "function") {
214+
onTransformEndCallbacks.push({
215+
args: {
216+
debug: this.getDebugMethodForPlugin(pluginName),
217+
getPluginContext: () => contextSnapshot[pluginName] || {},
218+
log: this.logFromPlugin.bind(this),
219+
options: this.parsePluginOptions(plugin, pluginBlock.options)
220+
},
221+
callback: plugin.onTransformEnd
222+
});
223+
}
224+
212225
return plugin.transform({
213226
data,
214227
debug: this.getDebugMethodForPlugin(pluginName),
@@ -232,6 +245,10 @@ class Sourcebit {
232245
try {
233246
const data = await queue;
234247

248+
onTransformEndCallbacks.forEach(({ args, callback }) => {
249+
callback({ ...args, data });
250+
});
251+
235252
finishTransform();
236253

237254
if (Array.isArray(data.files)) {

lib/sourcebit.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,71 @@ describe("`transform()`", () => {
888888
expect(error).toBeInstanceOf(Error);
889889
expect(callbackData).not.toBeDefined();
890890
});
891+
892+
test("calls the `onTransformEndFn` callback of each plugin, if defined, when `transform` has finished running", async () => {
893+
const onTransformEndFn = jest.fn();
894+
const plugins = [
895+
{
896+
module: {
897+
name: "sourcebit-test1",
898+
onTransformEnd: onTransformEndFn,
899+
transform: async ({ data }) => {
900+
return {
901+
...data,
902+
elements: [{ name: "Lead" }]
903+
};
904+
}
905+
}
906+
},
907+
{
908+
module: {
909+
name: "sourcebit-test2",
910+
onTransformEnd: onTransformEndFn,
911+
transform: ({ data }) => {
912+
return {
913+
...data,
914+
elements: data.elements.concat({
915+
name: "Rhenium"
916+
})
917+
};
918+
}
919+
}
920+
},
921+
{
922+
module: {
923+
name: "sourcebit-test3",
924+
onTransformEnd: onTransformEndFn,
925+
transform: ({ data }) => {
926+
return {
927+
...data,
928+
elements: data.elements.concat({
929+
name: "Osmium"
930+
})
931+
};
932+
}
933+
}
934+
}
935+
];
936+
const callback = jest.fn();
937+
const sourcebit = new Sourcebit();
938+
939+
sourcebit.onTransform = callback;
940+
sourcebit.loadPlugins(plugins);
941+
942+
await sourcebit.bootstrapAll();
943+
944+
const data = await sourcebit.transform();
945+
946+
expect(onTransformEndFn).toHaveBeenCalledTimes(3);
947+
948+
onTransformEndFn.mock.calls.forEach(call => {
949+
expect(call[0].debug).toBeInstanceOf(Function);
950+
expect(call[0].getPluginContext).toBeInstanceOf(Function);
951+
expect(call[0].log).toBeInstanceOf(Function);
952+
expect(call[0].options).toEqual({});
953+
expect(call[0].data).toEqual(data);
954+
});
955+
});
891956
});
892957

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

0 commit comments

Comments
 (0)