DirectoryExistsPlugin.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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.getHook(this.source).tapAsync("DirectoryExistsPlugin", (request, resolveContext, callback) => {
  14. const fs = resolver.fileSystem;
  15. const directory = request.path;
  16. fs.stat(directory, (err, stat) => {
  17. if(err || !stat) {
  18. if(resolveContext.missing) resolveContext.missing.add(directory);
  19. if(resolveContext.log) resolveContext.log(directory + " doesn't exist");
  20. return callback();
  21. }
  22. if(!stat.isDirectory()) {
  23. if(resolveContext.missing) resolveContext.missing.add(directory);
  24. if(resolveContext.log) resolveContext.log(directory + " is not a directory");
  25. return callback();
  26. }
  27. resolver.doResolve(target, request, "existing directory", resolveContext, callback);
  28. });
  29. });
  30. }
  31. };