Browse Source

first commit

Help-14 3 years ago
parent
commit
2a44193a89

+ 21 - 0
.vscode/launch.json

@@ -0,0 +1,21 @@
+{
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "request": "launch",
+            "name": "Launch Program",
+            "type": "pwa-node",
+            "program": "${workspaceFolder}/src/main.ts",
+            "cwd": "${workspaceFolder}/src",
+            "runtimeExecutable": "deno",
+            "runtimeArgs": [
+                "run",
+                "--inspect",
+                "--allow-net",
+                "--allow-read"
+            ],
+            "outputCapture": "std",
+            "attachSimplePort": 9229
+        }
+    ]
+}

+ 1 - 0
build.sh

@@ -0,0 +1 @@
+#!/bin/bash

+ 0 - 0
docs/index.md


+ 23 - 0
src/main.ts

@@ -0,0 +1,23 @@
+import { Config, loadConfig } from "./modules/config.ts";
+import { startServer } from "./modules/server.ts";
+import { Language } from "./modules/language.ts";
+
+declare global {
+  var config: Config;
+  var index: string;
+  var language: Language;
+  interface Window {
+    config: any;
+    index: string;
+    language: any;
+  }
+}
+
+window.config = await loadConfig();
+if (window.config === null) {
+  console.error("Failed to load config file.");
+  console.info("Using default config.");
+  window.config = new Config();
+}
+
+startServer();

+ 27 - 0
src/modules/config.ts

@@ -0,0 +1,27 @@
+export class Config {
+  server: ServerConfig = new ServerConfig();
+  website: WebsiteConfig = new WebsiteConfig();
+  addons: string[] = [];
+}
+
+class ServerConfig {
+  port: number = 7000;
+  minify: boolean = true;
+}
+
+class WebsiteConfig {
+  public title: string = "";
+  public description: string = "";
+  public background: string = "#218c74";
+  public localization: string = "en";
+}
+
+export async function loadConfig(): Promise<Config | null> {
+  try {
+    const json = JSON.parse(await Deno.readTextFile("./people.json"));
+    return json as Config;
+  } catch (err) {
+    console.error(err);
+    return null;
+  }
+}

+ 4 - 0
src/modules/file.ts

@@ -0,0 +1,4 @@
+// const watcher = Deno.watchFs("./public");
+// for await (const event of watcher) {
+//   console.log(">>>> event", event);
+//   Example event: { kind: "create", paths: [ "/home/alice/deno/foo.txt" ] }

+ 2 - 0
src/modules/language.ts

@@ -0,0 +1,2 @@
+export class Language {
+}

+ 68 - 0
src/modules/server.ts

@@ -0,0 +1,68 @@
+import { Application, Router } from "https://deno.land/x/oak/mod.ts";
+import { ensureDirSync, existsSync } from "https://deno.land/std/fs/mod.ts";
+import { Language, minify } from "https://deno.land/x/minifier/mod.ts";
+
+const tempFolder = `${Deno.cwd()}/temp`;
+
+export async function startServer(): Promise<void> {
+  const router = new Router();
+  router
+    .get("/", (context) => {
+      context.response.body = window.index;
+    });
+
+  const app = new Application();
+  app.use(router.routes());
+  app.use(router.allowedMethods());
+  app.use(async (context, next) => {
+    try {
+      await context.send({
+        root: `${Deno.cwd()}/public`,
+      });
+    } catch {
+      next();
+    }
+  });
+
+  await app.listen({ port: window.config.server.port });
+}
+
+export async function Compile(): Promise<void> {
+  ensureDirSync(tempFolder);
+  await CompileIndex();
+}
+
+export async function CompileIndex(): Promise<void> {
+  //Read index.html content
+  let indexPath = `${Deno.cwd()}/public/index.html`;
+  if (!existsSync(indexPath)) {
+    indexPath = `${Deno.cwd()}/private/index.html`;
+  }
+  let indexContent = await Deno.readTextFile(indexPath);
+
+  //Insert config into template
+  for (let property in window.config.website) {
+    indexContent = indexContent.replace(
+      `{{${property}}}`,
+      window.config.website[property],
+    );
+  }
+
+  //Insert localization into template
+  for (let key in window.language) {
+    indexContent = indexContent.replace(`{%${key}%}`, window.language[key]);
+  }
+
+  //Minify HTML
+  indexContent = minify(Language.HTML, indexContent);
+
+  //Write index.html into temp folder
+  ensureDirSync(tempFolder);
+  await Deno.writeTextFile(`${tempFolder}/index.html`, indexContent);
+}
+
+export async function CompileCSS(path: string): Promise<void> {
+}
+
+export async function CompileJS(path: string): Promise<void> {
+}

+ 12 - 0
src/private/config.json

@@ -0,0 +1,12 @@
+{
+    "server": {
+        "port": 7000,
+        "minify": true
+    },
+    "website": {
+        "title": "",
+        "description": "",
+        "background": ""
+    },
+    "addons": []
+}

+ 12 - 0
src/private/index.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="{{localization}}">
+    <head>
+        <meta charset="UTF-8">
+        <title>{{title}}</title>
+        <meta name="description" content="{{description}}"/>
+        <meta name="viewport" content="width=device-width, initial-scale=1">
+    </head>
+
+    <body style="background: {{background}}"></body>
+
+</html>

+ 1 - 0
src/private/languages/en.json

@@ -0,0 +1 @@
+{}

+ 1 - 0
src/private/languages/vn.json

@@ -0,0 +1 @@
+{}