-
Notifications
You must be signed in to change notification settings - Fork 0
/
Views.js
41 lines (31 loc) · 1.29 KB
/
Views.js
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
"use strict";
const Request = require("./lib/Request");
/**
* Registers the plugin and configures the view directory.
* @param {Pathlike} Pathlike A directory that contains all the views of the server.
* @param {Pathlike} [Headers] An optional path for header bindings.
* @returns {Function} A Serverful extension.
*/
module.exports = (Content, Headers = undefined) => {
if (typeof Content !== "string") {
throw new TypeError("Content path should be a type of String.");
}
const HeaderMap = new Map();
if (Headers) {
if (typeof Headers !== "string") throw new TypeError("Header path should be a type of String.");
const FS = require("fs");
const Path = require("path");
for (const Header of FS.readdirSync(Headers)) {
const Destination = FS.readFileSync(Path.join(Headers, Header), "utf-8");
HeaderMap.set(Header.split(".").shift(), Destination);
}
}
return function ServerfulExtension (Server) {
const BaseRequest = Server.Managers.get("Request");
const ExtendedRequest = Request(BaseRequest, Content, HeaderMap);
Server.Managers.set("Request", ExtendedRequest);
const Watcher = require("./lib/Watcher");
Watcher(Content, HeaderMap);
return true;
}
};