DirectoryExistsPlugin.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. module.exports = class DirectoryExistsPlugin {
  7. constructor(source, target) {
  8. this.source = source;
  9. this.target = target;
  10. }
  11. apply(resolver) {
  12. const target = resolver.ensureHook(this.target);
  13. resolver
  14. .getHook(this.source)
  15. .tapAsync(
  16. "DirectoryExistsPlugin",
  17. (request, resolveContext, callback) => {
  18. const fs = resolver.fileSystem;
  19. const directory = request.path;
  20. fs.stat(directory, (err, stat) => {
  21. if (err || !stat) {
  22. if (resolveContext.missing) resolveContext.missing.add(directory);
  23. if (resolveContext.log)
  24. resolveContext.log(directory + " doesn't exist");
  25. return callback();
  26. }
  27. if (!stat.isDirectory()) {
  28. if (resolveContext.missing) resolveContext.missing.add(directory);
  29. if (resolveContext.log)
  30. resolveContext.log(directory + " is not a directory");
  31. return callback();
  32. }
  33. resolver.doResolve(
  34. target,
  35. request,
  36. "existing directory",
  37. resolveContext,
  38. callback
  39. );
  40. });
  41. }
  42. );
  43. }
  44. };