summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/EleventyWatch.js
blob: 22dffbec24fed05390c9dde3d65954dfbc45f9f8 (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
import { TemplatePath } from "@11ty/eleventy-utils";

import PathNormalizer from "./Util/PathNormalizer.js";

/* Decides when to watch and in what mode to watch
 * Incremental builds don’t batch changes, they queue.
 * Nonincremental builds batch.
 */

class EleventyWatch {
	constructor() {
		this.incremental = false;
		this.isActive = false;
		this.activeQueue = [];
	}

	isBuildRunning() {
		return this.isActive;
	}

	setBuildRunning() {
		this.isActive = true;

		// pop waiting queue into the active queue
		this.activeQueue = this.popNextActiveQueue();
	}

	setBuildFinished() {
		this.isActive = false;
		this.activeQueue = [];
	}

	getIncrementalFile() {
		if (this.incremental) {
			return this.activeQueue.length ? this.activeQueue[0] : false;
		}

		return false;
	}

	/* Returns the changed files currently being operated on in the current `watch` build
	 * Works with or without incremental (though in incremental only one file per time will be processed)
	 */
	getActiveQueue() {
		if (!this.isActive) {
			return [];
		} else if (this.incremental && this.activeQueue.length === 0) {
			return [];
		} else if (this.incremental) {
			return [this.activeQueue[0]];
		}

		return this.activeQueue;
	}

	_queueMatches(file) {
		let filterCallback;
		if (typeof file === "function") {
			filterCallback = file;
		} else {
			filterCallback = (path) => path === file;
		}

		return this.activeQueue.filter(filterCallback);
	}

	hasAllQueueFiles(file) {
		return (
			this.activeQueue.length > 0 && this.activeQueue.length === this._queueMatches(file).length
		);
	}

	hasQueuedFile(file) {
		if (file) {
			return this._queueMatches(file).length > 0;
		}
		return false;
	}

	hasQueuedFiles(files) {
		for (const file of files) {
			if (this.hasQueuedFile(file)) {
				return true;
			}
		}
		return false;
	}

	get pendingQueue() {
		if (!this._queue) {
			this._queue = [];
		}
		return this._queue;
	}

	set pendingQueue(value) {
		this._queue = value;
	}

	addToPendingQueue(path) {
		if (path) {
			path = PathNormalizer.normalizeSeperator(TemplatePath.addLeadingDotSlash(path));
			this.pendingQueue.push(path);
		}
	}

	getPendingQueueSize() {
		return this.pendingQueue.length;
	}

	getPendingQueue() {
		return this.pendingQueue;
	}

	getActiveQueueSize() {
		return this.activeQueue.length;
	}

	// returns array
	popNextActiveQueue() {
		if (this.incremental) {
			return this.pendingQueue.length ? [this.pendingQueue.shift()] : [];
		}

		let ret = this.pendingQueue.slice();
		this.pendingQueue = [];
		return ret;
	}
}

export default EleventyWatch;