olum-compiler 1.0.0 → 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/bin/copySrc.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // todo check changed files only to be copied, use this project https://github.com/eissapk/diff
2
2
  const path = require("path");
3
3
  const fs = require("fs");
4
- const { copy, remove, ls, generateRouteManifest, generateImports, generateRoutes } = require("../lib/helpers");
4
+ const { copy, remove, compileRoutes } = require("../lib/helpers");
5
5
  const entryPoint = path.resolve(process.cwd(), "src").replace(/\/module/, "");
6
6
  const entryPoint2 = path.resolve(process.cwd(), "public").replace(/\/module/, "");
7
7
 
@@ -13,39 +13,10 @@ module.exports = function copySrc() {
13
13
  if (fs.existsSync(path.resolve(__dirname, "../src"))) remove(path.resolve(__dirname, "../src"));
14
14
  // copy
15
15
  copy(entryPoint, path.resolve(__dirname, "../src"), () => {
16
- // modify main.js file import paths -- old concept for main.js that was wriiten manullly but user
17
- // const mainJsPath = path.resolve(__dirname, "../src/main.js");
18
- // if (fs.existsSync(mainJsPath)) {
19
- // let mainJsContent = fs.readFileSync(mainJsPath).toString();
20
- // mainJsContent = rewriteImports(mainJsContent);
21
- // fs.writeFileSync(mainJsPath, mainJsContent);
22
- // }
23
-
24
- // clean routes
25
- let routes = ls(entryPoint, { absolute: true });
26
- routes = routes.filter((item) => {
27
- const extension = path.extname(item);
28
- const isDir = fs.lstatSync(item).isDirectory();
29
- const isHtml = fs.lstatSync(item).isFile() && extension == ".html";
30
- if (isDir || isHtml && !item.includes("components")) return item;
31
- });
32
- // generate routes manifest
33
- const manifest = generateRouteManifest(routes, entryPoint);
34
- let has404Page = false;
35
- if (manifest['not-found']) has404Page = true;
36
- const libImports = `import Olum from "/node_modules/olum/dist/olum.js";\nimport Router from "/node_modules/olum-router/dist/router.js";`;
37
- // const libImports = `import Olum from "../core/olum.js";\nimport Router from "../core/router.js";`; // for testing
38
- const imports = generateImports(manifest).replace(/.html/g, ".js"); // fix extension by replacing .html with .js since components are js modules
39
- const config = `const config = { mode: "history", root: "/", ${has404Page? `err: "/404",` : "" } routes: routes };\nconst router = new Router(config);\nnew Olum().$("#app").use(router);`;
40
- routes = generateRoutes(manifest);
41
- // console.log(libImports);
42
- // console.log(imports);
43
- // console.log(routes);
44
- // console.log(config);
45
-
16
+ const mainJsContent = compileRoutes(entryPoint);
46
17
  // automated main.js -- new concept
47
18
  const mainJsPath = path.resolve(__dirname, "../src/main.js");
48
- fs.writeFileSync(mainJsPath, `${libImports}\n${imports}\n${routes}\n${config}`);
19
+ fs.writeFileSync(mainJsPath, mainJsContent);
49
20
  });
50
21
  copy(entryPoint2, path.resolve(__dirname, "../public"), () => {
51
22
  const indexHtmlPath = path.resolve(__dirname, "../public/index.html");
package/bin/dev.js CHANGED
@@ -24,7 +24,8 @@ serve(entryPoint).then(({ server, wsPort, PORT, log, setWsPort }) => {
24
24
  watch.on("unlink", handleChange);
25
25
 
26
26
  (function startWs(port) {
27
- const wss = new WebSocket.Server({ port });
27
+ // const wss = new WebSocket.Server({ port });
28
+ const wss = new WebSocket.Server({ server }); // using the same server port
28
29
  wss.on("listening", () => setWsPort(port)); // tell server.js the real ws port to inject
29
30
  wss.on("error", (e) => { if (e.code === "EADDRINUSE") startWs(port + 1); });
30
31
  wss.on("connection", (ws) => {
package/bin/server.js CHANGED
@@ -5,11 +5,11 @@ const http = require("http");
5
5
  const jsdom = require("jsdom");
6
6
  const colors = require("../lib/colors");
7
7
  const mimes = require("./mimes.json");
8
- let wsPort = 8090;
8
+ let wsPort = 8090; // old way of ws -- it was using seperated port from server
9
9
 
10
10
  function handleWsFile() {
11
11
  let file = fs.readFileSync(path.resolve(__dirname, "./ws.js")).toString();
12
- file = file.replace(/{{port}}/, wsPort);
12
+ // file = file.replace(/{{port}}/, wsPort);
13
13
  file = "<script>" + file + "</script>";
14
14
  return file;
15
15
  }
@@ -51,7 +51,7 @@ function getPath(entry, url) {
51
51
  */
52
52
  function serve(entry, port) {
53
53
  return new Promise((resolve, reject) => {
54
- const PORT = port ? port : process.env.PORT || 8000;
54
+ const PORT = Number(port || process.env.PORT || 3000);
55
55
  const domain = `http://localhost:${PORT}`;
56
56
 
57
57
  const server = http.createServer(handler);
package/bin/ws.js CHANGED
@@ -4,7 +4,9 @@
4
4
  const interval = 500;
5
5
  const maxAttempts = 30;
6
6
  let attempts = 0;
7
- const socketUrl = "ws://localhost:{{port}}";
7
+ const wsProtocol = location.protocol === "https:" ? "wss:" : "ws:";
8
+ // const socketUrl = `${wsProtocol}//${location.hostname}:{{port}}`;
9
+ const socketUrl = `${wsProtocol}//${location.host}`; // removing port so it uses the server port
8
10
  let socket = new WebSocket(socketUrl);
9
11
 
10
12
  socket.onmessage = function (res) {
package/lib/helpers.js CHANGED
@@ -18,6 +18,19 @@ const stringToBase64 = (str) => Buffer.from(str).toString('base64');
18
18
  const base64ToString = (base64) => Buffer.from(base64, 'base64').toString('ascii');
19
19
  const acorn = require("acorn");
20
20
 
21
+ const cleanParentheses = (filePath, type="name") => {
22
+ if (type == "name") {
23
+ return filePath.replace(/\([^)]*\)/g, "")
24
+ } else if (type == "url"){ // clean slash after it also e.g. (main)/
25
+ return filePath.replace(/\([^)]*\)\//g, "")
26
+ }
27
+ return filePath;
28
+ }
29
+
30
+ const cleanNumbersPrefix = (name) => {
31
+ return name.replace(/\d+-?/g, "")
32
+ }
33
+
21
34
  function isUpper(char) {
22
35
  const upperChar = char.toUpperCase();
23
36
  const regex = new RegExp(upperChar);
@@ -565,9 +578,10 @@ function generateRouteManifest(paths, rootDir) {
565
578
  }
566
579
 
567
580
  // Remove "/page.html" or "page.html"
568
- const routePath = relative
581
+ let routePath = relative
569
582
  .replace(/\/page\.html$/, "")
570
583
  .replace(/^page\.html$/, "");
584
+ routePath = cleanParentheses(routePath, "url")
571
585
 
572
586
  const segments = routePath
573
587
  .split("/")
@@ -598,6 +612,7 @@ function generateRouteManifest(paths, rootDir) {
598
612
  }
599
613
 
600
614
  function toComponentName(file) {
615
+ file = cleanParentheses(file, "name");
601
616
  if (file === "page.html") return "App";
602
617
  if (file === "not-found.html") return "NotFound";
603
618
 
@@ -621,8 +636,8 @@ function toComponentName(file) {
621
636
  function generateImports(manifest) {
622
637
  return Object.values(manifest)
623
638
  .map(({ page }) => {
624
- const component = toComponentName(page);
625
- return `import ${component} from "./${page}";`;
639
+ const component = cleanNumbersPrefix(toComponentName(page));
640
+ return `import ${component} from "./${page.replace(/.html$/, ".js")}";`;
626
641
  })
627
642
  .join("\n");
628
643
  }
@@ -658,7 +673,7 @@ function generateRoutes(manifest) {
658
673
 
659
674
  return `export const routes = [
660
675
  ${routes
661
- .map(({ path, comp }) => ` { path: "${path}", comp: ${comp} }`)
676
+ .map(({ path, comp }) => ` { path: "${cleanNumbersPrefix(path)}", comp: ${cleanNumbersPrefix(comp)} }`)
662
677
  .join(",\n")}
663
678
  ];`;
664
679
  }
@@ -674,6 +689,30 @@ const libsMap = {
674
689
  }
675
690
  }
676
691
 
692
+ const compileRoutes = (entryPoint) => {
693
+ let routes = ls(entryPoint, { absolute: true });
694
+ routes = routes.filter((item) => {
695
+ const extension = path.extname(item);
696
+ const isDir = fs.lstatSync(item).isDirectory();
697
+ const isHtml = fs.lstatSync(item).isFile() && extension == ".html";
698
+ if (isDir || isHtml && !item.includes("components")) return item;
699
+ });
700
+ // generate routes manifest
701
+ const manifest = generateRouteManifest(routes, entryPoint);
702
+ let has404Page = false;
703
+ if (manifest['not-found']) has404Page = true;
704
+ const libImports = `import Olum from "/node_modules/olum/dist/olum.js";\nimport Router from "/node_modules/olum-router/dist/router.js";`;
705
+ // const libImports = `import Olum from "../core/olum.js";\nimport Router from "../core/router.js";`; // for testing
706
+ const imports = generateImports(manifest)
707
+ const config = `const config = { mode: "history", root: "/", ${has404Page? `err: "/404",` : "" } routes: routes };\nconst router = new Router(config);\nnew Olum().$("#app").use(router);`;
708
+ routes = generateRoutes(manifest);
709
+ // console.log(libImports);
710
+ // console.log(imports);
711
+ // console.log(routes);
712
+ // console.log(config);
713
+ return `${libImports}\n${imports}\n${routes}\n${config}`;
714
+ }
715
+
677
716
  module.exports.isNum = isNum;
678
717
  module.exports.id = id;
679
718
  module.exports.isUpper = isUpper;
@@ -700,3 +739,4 @@ module.exports.generateRouteManifest = generateRouteManifest;
700
739
  module.exports.generateImports = generateImports;
701
740
  module.exports.generateRoutes = generateRoutes;
702
741
  module.exports.libsMap = libsMap;
742
+ module.exports.compileRoutes = compileRoutes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {