-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
source.ts
242 lines (196 loc) Β· 5.7 KB
/
source.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { basename, dirname, join } from "../deps/path.ts";
import { concurrent, normalizePath } from "./utils.ts";
import { Directory, Page } from "./filesystem.ts";
import type { DataLoader, PageLoader, Reader } from "../core.ts";
export interface Options {
dataLoader: DataLoader;
pageLoader: PageLoader;
reader: Reader;
}
/**
* Scan and load files from the source folder
* with the data, pages, assets and static files
*/
export default class Source {
/** The root of the src directory */
root?: Directory;
/** Filesystem reader to scan folders */
reader: Reader;
/** To load all _data files */
dataLoader: DataLoader;
/** To load all resources (HTML pages and assets) */
pageLoader: PageLoader;
/** The list of paths to ignore */
ignored = new Set<string>();
constructor(options: Options) {
this.pageLoader = options.pageLoader;
this.dataLoader = options.dataLoader;
this.reader = options.reader;
}
/**
* Refresh the cache
* Used on update files
*/
clearCache() {
this.root?.refreshCache();
}
addIgnoredPath(path: string) {
this.ignored.add(join("/", path));
}
/** Returns all pages found */
getPages(...filters: ((page: Page) => boolean)[]): Page[] {
if (!this.root) {
return [];
}
return [...this.root.getPages()].filter((page) =>
filters.every((filter) => filter(page))
);
}
/** Load all sources */
load() {
this.root = new Directory({ path: "/" });
return this.#loadDirectory(this.root);
}
/** Update a file */
async update(file: string): Promise<void> {
// Check if the file is in the list of ignored paths
for (const path of this.ignored) {
if (file === path || file.startsWith(join(path, "/"))) {
return;
}
}
const normalized = normalizePath(file);
// It's inside a _data file or directory
if (/\/_data(?:\.\w+$|\/)/.test(normalized)) {
return await this.#updateFile(normalized);
}
// Any path segment starts with _ or .
if (normalized.includes("/_") || normalized.includes("/.")) {
return;
}
// Default
return await this.#updateFile(normalized);
}
/** Return the File or Directory of a path */
getFileOrDirectory(path: string): Directory | Page | undefined {
let result: Directory | Page | undefined = this.root;
path.split("/").forEach((name) => {
if (!name || !result) {
return;
}
if (result instanceof Directory) {
result = result.dirs.get(name) || result.pages.get(name);
}
});
return result;
}
/** Loads a directory recursively */
#loadDirectory(directory: Directory) {
return concurrent(
this.reader.readDir(directory.src.path),
(entry) => this.#loadEntry(directory, entry),
);
}
/** Reloads a file */
async #updateFile(file: string) {
const entry = {
name: basename(file),
isFile: true,
isDirectory: false,
isSymlink: false,
};
// Is a file inside a _data directory
if (file.includes("/_data/")) {
const [dir, remain] = file.split("/_data/", 2);
const directory = await this.#getOrCreateDirectory(dir);
const path = dirname(remain).split("/").filter((name: string) =>
name && name !== "."
);
let data = directory.data as Record<string, unknown>;
for (const name of path) {
if (!(name in data)) {
data[name] = {};
}
data = data[name] as Record<string, unknown>;
}
return await this.dataLoader.loadEntry(
join(dirname(file)),
entry,
data,
);
}
const directory = await this.#getOrCreateDirectory(dirname(file));
await this.#loadEntry(directory, entry);
}
/** Get an existing directory. Create it if it doesn't exist */
async #getOrCreateDirectory(path: string): Promise<Directory> {
let dir: Directory;
if (this.root) {
dir = this.root;
} else {
dir = this.root = new Directory({ path: "/" });
await this.#loadData(dir);
}
for (const name of path.split("/")) {
if (!name) {
continue;
}
if (dir.dirs.has(name)) {
dir = dir.dirs.get(name)!;
continue;
}
dir = dir.createDirectory(name);
await this.#loadData(dir);
}
return dir;
}
/** Load an entry from a directory */
async #loadEntry(directory: Directory, entry: Deno.DirEntry) {
if (entry.isSymlink || entry.name.startsWith(".")) {
return;
}
const path = join(directory.src.path, entry.name);
if (this.ignored.has(path)) {
return;
}
// It's a _data file or directory
if (entry.name === "_data" || /^_data\.\w+$/.test(entry.name)) {
directory.addData(await this.dataLoader.load(path) || {});
return;
}
// Ignore entries starting with _
if (entry.name.startsWith("_")) {
return;
}
if (entry.isFile) {
const page = (await this.pageLoader.load(path));
if (page) {
directory.setPage(entry.name, page);
} else {
directory.unsetPage(entry.name);
}
return;
}
if (entry.isDirectory) {
const subDirectory = directory.createDirectory(entry.name);
await this.#loadDirectory(subDirectory);
return;
}
}
/** Load the _data inside a directory */
async #loadData(directory: Directory) {
await concurrent(
this.reader.readDir(directory.src.path),
async (entry) => {
const path = join(directory.src.path, entry.name);
if (this.ignored.has(path)) {
return;
}
if (entry.name === "_data" || /^_data\.\w+$/.test(entry.name)) {
directory.addData(await this.dataLoader.load(path) || {});
return;
}
},
);
}
}