summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Util/ProjectTemplateFormats.js
blob: f37040ef13c6542728ca495740aaae13bd003839 (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
import debugUtil from "debug";
const debug = debugUtil("Eleventy:Util:ProjectTemplateFormats");

class ProjectTemplateFormats {
	#useAll = {};
	#raw = {};

	#values = {}; // Set objects

	static union(...sets) {
		let s = new Set();

		for (let set of sets) {
			if (!set || typeof set[Symbol.iterator] !== "function") {
				continue;
			}
			for (let v of set) {
				s.add(v);
			}
		}

		return s;
	}

	#normalize(formats) {
		if (Array.isArray(formats)) {
			formats = "" + formats.join(",");
		}

		if (typeof formats !== "string") {
			throw new Error(
				`Invalid formats (expect String, Array) passed to ProjectTemplateFormats->normalize: ${formats}`,
			);
		}

		let final = new Set();
		for (let format of formats.split(",")) {
			format = format.trim();
			if (format && format !== "*") {
				final.add(format);
			}
		}

		return final;
	}

	isWildcard() {
		return this.#useAll.cli || this.#useAll.config || false;
	}

	/** @returns {boolean} */
	#isUseAll(rawFormats) {
		if (rawFormats === "") {
			return false;
		}

		if (typeof rawFormats === "string") {
			rawFormats = rawFormats.split(",");
		}

		if (Array.isArray(rawFormats)) {
			return rawFormats.find((entry) => entry === "*") !== undefined;
		}

		return false;
	}

	// 3.x Breaking: "" now means no formats. In 2.x and prior it meant "*"
	setViaCommandLine(formats) {
		if (formats === undefined) {
			return;
		}

		this.#useAll.cli = this.#isUseAll(formats);
		this.#raw.cli = formats;
		this.#values.cli = this.#normalize(formats);
	}

	// 3.x Breaking: "" now means no formats—in 2.x and prior it meant "*"
	// 3.x Adds support for comma separated string—in 2.x this required an Array
	setViaConfig(formats) {
		if (formats === undefined) {
			return;
		}

		// "*" is supported
		this.#useAll.config = this.#isUseAll(formats);
		this.#raw.config = formats;
		this.#values.config = this.#normalize(formats);
	}

	addViaConfig(formats) {
		if (!formats) {
			return;
		}

		if (this.#isUseAll(formats)) {
			throw new Error(
				`\`addTemplateFormats("*")\` is not supported for project template syntaxes.`,
			);
		}

		// "*" not supported here
		this.#raw.configAdd = formats;
		this.#values.configAdd = this.#normalize(formats);
	}

	getAllTemplateFormats() {
		return Array.from(ProjectTemplateFormats.union(this.#values.config, this.#values.configAdd));
	}

	getTemplateFormats() {
		if (this.#useAll.cli) {
			let v = this.getAllTemplateFormats();
			debug("Using CLI --formats='*': %o", v);
			return v;
		}

		if (this.#raw.cli !== undefined) {
			let v = Array.from(this.#values.cli);
			debug("Using CLI --formats: %o", v);
			return v;
		}

		let v = this.getAllTemplateFormats();
		debug(
			"Using configuration `templateFormats`, `setTemplateFormats()`, `addTemplateFormats()`: %o",
			v,
		);
		return v;
	}
}

export default ProjectTemplateFormats;