htmx-router 0.0.3 → 0.0.5

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,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/router.d.ts CHANGED
@@ -6,7 +6,7 @@ declare class RouteLeaf {
6
6
  module: RouteModule;
7
7
  mask: boolean[];
8
8
  constructor(module: RouteModule, mask: boolean[]);
9
- makeOutlet(args: RenderArgs, outlet: Outlet): Outlet;
9
+ makeOutlet(args: RenderArgs, outlet: Outlet, depth: number): Outlet;
10
10
  }
11
11
  export declare class RouteTree {
12
12
  nested: Map<string, RouteTree>;
package/bin/router.js CHANGED
@@ -27,11 +27,12 @@ class RouteLeaf {
27
27
  this.module = module;
28
28
  this.mask = mask;
29
29
  }
30
- makeOutlet(args, outlet) {
30
+ makeOutlet(args, outlet, depth) {
31
31
  const renderer = this.module.Render || blankOutlet;
32
32
  const catcher = this.module.CatchError;
33
33
  return async () => {
34
34
  try {
35
+ args.depth = depth;
35
36
  return await renderer(args, outlet);
36
37
  }
37
38
  catch (e) {
@@ -39,8 +40,10 @@ class RouteLeaf {
39
40
  throw e;
40
41
  const err = (e instanceof shared_1.ErrorResponse) ? e :
41
42
  new shared_1.ErrorResponse(500, "Runtime Error", e);
42
- if (catcher)
43
+ if (catcher) {
44
+ args.depth = depth;
43
45
  return await catcher(args, err);
46
+ }
44
47
  throw err;
45
48
  }
46
49
  };
@@ -137,8 +140,8 @@ class RouteTree {
137
140
  };
138
141
  }
139
142
  else if ((_a = this.default) === null || _a === void 0 ? void 0 : _a.module.Render) {
140
- out.outlet = this.default.makeOutlet(args, out.outlet);
141
143
  out.mask = [...this.default.mask];
144
+ out.outlet = this.default.makeOutlet(args, out.outlet, out.mask.length);
142
145
  }
143
146
  }
144
147
  else {
@@ -160,7 +163,7 @@ class RouteTree {
160
163
  // Is this route masked out?
161
164
  const ignored = out.mask.splice(0, 1)[0] === true;
162
165
  if (!ignored && this.route) {
163
- out.outlet = this.route.makeOutlet(args, out.outlet);
166
+ out.outlet = this.route.makeOutlet(args, out.outlet, out.mask.length);
164
167
  }
165
168
  return out;
166
169
  }
package/bin/shared.d.ts CHANGED
@@ -30,6 +30,7 @@ export declare class RenderArgs {
30
30
  req: http.IncomingMessage;
31
31
  res: http.ServerResponse;
32
32
  params: MetaHTML;
33
+ depth: number;
33
34
  url: URL;
34
35
  links: MetaHTML[];
35
36
  meta: MetaHTML[];
package/bin/shared.js CHANGED
@@ -47,6 +47,7 @@ class RenderArgs {
47
47
  this.res = res;
48
48
  this.url = url;
49
49
  this.params = {};
50
+ this.depth = -1;
50
51
  this.links = [];
51
52
  this.meta = [];
52
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "htmx-router",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "A remix.js style file path router for htmX websites",
5
5
  "main": "./bin/index.js",
6
6
  "scripts": {
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