SyncBailHook.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Hook = require("./Hook");
  7. const HookCodeFactory = require("./HookCodeFactory");
  8. class SyncBailHookCodeFactory extends HookCodeFactory {
  9. content({ onError, onResult, resultReturns, onDone, rethrowIfPossible }) {
  10. return this.callTapsSeries({
  11. onError: (i, err) => onError(err),
  12. onResult: (i, result, next) =>
  13. `if(${result} !== undefined) {\n${onResult(
  14. result
  15. )};\n} else {\n${next()}}\n`,
  16. resultReturns,
  17. onDone,
  18. rethrowIfPossible
  19. });
  20. }
  21. }
  22. const factory = new SyncBailHookCodeFactory();
  23. const TAP_ASYNC = () => {
  24. throw new Error("tapAsync is not supported on a SyncBailHook");
  25. };
  26. const TAP_PROMISE = () => {
  27. throw new Error("tapPromise is not supported on a SyncBailHook");
  28. };
  29. const COMPILE = function(options) {
  30. factory.setup(this, options);
  31. return factory.create(options);
  32. };
  33. function SyncBailHook(args = [], name = undefined) {
  34. const hook = new Hook(args, name);
  35. hook.constructor = SyncBailHook;
  36. hook.tapAsync = TAP_ASYNC;
  37. hook.tapPromise = TAP_PROMISE;
  38. hook.compile = COMPILE;
  39. return hook;
  40. }
  41. SyncBailHook.prototype = null;
  42. module.exports = SyncBailHook;