jcc-express-mvc 1.0.17 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jcc-express-mvc",
3
- "version": "1.0.17",
3
+ "version": "1.1.0",
4
4
  "description": "express mvc structure",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -5,18 +5,19 @@ class Template {
5
5
  #directives = {
6
6
  extends: /@extends\((?<extendPath>['"]([^'"]+)['"])\)/,
7
7
  foreach:
8
- /@foreach \s*\((?<loopvariable>[a-zA-Z0-9_-]+) in (?<loopData>[a-zA-Z0-9._-]+)\)(?<loopBody>[\s\S\t\n\r]+?)@endforeach/,
9
- if: /@if \s*\((?<ifData>[a-zA-Z0-9._-]+)\)(?<ifBody>[\s\S]+?)@else(?<elseBody>[\s\S]+?)@endif/,
8
+ /@foreach\s*\((?<loopvariable>(.*?)+) in (?<loopData>(.*?)+)\)(?<loopBody>[\s\S\t\n\r]+?)@endforeach/,
9
+ if: /@if\s*\((?<ifData>(.*?))\)\s*(?<ifBody>[\s\S]*?)\s*(?:@else\s*(?<elseBody>[\s\S]*?)\s*)?@endif/g,
10
10
  include: /@include\((?<includePath>['"]([^'"]+)['"])\)/,
11
11
  ternary:
12
- /@ternary \S*\((?<condition>[a-zA-Z0-9_.-]+)\s*\?\s*(?<trueData>[\s\S]+?)\s*:\s*(?<falseData>[\s\S]+?)\)/,
12
+ /@ternary\s*\((?<condition>[a-zA-Z0-9_.-]+)\s*\?\s*(?<trueData>[\s\S]+?)\s*:\s*(?<falseData>[\s\S]+?)\)/,
13
13
  variable: /{{\s*(?<param>(.*?))\s*}}/,
14
- auth: /@auth\s*(?:(\((?<authData>[^)]*)\)))?(?<authBody>[\s\S]*?)@endauth/,
14
+ auth: /@auth\s*(?:(\((?<authData>(.*?)*)\)))?(?<authBody>[\s\S]*?)@endauth/,
15
15
  guest: /@guest\s*(?<guestBody>[\s\S]*?)@endguest/,
16
16
  };
17
17
 
18
18
  constructor() {
19
19
  this.option = {};
20
+ this.cache = new Map();
20
21
  }
21
22
 
22
23
  #handleVariable(content, match, param) {
@@ -51,7 +52,10 @@ class Template {
51
52
 
52
53
  #handleCondition(content, match, param) {
53
54
  let result = "";
54
- const value = Util.accessNestedProperty(param, match?.groups?.ifData);
55
+ const value =
56
+ Util.accessNestedProperty(param, match?.groups?.ifData) ??
57
+ match?.groups.ifData;
58
+
55
59
  if (value) {
56
60
  result = this.#parser(match.groups?.ifBody, param);
57
61
  } else {
@@ -75,7 +79,7 @@ class Template {
75
79
  return beforMatch + result + this.#parser(afterMatch, param);
76
80
  }
77
81
 
78
- #extends(content) {
82
+ async #extends(content) {
79
83
  const match = content.match(this.#directives.extends);
80
84
  if (!match) return content;
81
85
  const layoutPath = match.groups?.extendPath;
@@ -103,7 +107,16 @@ class Template {
103
107
  #auth(content, match, param) {
104
108
  let result = "";
105
109
  if (Object.values(param?.Auth).length > 1) {
106
- result = match?.groups?.authBody;
110
+ const authData = match?.groups?.authData?.replace(/(\'|\")/g, "");
111
+ if (
112
+ (authData && param?.Auth?.role === authData) ||
113
+ param?.Auth?.roleType === authData
114
+ ) {
115
+ result = match?.groups?.authBody;
116
+ }
117
+ if (!authData) {
118
+ result = match?.groups?.authBody;
119
+ }
107
120
  }
108
121
  const { beforMatch, afterMatch } = Util.content(content, match);
109
122
  return this.#parser(beforMatch + result + afterMatch, param);
@@ -153,22 +166,29 @@ class Template {
153
166
  }
154
167
  }
155
168
 
156
- #compiler(content) {
157
- const newContent = this.#extends(content)
158
- .replace(/(\{\{\-\-)/g, "<!--")
159
- .replace(/(\-\-\}\})/g, "-->");
169
+ async #compiler(content) {
170
+ const newContent = await this.#extends(content);
160
171
  const finalContent = this.#parser(newContent);
161
172
  return finalContent
162
- .replace(/(\{\{\-\-)/g, "<!--")
163
- .replace(/(\-\-\}\})/g, "-->");
173
+ .replace(/(\{\{\-\-)/gm, "<!--")
174
+ .replace(/(\-\-\}\})/gm, "-->");
175
+ }
176
+
177
+ async #renderFromCache(templatePath) {
178
+ const renderedContent = this.cache.get(templatePath);
179
+ return await this.#compiler(renderedContent);
164
180
  }
165
- render(filePath, locals, callback) {
181
+ async render(filePath, locals, callback) {
166
182
  try {
167
- return fs.readFile(filePath, "utf-8", (err, content) => {
168
- if (err) return callback(err);
169
- this.option = locals;
170
- return callback(null, this.#compiler(content));
171
- });
183
+ // if (this.cache.has(filePath)) {
184
+ // return callback(null, await this.#renderFromCache(filePath));
185
+ // }
186
+ const content = fs.readFileSync(filePath, "utf-8");
187
+ this.option = locals;
188
+ const renderedContent = await this.#compiler(content);
189
+ // this.cache.set(filePath, renderedContent);
190
+
191
+ return callback(null, renderedContent);
172
192
  } catch (error) {
173
193
  throw new Error(error?.message);
174
194
  }