ResultPlugin.js 971 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Resolver")} Resolver */
  7. /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
  8. module.exports = class ResultPlugin {
  9. /**
  10. * @param {ResolveStepHook} source source
  11. */
  12. constructor(source) {
  13. this.source = source;
  14. }
  15. /**
  16. * @param {Resolver} resolver the resolver
  17. * @returns {void}
  18. */
  19. apply(resolver) {
  20. this.source.tapAsync(
  21. "ResultPlugin",
  22. (request, resolverContext, callback) => {
  23. const obj = { ...request };
  24. if (resolverContext.log)
  25. resolverContext.log("reporting result " + obj.path);
  26. resolver.hooks.result.callAsync(obj, resolverContext, err => {
  27. if (err) return callback(err);
  28. if (typeof resolverContext.yield === "function") {
  29. resolverContext.yield(obj);
  30. callback(null, null);
  31. } else {
  32. callback(null, obj);
  33. }
  34. });
  35. }
  36. );
  37. }
  38. };