1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const FS = require("fs");
|
4 | const Path = require("path");
|
5 | const Util = require("util");
|
6 | const helpers_1 = require("./resources/helpers");
|
7 | const templates_1 = require("./resources/templates");
|
8 | const renderer_1 = require("../renderer");
|
9 | class Resources {
|
10 | constructor(theme) {
|
11 | this.isActive = false;
|
12 | this.theme = theme;
|
13 | this.templates = new templates_1.TemplateStack();
|
14 | this.layouts = new templates_1.TemplateStack();
|
15 | this.partials = new templates_1.PartialStack();
|
16 | this.helpers = new helpers_1.HelperStack();
|
17 | this.addDirectory('default', renderer_1.Renderer.getDefaultTheme());
|
18 | this.addDirectory('theme', theme.basePath);
|
19 | }
|
20 | activate() {
|
21 | if (this.isActive) {
|
22 | return false;
|
23 | }
|
24 | this.isActive = true;
|
25 | this.partials.activate();
|
26 | this.helpers.activate();
|
27 | return true;
|
28 | }
|
29 | deactivate() {
|
30 | if (!this.isActive) {
|
31 | return false;
|
32 | }
|
33 | this.isActive = false;
|
34 | this.partials.deactivate();
|
35 | this.helpers.deactivate();
|
36 | return true;
|
37 | }
|
38 | getTheme() {
|
39 | return this.theme;
|
40 | }
|
41 | addDirectory(name, path) {
|
42 | if (this.isActive) {
|
43 | throw new Error('Cannot add directories while the resource is active.');
|
44 | }
|
45 | path = Path.resolve(path);
|
46 | if (!FS.existsSync(path)) {
|
47 | throw new Error(Util.format('The theme path `%s` does not exist.', path));
|
48 | }
|
49 | if (!FS.statSync(path).isDirectory()) {
|
50 | throw new Error(Util.format('The theme path `%s` is not a directory.', path));
|
51 | }
|
52 | this.templates.addOrigin(name, Path.join(path, 'templates'), true);
|
53 | this.layouts.addOrigin(name, Path.join(path, 'layouts'), true);
|
54 | this.partials.addOrigin(name, Path.join(path, 'partials'), true);
|
55 | this.helpers.addOrigin(name, Path.join(path, 'helpers'), true);
|
56 | }
|
57 | removeDirectory(name) {
|
58 | if (this.isActive) {
|
59 | throw new Error('Cannot remove directories while the resource is active.');
|
60 | }
|
61 | this.templates.removeOrigin(name);
|
62 | this.layouts.removeOrigin(name);
|
63 | this.partials.removeOrigin(name);
|
64 | this.helpers.removeOrigin(name);
|
65 | }
|
66 | removeAllDirectories() {
|
67 | if (this.isActive) {
|
68 | throw new Error('Cannot remove directories while the resource is active.');
|
69 | }
|
70 | this.templates.removeAllOrigins();
|
71 | this.layouts.removeAllOrigins();
|
72 | this.partials.removeAllOrigins();
|
73 | this.helpers.removeAllOrigins();
|
74 | }
|
75 | }
|
76 | exports.Resources = Resources;
|
77 |
|
\ | No newline at end of file |