htmx-router 0.0.2 → 0.0.4

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.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export declare function BuildDynamic(cwd: string): void;
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.BuildDynamic = void 0;
5
+ const fs_1 = require("fs");
6
+ function BuildDynamic(cwd) {
7
+ const rootMatcher = new RegExp(/^root\.(j|t)sx?$/);
8
+ const root = (0, fs_1.readdirSync)(cwd)
9
+ .filter(x => rootMatcher.test(x))[0];
10
+ if (!root) {
11
+ console.log(`Missing root.jsx/tsx`);
12
+ process.exit(1);
13
+ }
14
+ let script = `import { RouteTree, IsAllowedExt } from "htmx-router";\n`;
15
+ script +=
16
+ `import { readdirSync } from "fs";
17
+ import { extname, join, relative, resolve } from "path";
18
+
19
+ function readDirRecursively(dir: string) {
20
+ const files = readdirSync(dir, { withFileTypes: true });
21
+
22
+ let filePaths: string[] = [];
23
+ for (const file of files) {
24
+ if (file.isDirectory()) {
25
+ filePaths = [...filePaths, ...readDirRecursively(join(dir, file.name))];
26
+ } else {
27
+ filePaths.push(join(dir, file.name));
28
+ }
29
+ }
30
+
31
+ return filePaths;
32
+ }\n`;
33
+ script += `\nexport const Router = new RouteTree();\n`;
34
+ script += `import * as RootRoute from "./root";\n`;
35
+ script += `Router.assignRoot(RootRoute);\n\n`;
36
+ script += "const ctx = resolve(`${__dirname}/routes`);\n";
37
+ script += "const files = readDirRecursively(ctx);\n";
38
+ script += "for (const file of files){\n";
39
+ script += "\tconst ext = extname(file);\n";
40
+ script += "\tif (!IsAllowedExt(ext)) continue;\n";
41
+ script += "\tconst url = relative(ctx, file.slice(0, file.lastIndexOf(\".\")).replace(/\\\\/g, \"/\"));\n";
42
+ script += `\timport(file).then((mod) => Router.ingest(url, mod, []));\n`;
43
+ script += "}\n";
44
+ (0, fs_1.writeFileSync)(`${cwd}/router.ts`, script);
45
+ console.log(`Finished Building`);
46
+ }
47
+ exports.BuildDynamic = BuildDynamic;
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const dynamic_1 = require("./dynamic");
5
+ const static_1 = require("./static");
6
+ const isDynamic = process.argv.includes('--dynamic');
7
+ const cwd = process.argv[2] || "./";
8
+ console.log(`Building ${isDynamic ? "dynamic" : "static"} routes`);
9
+ if (isDynamic) {
10
+ (0, dynamic_1.BuildDynamic)(cwd);
11
+ }
12
+ else {
13
+ (0, static_1.BuildStatic)(cwd);
14
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export declare function BuildStatic(cwd: string): void;
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.BuildStatic = void 0;
5
+ const fs_1 = require("fs");
6
+ const path_1 = require("path");
7
+ const router_1 = require("../router");
8
+ function readDirRecursively(dir) {
9
+ const files = (0, fs_1.readdirSync)(dir, { withFileTypes: true });
10
+ let filePaths = [];
11
+ for (const file of files) {
12
+ if (file.isDirectory()) {
13
+ filePaths = [...filePaths, ...readDirRecursively((0, path_1.join)(dir, file.name))];
14
+ }
15
+ else {
16
+ filePaths.push((0, path_1.join)(dir, file.name));
17
+ }
18
+ }
19
+ return filePaths;
20
+ }
21
+ function BuildStatic(cwd) {
22
+ const rootMatcher = new RegExp(/^root\.(j|t)sx?$/);
23
+ const root = (0, fs_1.readdirSync)(cwd)
24
+ .filter(x => rootMatcher.test(x))[0];
25
+ if (!root) {
26
+ console.log(`Missing root.jsx/tsx`);
27
+ process.exit(1);
28
+ }
29
+ const DIR = './routes';
30
+ const files = readDirRecursively(`${cwd}/routes`)
31
+ .filter(x => (0, router_1.IsAllowedExt)((0, path_1.extname)(x)))
32
+ .map(x => (0, path_1.relative)(cwd, x.slice(0, x.lastIndexOf("."))).replace(/\\/g, "/"))
33
+ .sort();
34
+ let script = `import { RouteTree } from "htmx-router";\n`;
35
+ for (let i = 0; i < files.length; i++) {
36
+ const file = files[i];
37
+ script += `import * as Route${i} from "./${file}";\n`;
38
+ }
39
+ script += `import * as RootRoute from "./root";\n`;
40
+ script += `\nexport const Router = new RouteTree();\n`;
41
+ for (let i = 0; i < files.length; i++) {
42
+ const file = files[i];
43
+ script += `Router.ingest("${file.slice(DIR.length - 1)}", Route${i}, []);\n`;
44
+ }
45
+ script += `Router.assignRoot(RootRoute);\n`;
46
+ (0, fs_1.writeFileSync)(`${cwd}/router.ts`, script);
47
+ console.log(`Build with routes;\n` + files.map(x => ` - ${x}`).join("\n"));
48
+ }
49
+ exports.BuildStatic = BuildStatic;
package/bin/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RouteTree } from "./router";
1
+ import { RouteTree, IsAllowedExt } from "./router";
2
2
  import { ErrorResponse, Redirect, Outlet, Override, RenderArgs } from "./shared";
