summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Util/FileSystemManager.js
blob: 12881d798e991c48ac1707290ce519572dca66cd (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
import path from "node:path";
import fs from "node:fs";
import { mkdir, writeFile } from "node:fs/promises";

class FileSystemManager {
	constructor(templateConfig) {
		if (!templateConfig || templateConfig.constructor.name !== "TemplateConfig") {
			throw new Error(
				"Internal error: Missing `templateConfig` or was not an instance of `TemplateConfig`.",
			);
		}
		this.templateConfig = templateConfig;
	}

	exists(pathname) {
		return this.templateConfig.existsCache.exists(pathname);
	}

	async createDirectoryForFile(filePath) {
		let dir = path.parse(filePath).dir;
		if (!dir || this.exists(dir)) {
			return;
		}

		return mkdir(dir, { recursive: true });
	}

	createDirectoryForFileSync(filePath) {
		let dir = path.parse(filePath).dir;
		if (!dir || this.exists(dir)) {
			return;
		}

		fs.mkdirSync(dir, { recursive: true });
	}

	async writeFile(filePath, content) {
		return writeFile(filePath, content);
	}

	writeFileSync(filePath, content) {
		// Note: This deliberately uses the synchronous version to avoid
		// unbounded concurrency: https://github.com/11ty/eleventy/issues/3271
		fs.writeFileSync(filePath, content);
	}
}

export { FileSystemManager };