parseDir2.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import fs from "node:fs/promises";
  2. import {constants} from "node:fs";
  3. import path from "path";
  4. import htmlMinifier from "html-minifier-terser";
  5. import esbuild from "esbuild";
  6. export default async (dir)=>{
  7. const indexFile = await findIndex(dir);
  8. if(!indexFile) return;
  9. let data = {};
  10. if(path.extname(indexFile) === ".neovan"){
  11. data = await getNeovanData(indexFile);
  12. }else{
  13. data = await parseHtml();
  14. }
  15. fs.rm(path.join(dir, "tmp/"), {recursive: true, force: true});
  16. }
  17. const findIndex = async (dir)=>{
  18. const neovanPath = path.join(dir, "index.neovan");
  19. const htmlPath = path.join(dir, "index.html");
  20. const [neovan, html] = await Promise.allSettled([
  21. fs.access(path.join(dir, "index.neovan"), constants.F_OK),
  22. fs.access(path.join(dir, "index.html"), constants.F_OK)
  23. ]);
  24. if(neovan.status === "fulfilled") return neovanPath;
  25. if(html.status === "fulfilled") return htmlPath;
  26. return null;
  27. }
  28. const getNeovanData = async (index)=>{
  29. const neovan = await fs.readFile(index, "utf-8");
  30. const parentPath = path.dirname(index);
  31. const html = neovan.slice(neovan.indexOf("<contents>") + 10, neovan.indexOf("</contents>"));
  32. const css = neovan.slice(neovan.indexOf("<style>") + 7, neovan.indexOf("</style>"));
  33. const js = neovan.slice(neovan.indexOf("<script>") + 8, neovan.indexOf("</script>"));
  34. const cssFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.css`);
  35. const jsFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.js`);
  36. await fs.mkdir(path.join(parentPath, "tmp/"));
  37. await Promise.all([
  38. css === "" ? null : fs.writeFile(cssFile, css),
  39. js === "" ? null : fs.writeFile(jsFile, js)
  40. ]);
  41. return {
  42. html: html,
  43. css: cssFile,
  44. js: jsFile
  45. };
  46. }
  47. const parseHtml = async (index)=>{
  48. return null;
  49. }