3
3
  import { StyleCSS } from "./helper";
4
- export { RouteTree, ErrorResponse, Redirect, Override, RenderArgs, Outlet, StyleCSS };
4
+ export { IsAllowedExt, RouteTree, ErrorResponse, Redirect, Override, RenderArgs, Outlet, StyleCSS };
package/bin/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StyleCSS = exports.RenderArgs = exports.Override = exports.Redirect = exports.ErrorResponse = exports.RouteTree = void 0;
3
+ exports.StyleCSS = exports.RenderArgs = exports.Override = exports.Redirect = exports.ErrorResponse = exports.RouteTree = exports.IsAllowedExt = void 0;
4
4
  const router_1 = require("./router");
5
5
  Object.defineProperty(exports, "RouteTree", { enumerable: true, get: function () { return router_1.RouteTree; } });
6
+ Object.defineProperty(exports, "IsAllowedExt", { enumerable: true, get: function () { return router_1.IsAllowedExt; } });
6
7
  const shared_1 = require("./shared");
7
8
  Object.defineProperty(exports, "ErrorResponse", { enumerable: true, get: function () { return shared_1.ErrorResponse; } });
8
9
  Object.defineProperty(exports, "Redirect", { enumerable: true, get: function () { return shared_1.Redirect; } });
package/bin/router.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import type http from "node:http";
3
- import { Outlet, RenderArgs, RouteModule } from "./shared";
3
+ import { Outlet, Override, Redirect, RenderArgs, RouteModule } from "./shared";
4
4
  export declare function IsAllowedExt(ext: string): boolean;
