| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /** @typedef {import("./Resolver")} Resolver */
|
|---|
| 9 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
|---|
| 10 |
|
|---|
| 11 | module.exports = class DirectoryExistsPlugin {
|
|---|
| 12 | /**
|
|---|
| 13 | * @param {string | ResolveStepHook} source source
|
|---|
| 14 | * @param {string | ResolveStepHook} target target
|
|---|
| 15 | */
|
|---|
| 16 | constructor(source, target) {
|
|---|
| 17 | this.source = source;
|
|---|
| 18 | this.target = target;
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * @param {Resolver} resolver the resolver
|
|---|
| 23 | * @returns {void}
|
|---|
| 24 | */
|
|---|
| 25 | apply(resolver) {
|
|---|
| 26 | const target = resolver.ensureHook(this.target);
|
|---|
| 27 | resolver
|
|---|
| 28 | .getHook(this.source)
|
|---|
| 29 | .tapAsync(
|
|---|
| 30 | "DirectoryExistsPlugin",
|
|---|
| 31 | (request, resolveContext, callback) => {
|
|---|
| 32 | const fs = resolver.fileSystem;
|
|---|
| 33 | const directory = request.path;
|
|---|
| 34 | if (!directory) return callback();
|
|---|
| 35 | fs.stat(directory, (err, stat) => {
|
|---|
| 36 | // Combine the two miss branches: a stat failure and a
|
|---|
| 37 | // "not a directory" result share the same handling — record
|
|---|
| 38 | // the path on `missingDependencies`, log the right reason,
|
|---|
| 39 | // then bail. The error-message ternary picks the wording
|
|---|
| 40 | // that matched the failing condition.
|
|---|
| 41 | if (err || !stat || !stat.isDirectory()) {
|
|---|
| 42 | if (resolveContext.missingDependencies) {
|
|---|
| 43 | resolveContext.missingDependencies.add(directory);
|
|---|
| 44 | }
|
|---|
| 45 | if (resolveContext.log) {
|
|---|
| 46 | resolveContext.log(
|
|---|
| 47 | err || !stat
|
|---|
| 48 | ? `${directory} doesn't exist`
|
|---|
| 49 | : `${directory} is not a directory`,
|
|---|
| 50 | );
|
|---|
| 51 | }
|
|---|
| 52 | return callback();
|
|---|
| 53 | }
|
|---|
| 54 | if (resolveContext.fileDependencies) {
|
|---|
| 55 | resolveContext.fileDependencies.add(directory);
|
|---|
| 56 | }
|
|---|
| 57 | resolver.doResolve(
|
|---|
| 58 | target,
|
|---|
| 59 | request,
|
|---|
| 60 | `existing directory ${directory}`,
|
|---|
| 61 | resolveContext,
|
|---|
| 62 | callback,
|
|---|
| 63 | );
|
|---|
| 64 | });
|
|---|
| 65 | },
|
|---|
| 66 | );
|
|---|
| 67 | }
|
|---|
| 68 | };
|
|---|