Skip to content

Commit f3c34a0

Browse files
Merge pull request #15 from stackbithq/ontransform-updates
Ontransform updates
2 parents 6ff2a39 + bdd3ff5 commit f3c34a0

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

lib/sourcebit.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ class Sourcebit {
183183
objects: []
184184
};
185185
const contextSnapshot = cloneDeep(this.context);
186+
187+
this.pluginBlocks.forEach((pluginBlock, index) => {
188+
const plugin = this.pluginModules[index];
189+
const pluginName = this.getNameOfPluginAtIndex(index);
190+
191+
if (typeof plugin.onTransformStart === 'function') {
192+
plugin.onTransformStart({
193+
debug: this.getDebugMethodForPlugin(pluginName),
194+
getPluginContext: () => contextSnapshot[pluginName] || {},
195+
log: this.logFromPlugin.bind(this),
196+
options: this.parsePluginOptions(plugin, pluginBlock.options)
197+
});
198+
}
199+
});
200+
186201
const onTransformEndCallbacks = [];
187202
const queue = this.pluginBlocks.reduce((queue, pluginBlock, index) => {
188203
// If the plugin hasn't been bootstrapped, we don't want to run its
@@ -195,10 +210,6 @@ class Sourcebit {
195210
const plugin = this.pluginModules[index];
196211
const pluginName = this.getNameOfPluginAtIndex(index);
197212

198-
if (typeof plugin.transform !== 'function') {
199-
return data;
200-
}
201-
202213
if (typeof plugin.onTransformEnd === 'function') {
203214
onTransformEndCallbacks.push({
204215
args: {
@@ -211,6 +222,10 @@ class Sourcebit {
211222
});
212223
}
213224

225+
if (typeof plugin.transform !== 'function') {
226+
return data;
227+
}
228+
214229
return plugin.transform({
215230
data,
216231
debug: this.getDebugMethodForPlugin(pluginName),

lib/sourcebit.test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,12 +896,14 @@ describe('`transform()`', () => {
896896
expect(callbackData).not.toBeDefined();
897897
});
898898

899-
test('calls the `onTransformEndFn` callback of each plugin, if defined, when `transform` has finished running', async () => {
899+
test('calls the `onTransformStart` and `onTransformEnd` callbacks of each plugin, if defined, when `transform` has finished running', async () => {
900+
const onTransformStartFn = jest.fn();
900901
const onTransformEndFn = jest.fn();
901902
const plugins = [
902903
{
903904
module: {
904905
name: 'sourcebit-test1',
906+
onTransformStart: onTransformStartFn,
905907
onTransformEnd: onTransformEndFn,
906908
transform: async ({ data }) => {
907909
return {
@@ -938,6 +940,13 @@ describe('`transform()`', () => {
938940
};
939941
}
940942
}
943+
},
944+
{
945+
module: {
946+
name: 'sourcebit-test4',
947+
onTransformStart: onTransformStartFn,
948+
onTransformEnd: onTransformEndFn
949+
}
941950
}
942951
];
943952
const callback = jest.fn();
@@ -950,7 +959,15 @@ describe('`transform()`', () => {
950959

951960
const data = await sourcebit.transform();
952961

953-
expect(onTransformEndFn).toHaveBeenCalledTimes(3);
962+
expect(onTransformStartFn).toHaveBeenCalledTimes(2);
963+
expect(onTransformEndFn).toHaveBeenCalledTimes(4);
964+
965+
onTransformStartFn.mock.calls.forEach(call => {
966+
expect(call[0].debug).toBeInstanceOf(Function);
967+
expect(call[0].getPluginContext).toBeInstanceOf(Function);
968+
expect(call[0].log).toBeInstanceOf(Function);
969+
expect(call[0].options).toEqual({});
970+
});
954971

955972
onTransformEndFn.mock.calls.forEach(call => {
956973
expect(call[0].debug).toBeInstanceOf(Function);

0 commit comments

Comments
 (0)