소스 검색

Refactor parsedir to parse a single component instead.

Lee Morgan 2 주 전
부모
커밋
63f3dcbfba
2개의 변경된 파일24개의 추가작업 그리고 9개의 파일을 삭제
  1. 19 2
      index.js
  2. 5 7
      parseComponent.js

+ 19 - 2
index.js

@@ -1,7 +1,8 @@
 import fs from "node:fs/promises";
 import path from "path";
+import {constants} from "node:fs";
 
-import parseDir from "./parseDir.js";
+import parseComponent from "./parseComponent.js";
 
 export default async (express, options = {})=> {
     console.time("Build Completed In");
@@ -27,5 +28,21 @@ const readFiles = async (dir, root, app)=>{
         }
     }
 
-    parseDir(dir);
+    let indexFile = await findIndexFile(dir);
+    if(!indexFile) return;
+    parseComponent(indexFile);
+}
+
+const findIndexFile = async (dir)=>{
+    const neovanPath = path.join(dir, "index.neovan");
+    const htmlPath = path.join(dir, "index.html");
+
+    const [neovan, html] = await Promise.allSettled([
+        fs.access(path.join(dir, "index.neovan"), constants.F_OK),
+        fs.access(path.join(dir, "index.html"), constants.F_OK)
+    ]);
+
+    if(neovan.status === "fulfilled") return neovanPath;
+    if(html.status === "fulfilled") return htmlPath;
+    return null;
 }

+ 5 - 7
parseDir.js → parseComponent.js

@@ -1,17 +1,15 @@
 import fs from "node:fs/promises";
-import {constants} from "node:fs";
 import path from "path";
 import htmlMinifier from "html-minifier-terser";
 import esbuild from "esbuild";
 
-export default async (dir)=>{
-    const indexFile = await findIndex(dir);
-    if(!indexFile) return;
+export default async (file)=>{
+    const dir = path.dirname(file);
     let data = {};
-    if(path.extname(indexFile) === ".neovan"){
-        data = await getNeovanData(indexFile);
+    if(path.extname(file) === ".neovan"){
+        data = await getNeovanData(file);
     }else{
-        data = await parseHtml(indexFile);
+        data = await parseHtml(file);
     }
     const bundle = await createBundle(data);
     writeFile(dir, bundle);