5
5
  declare class RouteLeaf {
6
6
  module: RouteModule;
@@ -17,7 +17,7 @@ export declare class RouteTree {
17
17
  constructor();
18
18
  assignRoot(module: RouteModule): void;
19
19
  ingest(path: string | string[], module: RouteModule, override: boolean[]): void;
20
- render(req: http.IncomingMessage, res: http.ServerResponse, url: URL): Promise<string> | "";
20
+ render(req: http.IncomingMessage, res: http.ServerResponse, url: URL): Promise<string | Redirect | Override>;
21
21
  private _recursiveRender;
22
22
  }
23
23
  export {};
package/bin/router.js CHANGED
@@ -3,16 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RouteTree = exports.IsAllowedExt = void 0;
4
4
  const shared_1 = require("./shared");
5
5
  function IsAllowedExt(ext) {
6
+ if (ext[0] !== ".")
7
+ return false;
6
8
  // js, jsx, tsx, ts
7
- if (ext[1] !== "s")
9
+ if (ext[2] !== "s")
8
10
  return false;
9
- if (ext[0] !== "j" && ext[0] !== "t")
11
+ if (ext[1] !== "j" && ext[1] !== "t")
10
12
  return false;
11
- if (ext.length == 2)
13
+ if (ext.length == 3)
12
14
  return true;
13
- if (ext.length != 3)
15
+ if (ext.length != 4)
14
16
  return false;
15
- if (ext[2] !== "x")
17
+ if (ext[3] !== "x")
16
18
  return false;
17
19
  return true;
18
20
  }
@@ -100,7 +102,7 @@ class RouteTree {
100
102
  path.splice(0, 1);
101
103
  next.ingest(path, module, override);
102
104
  }
103
- render(req, res, url) {
105
+ async render(req, res, url) {
104
106
  const args = new shared_1.RenderArgs(req, res, url);
105
107
  if (!this.default || !this.default.module.Render) {
106
108
  return "";
@@ -109,7 +111,18 @@ class RouteTree {
109
111
  if (frags.length === 1 && frags[0] === "") {
110
112
  frags.splice(0, 1);
111
113
  }
112
- return this._recursiveRender(args, frags).outlet();
114
+ try {
115
+ const out = await this._recursiveRender(args, frags).outlet();
116
+ return out;
117
+ }
118
+ catch (e) {
119
+ if (e instanceof shared_1.Redirect)
120
+ return e;
121
+ if (e instanceof shared_1.Override)
122
+ return e;
123
+ throw new Error(`Unhandled boil up type ${typeof (e)}: ${e}`);
124
+ }
125
+ ;
113
126
  }
114
127
  _recursiveRender(args, frags) {
115
128
  var _a;
package/bin/shared.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import type http from "node:http";
3
4
  export type Outlet = () => Promise<string>;
4
5
  export type RenderFunction = (args: RenderArgs, Outlet: Outlet) => Promise<string>;
@@ -19,8 +20,8 @@ export declare class Redirect {
19
20
  run(res: http.ServerResponse): http.ServerResponse<http.IncomingMessage>;
20
21
  }
21
22
  export declare class Override {
22
- data: BufferSource;
23
- constructor(data: BufferSource);
23
+ data: string | Buffer | Uint8Array;
24
+ constructor(data: string | Buffer | Uint8Array);
24
25
  }
25
26
  type MetaHTML = {
26
27
  [key: string]: string;
package/bin/shared.js CHANGED
@@ -26,7 +26,7 @@ class Override {
26
26
  }
27
27
  }
28
28
  exports.Override = Override;
29
- const attrRegex = /[A-z]+/;
29
+ const attrRegex = /^[A-z][A-z\-0-9]+$/;
30
30
  function ValidateMetaHTML(val) {
31
31
  for (const key in val) {
32
32
  if (!attrRegex.test(key))
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "htmx-router",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "A remix.js style file path router for htmX websites",
5
5
  "main": "./bin/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc"
8
8
  },
9
9
  "bin": {
10
- "htmx-router": "bin/cli.js"
10
+ "htmx-router": "bin/cli/index.js"
11
11
  },
12
12
  "repository": {
13
13
  "type": "git",
@@ -25,7 +25,6 @@
25
25
  "typescript": "^5.1.6"
26
26
  },
27
27
  "dependencies": {
28
- "csstype": "^3.1.2",
29
- "import": "^0.0.6"
28
+ "csstype": "^3.1.2"
30
29
  }
31
30
  }
package/bin/cli.js DELETED
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- const fs_1 = require("fs");
5
- const path_1 = require("path");
6
- const router_1 = require("./router");
7
- const cwd = process.argv[2] || "./";
8
- const rootMatcher = new RegExp(/^root\.(j|t)sx?$/);
9
- const root = (0, fs_1.readdirSync)(cwd)
10
- .filter(x => rootMatcher.test(x))[0];
11
- if (!root) {
12
- console.log(`Missing root.jsx/tsx`);
13
- process.exit(1);
14
- }
15
- function readDirRecursively(dir) {
16
- const files = (0, fs_1.readdirSync)(dir, { withFileTypes: true });
17
- let filePaths = [];
18
- for (const file of files) {
19
- if (file.isDirectory()) {
20
- filePaths = [...filePaths, ...readDirRecursively((0, path_1.join)(dir, file.name))];
21
- }
22
- else {
23
- filePaths.push((0, path_1.join)(dir, file.name));
24
- }
25
- }
26
- return filePaths;
27
- }
28
- const DIR = './routes';
29
- const files = readDirRecursively(`${cwd}/routes`)
30
- .filter(x => (0, router_1.IsAllowedExt)((0, path_1.extname)(x).slice(1)))
31
- .map(x => (0, path_1.relative)(cwd, x.slice(0, x.lastIndexOf("."))).replace(/\\/g, "/"))
32
- .sort();
33
- let script = `import { RouteTree } from "htmx-router";\n`;
34
- for (let i = 0; i < files.length; i++) {
35
- const file = files[i];
36
- script += `import * as Route${i} from "./${file}";\n`;
37
- }
38
- script += `import * as RootRoute from "./root";\n`;
39
- script += `\nexport const Router = new RouteTree;\n`;
40
- for (let i = 0; i < files.length; i++) {
41
- const file = files[i];
42
- script += `Router.ingest("${file.slice(DIR.length - 1)}", Route${i}, []);\n`;
43
- }
44
- script += `Router.assignRoot(RootRoute);\n`;
45
- (0, fs_1.writeFileSync)(`${cwd}/router.ts`, script);
46
- console.log(`Build with routes;\n` + files.map(x => ` - ${x}`).join("\n"));
File without changes