Эх сурвалжийг харах

Refactoring. Created reading/parsing of neovan files.

Lee Morgan 2 долоо хоног өмнө
parent
commit
3cef97e8ac
3 өөрчлөгдсөн 64 нэмэгдсэн , 2 устгасан
  1. 1 1
      index.js
  2. 3 1
      parseDir.js
  3. 60 0
      parseDir2.js

+ 1 - 1
index.js

@@ -1,7 +1,7 @@
 import fs from "node:fs/promises";
 import path from "path";
 
-import parseDir from "./parseDir.js";
+import parseDir from "./parseDir2.js";
 
 export default async (express, options = {})=> {
     console.time("Build Completed In");

+ 3 - 1
parseDir.js

@@ -76,6 +76,7 @@ const bundleFiles = async (files)=>{
         contents.js = `<script>${data[0].outputFiles[0].text}</script>`;
     }
 
+    if(files.neovan) fs.rm(path.dirname(files.html), {recursive: true});
     return contents;
 }
 
@@ -116,6 +117,7 @@ const splitNeovan = async (file)=>{
     return {
         html: htmlFile,
         css: css !== "" ? cssFile : undefined,
-        js: js !== "" ? jsFile : undefined
+        js: js !== "" ? jsFile : undefined,
+        neovan: true
     };
 }

+ 60 - 0
parseDir2.js

@@ -0,0 +1,60 @@
+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;
+    let data = {};
+    if(path.extname(indexFile) === ".neovan"){
+        data = await getNeovanData(indexFile);
+    }else{
+        data = await parseHtml();
+    }
+    fs.rm(path.join(dir, "tmp/"), {recursive: true, force: true});
+}
+
+const findIndex = 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;
+}
+
+const getNeovanData = async (index)=>{
+    const neovan = await fs.readFile(index, "utf-8");
+    const parentPath = path.dirname(index);
+    
+    const html = neovan.slice(neovan.indexOf("<contents>") + 10, neovan.indexOf("</contents>"));
+    const css = neovan.slice(neovan.indexOf("<style>") + 7, neovan.indexOf("</style>"));
+    const js = neovan.slice(neovan.indexOf("<script>") + 8, neovan.indexOf("</script>"));
+
+    const cssFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.css`);
+    const jsFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.js`);
+
+    await fs.mkdir(path.join(parentPath, "tmp/"));
+
+    await Promise.all([
+        css === "" ? null : fs.writeFile(cssFile, css),
+        js === "" ? null : fs.writeFile(jsFile, js)
+    ]);
+
+    return {
+        html: html,
+        css: cssFile,
+        js: jsFile
+    };
+}
+
+const parseHtml = async (index)=>{
+    return null;
+}