htmx-router 0.0.3 → 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,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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "htmx-router",
3
- "version": "0.0.3",
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": {
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