deno.land / x / lume@v2.1.4 / core / data_loader.ts

data_loader.ts
نووسراو ببینە
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
import { posix } from "../deps/path.ts";import { isPlainObject } from "./utils/object.ts";
import type { Entry } from "./fs.ts";import type { RawData } from "./file.ts";import type Formats from "./formats.ts";
export interface Options { /** The registered file formats */ formats: Formats;}
/** * Class to load data files. */export default class DataLoader { /** List of extensions to load data files and the loader used */ formats: Formats;
constructor(options: Options) { this.formats = options.formats; }
load(entry: Entry): Promise<RawData | undefined> { if (entry.type === "directory") { return this.#loadDirectory(entry); }
return this.#loadFile(entry); }
/** Load a _data.* file */ async #loadFile(entry: Entry): Promise<RawData | undefined> { const format = this.formats.search(entry.path);
if (!format?.dataLoader) { return; }
return await entry.getContent(format.dataLoader); }
/** Load a _data directory */ async #loadDirectory(entry: Entry): Promise<RawData> { const data: RawData = {};
for await (const child of entry.children.values()) { await this.loadEntry(child, data); }
return data; }
/** * Load a data entry inside a _data directory * and append the data to the data object */ async loadEntry(entry: Entry, data: RawData) { if (entry.name.startsWith(".") || entry.name.startsWith("_")) { return; }
if (entry.type === "file") { const name = posix.basename(entry.name, posix.extname(entry.name)); const fileData = await this.#loadFile(entry) || {};
if (fileData.content && Object.keys(fileData).length === 1) { data[name] = fileData.content; } else { const target = data[name] as Record<string, unknown> | undefined; if (isPlainObject(fileData) || target) { data[name] = Object.assign(target || {}, fileData); } else { data[name] = fileData; } }
return; }
if (entry.type === "directory") { data[entry.name] = await this.#loadDirectory(entry); } }}
lume

Version Info

Tagged at
7 months ago