summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Util/HtmlRelativeCopy.js
blob: 3059014773baff1bb2d97b6dc2b027aab0f67aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import path from "node:path";
import { TemplatePath } from "@11ty/eleventy-utils";
import isValidUrl from "./ValidUrl.js";
import { isGlobMatch } from "./GlobMatcher.js";

class HtmlRelativeCopy {
	#userConfig;
	#matchingGlobs = new Set();
	#matchingGlobsArray;
	#dirty = false;
	#paths = new Set();
	#failOnError = true;
	#copyOptions = {
		dot: false, // differs from standard passthrough copy
	};

	isEnabled() {
		return this.#matchingGlobs.size > 0;
	}

	setFailOnError(failOnError) {
		this.#failOnError = Boolean(failOnError);
	}

	setCopyOptions(opts) {
		if (opts) {
			Object.assign(this.#copyOptions, opts);
		}
	}

	setUserConfig(userConfig) {
		if (!userConfig || userConfig.constructor.name !== "UserConfig") {
			throw new Error(
				"Internal error: Missing `userConfig` or was not an instance of `UserConfig`.",
			);
		}
		this.#userConfig = userConfig;
	}

	addPaths(paths = []) {
		for (let path of paths) {
			this.#paths.add(TemplatePath.getDir(path));
		}
	}

	get matchingGlobs() {
		if (this.#dirty || !this.#matchingGlobsArray) {
			this.#matchingGlobsArray = Array.from(this.#matchingGlobs);
			this.#dirty = false;
		}

		return this.#matchingGlobsArray;
	}

	addMatchingGlob(glob) {
		if (glob) {
			if (Array.isArray(glob)) {
				for (let g of glob) {
					this.#matchingGlobs.add(g);
				}
			} else {
				this.#matchingGlobs.add(glob);
			}
			this.#dirty = true;
		}
	}

	isSkippableHref(rawRef) {
		if (
			this.#matchingGlobs.size === 0 ||
			!rawRef ||
			path.isAbsolute(rawRef) ||
			isValidUrl(rawRef)
		) {
			return true;
		}
		return false;
	}

	isCopyableTarget(target) {
		if (!isGlobMatch(target, this.matchingGlobs)) {
			return false;
		}

		return true;
	}

	exists(filePath) {
		return this.#userConfig.exists(filePath);
	}

	getAliasedPath(ref) {
		for (let dir of this.#paths) {
			let found = TemplatePath.join(dir, ref);
			if (this.isCopyableTarget(found) && this.exists(found)) {
				return found;
			}
		}
	}

	getFilePathRelativeToProjectRoot(ref, contextFilePath) {
		let dir = TemplatePath.getDirFromFilePath(contextFilePath);
		return TemplatePath.join(dir, ref);
	}

	copy(fileRef, tmplInputPath, tmplOutputPath) {
		// original ref is a full URL or no globs exist
		if (this.isSkippableHref(fileRef)) {
			return;
		}

		// Relative to source file’s input path
		let source = this.getFilePathRelativeToProjectRoot(fileRef, tmplInputPath);
		if (!this.isCopyableTarget(source)) {
			return;
		}

		if (!this.exists(source)) {
			// Try to alias using `options.paths`
			let alias = this.getAliasedPath(fileRef);
			if (!alias) {
				if (this.#failOnError) {
					throw new Error(
						"Missing input file for `html-relative` Passthrough Copy file: " +
							TemplatePath.absolutePath(source),
					);
				}

				// don’t fail on error
				return;
			}

			source = alias;
		}

		let target = this.getFilePathRelativeToProjectRoot(fileRef, tmplOutputPath);

		// We use a Set here to allow passthrough copy manager to properly error on conflicts upstream
		// Only errors when different inputs write to the same output
		// Also errors if attempts to write outside the output folder.
		this.#userConfig.emit("eleventy#copy", {
			source,
			target,
			options: this.#copyOptions,
		});
	}
}

export { HtmlRelativeCopy };