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 | function normalizeName(name) {
|
7 | return name.replace('\\', '/').replace(/\.\w+$/, '');
|
8 | }
|
9 | class Resource {
|
10 | constructor(origin, name, fileName) {
|
11 | this.origin = origin;
|
12 | this.name = name;
|
13 | this.fileName = fileName;
|
14 | }
|
15 | getName() {
|
16 | return this.name;
|
17 | }
|
18 | }
|
19 | exports.Resource = Resource;
|
20 | class ResourceOrigin {
|
21 | constructor(stack, name, path) {
|
22 | this.resources = {};
|
23 | this.stack = stack;
|
24 | this.name = name;
|
25 | this.path = path;
|
26 | this.findResources();
|
27 | }
|
28 | mergeResources(target) {
|
29 | const resources = this.resources;
|
30 | for (let name in resources) {
|
31 | if (name in target) {
|
32 | continue;
|
33 | }
|
34 | target[name] = resources[name];
|
35 | }
|
36 | }
|
37 | hasResource(name) {
|
38 | return name in this.resources;
|
39 | }
|
40 | getResource(name) {
|
41 | return this.resources[name];
|
42 | }
|
43 | getName() {
|
44 | return this.name;
|
45 | }
|
46 | findResources(dir) {
|
47 | const resourceClass = this.stack.getResourceClass();
|
48 | const resourceRegExp = this.stack.getResourceRegExp();
|
49 | let path = this.path;
|
50 | if (dir) {
|
51 | path = Path.join(path, dir);
|
52 | }
|
53 | for (let fileName of FS.readdirSync(path)) {
|
54 | const fullName = Path.join(path, fileName);
|
55 | if (FS.statSync(fullName).isDirectory()) {
|
56 | this.findResources(dir ? Path.join(dir, fileName) : fileName);
|
57 | }
|
58 | else if (resourceRegExp.test(fileName)) {
|
59 | const name = normalizeName(dir ? Path.join(dir, fileName) : fileName);
|
60 | this.resources[name] = new resourceClass(this, name, fullName);
|
61 | }
|
62 | }
|
63 | }
|
64 | }
|
65 | exports.ResourceOrigin = ResourceOrigin;
|
66 | class ResourceStack {
|
67 | constructor(resourceClass, resourceRegExp) {
|
68 | this.isActive = false;
|
69 | this.origins = [];
|
70 | this.resourceClass = resourceClass;
|
71 | this.resourceRegExp = resourceRegExp || /.*/;
|
72 | }
|
73 | activate() {
|
74 | if (this.isActive) {
|
75 | return false;
|
76 | }
|
77 | this.isActive = true;
|
78 | return true;
|
79 | }
|
80 | deactivate() {
|
81 | if (!this.isActive) {
|
82 | return false;
|
83 | }
|
84 | this.isActive = false;
|
85 | return true;
|
86 | }
|
87 | getResource(name) {
|
88 | const normalizedName = normalizeName(name);
|
89 | let index = this.origins.length - 1;
|
90 | while (index >= 0) {
|
91 | const origin = this.origins[index--];
|
92 | if (origin.hasResource(normalizedName)) {
|
93 | return origin.getResource(normalizedName);
|
94 | }
|
95 | }
|
96 | throw new Error(Util.format('Cannot find resource `%s`.', name));
|
97 | }
|
98 | getAllResources() {
|
99 | const resources = {};
|
100 | let index = this.origins.length - 1;
|
101 | while (index >= 0) {
|
102 | this.origins[index--].mergeResources(resources);
|
103 | }
|
104 | return resources;
|
105 | }
|
106 | getResourceClass() {
|
107 | return this.resourceClass;
|
108 | }
|
109 | getResourceRegExp() {
|
110 | return this.resourceRegExp;
|
111 | }
|
112 | getOrigin(name) {
|
113 | for (let origin of this.origins) {
|
114 | if (origin.getName() === name) {
|
115 | return origin;
|
116 | }
|
117 | }
|
118 | }
|
119 | hasOrigin(name) {
|
120 | return !!this.getOrigin(name);
|
121 | }
|
122 | addOrigin(name, path, ignoreErrors) {
|
123 | if (this.isActive) {
|
124 | throw new Error('Cannot add origins while the resource is active.');
|
125 | }
|
126 | if (this.hasOrigin(name)) {
|
127 | throw new Error(Util.format('The origin `%s` is already registered.', name));
|
128 | }
|
129 | path = Path.resolve(path);
|
130 | if (!FS.existsSync(path)) {
|
131 | if (!ignoreErrors) {
|
132 | throw new Error(Util.format('The resource path `%s` does not exist.', path));
|
133 | }
|
134 | return;
|
135 | }
|
136 | if (!FS.statSync(path).isDirectory()) {
|
137 | if (!ignoreErrors) {
|
138 | throw new Error(Util.format('The resource path `%s` is not a directory.', path));
|
139 | }
|
140 | return;
|
141 | }
|
142 | this.origins.push(new ResourceOrigin(this, name, path));
|
143 | }
|
144 | removeOrigin(name) {
|
145 | if (this.isActive) {
|
146 | throw new Error('Cannot remove origins while the resource is active.');
|
147 | }
|
148 | let index = 0, count = this.origins.length;
|
149 | while (index < count) {
|
150 | const origin = this.origins[index];
|
151 | if (origin.getName() === name) {
|
152 | this.origins.splice(index, 1);
|
153 | count -= 1;
|
154 | }
|
155 | else {
|
156 | index += 1;
|
157 | }
|
158 | }
|
159 | }
|
160 | removeAllOrigins() {
|
161 | if (this.isActive) {
|
162 | throw new Error('Cannot remove origins while the resource is active.');
|
163 | }
|
164 | this.origins = [];
|
165 | }
|
166 | }
|
167 | exports.ResourceStack = ResourceStack;
|
168 |
|
\ | No newline at end of file |