@uxf/router 11.64.3 → 11.68.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/router",
3
- "version": "11.64.3",
3
+ "version": "11.68.0",
4
4
  "description": "UXF Router",
5
5
  "author": "UXFans <dev@uxf.cz>",
6
6
  "homepage": "https://gitlab.com/uxf-npm/router#readme",
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ts-node-script
2
+ export declare function routesCheck(routes: Record<string, string>, shouldProcessExit?: boolean): void;
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env ts-node-script
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.routesCheck = routesCheck;
8
+ const is_not_empty_1 = require("@uxf/core/utils/is-not-empty");
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const path_1 = require("path");
11
+ const true_case_path_1 = require("true-case-path");
12
+ let error = false;
13
+ function fileExists(path) {
14
+ try {
15
+ return Boolean((0, true_case_path_1.trueCasePathSync)(path));
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ }
21
+ function directoryExists(path) {
22
+ try {
23
+ const stats = fs_1.default.lstatSync(path);
24
+ return stats.isDirectory();
25
+ }
26
+ catch {
27
+ return false;
28
+ }
29
+ }
30
+ /**
31
+ * Filters out folders used for organizational purposes in the app folder
32
+ * such as parallel routes (e.g., (group)) and dynamic routes ([param]).
33
+ */
34
+ function cleanPathSegments(pathSegments) {
35
+ return pathSegments.filter((segment) => !segment.startsWith("(") && !segment.endsWith(")"));
36
+ }
37
+ function normalizeDynamicSegment(segment) {
38
+ return (segment === null || segment === void 0 ? void 0 : segment.startsWith("[")) ? "index" : segment || "";
39
+ }
40
+ /**
41
+ * Checks for collisions in the path (in `baseDir`) * between the `segment.tsx` file and the `segment/` folder within one level.
42
+ *
43
+ * Example:
44
+ * - route `/payments/export` gives segments ["payments", "export"]
45
+ * - check if it exists at the same time `pages/payments.tsx` and `pages/payments/`.
46
+ */
47
+ function checkFolderFileCollision(baseDir, pathSegments) {
48
+ for (let i = 0; i < pathSegments.length - 1; i++) {
49
+ const segment = pathSegments[i];
50
+ const parentDir = (0, path_1.join)(baseDir, ...pathSegments.slice(0, i));
51
+ const filePath = (0, path_1.join)(parentDir, `${segment}.tsx`);
52
+ const folderPath = (0, path_1.join)(parentDir, segment);
53
+ if (fileExists(filePath) && directoryExists(folderPath)) {
54
+ // eslint-disable-next-line no-console
55
+ console.log(`Colission: For segment '${segment}' there is file and folder at the same time.\n` +
56
+ `Replace file ${segment}.tsx with ${segment}/index.tsx.`);
57
+ throw new Error("Route colission");
58
+ }
59
+ }
60
+ }
61
+ function routesCheck(routes, shouldProcessExit = true) {
62
+ Object.keys(routes).forEach((route) => {
63
+ const pathname = routes[route];
64
+ try {
65
+ const basePath = (0, path_1.join)(__dirname, "..", "..", "src", "pages");
66
+ const appBasePath = (0, path_1.join)(__dirname, "..", "..", "src", "app");
67
+ const rawPathSegments = pathname.substring(1).split("/");
68
+ const pathSegments = cleanPathSegments(rawPathSegments);
69
+ checkFolderFileCollision(basePath, pathSegments);
70
+ checkFolderFileCollision(appBasePath, pathSegments);
71
+ // Find the last segment (e.g. for /payments/export it will be "export")
72
+ const lastPart = pathSegments.splice(-1);
73
+ const normalizedLastPart = normalizeDynamicSegment(lastPart.at(0));
74
+ // --- Checks in `pages` folder ---
75
+ // 1) path like pages/product.tsx (if it's at "first level")
76
+ const firstLevelPagePath = (0, path_1.join)(basePath, `${normalizedLastPart}.tsx`);
77
+ /// 2) path type pages/product/index.tsx
78
+ const firstLevelFolderPath = (0, path_1.join)(basePath, normalizedLastPart, "index.tsx");
79
+ // 3) paths for nested pages (pages/payments/export.tsx etc.)
80
+ const nestedPaths = (0, path_1.join)(basePath, ...pathSegments, `${normalizedLastPart || "index"}.tsx`);
81
+ const pagePath = (0, is_not_empty_1.isNotEmpty)(pathSegments)
82
+ ? nestedPaths
83
+ : fileExists(firstLevelFolderPath)
84
+ ? firstLevelFolderPath
85
+ : firstLevelPagePath;
86
+ // --- Checks in `app folder ` ---
87
+ const appPath = (0, path_1.join)(appBasePath, ...pathSegments, normalizedLastPart, "page.tsx");
88
+ // Check if the path exists in either `pages` or `app`
89
+ if (fileExists(pagePath) || fileExists(appPath)) {
90
+ return; // Valid route
91
+ }
92
+ throw new Error("Missing file for route!");
93
+ }
94
+ catch (err) {
95
+ // eslint-disable-next-line no-console
96
+ console.log("❌ Invalid route: " + pathname);
97
+ if (err instanceof Error) {
98
+ // eslint-disable-next-line no-console
99
+ console.error(err.message);
100
+ }
101
+ error = true;
102
+ }
103
+ });
104
+ if (error) {
105
+ if (shouldProcessExit) {
106
+ process.exit(1);
107
+ }
108
+ throw new Error("At least one invalid route found");
109
+ }
110
+ else {
111
+ if (shouldProcessExit) {
112
+ process.exit(0);
113
+ }
114
+ // eslint-disable-next-line no-console
115
+ console.log("All routes checked.");
116
+ }
117
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const path_1 = require("path");
8
+ const true_case_path_1 = require("true-case-path");
9
+ const routes_check_1 = require("./routes-check");
10
+ jest.mock("fs", () => {
11
+ // Access the actual fs module so we can spread it
12
+ const actualFs = jest.requireActual("fs");
13
+ return {
14
+ ...actualFs,
15
+ // We'll mock only the function(s) we need
16
+ lstatSync: jest.fn(),
17
+ };
18
+ });
19
+ jest.mock("true-case-path", () => ({
20
+ trueCasePathSync: jest.fn(),
21
+ }));
22
+ const mockTrueCasePathSync = true_case_path_1.trueCasePathSync;
23
+ /**
24
+ * Helper function to mock:
25
+ * 1) Which file paths "exist"
26
+ * 2) Which directory paths "exist"
27
+ */
28
+ function mockFileExists(knownPaths, knownDirs = []) {
29
+ // Mock the trueCasePathSync behavior (our fileExists check)
30
+ mockTrueCasePathSync.mockImplementation((inputPath) => {
31
+ if (knownPaths.includes(inputPath)) {
32
+ return inputPath; // indicates file exists
33
+ }
34
+ throw new Error(`Path not found: ${inputPath}`);
35
+ });
36
+ // Mock lstatSync for directory checks
37
+ fs_1.default.lstatSync.mockImplementation((inputPath) => {
38
+ if (knownDirs.includes(inputPath)) {
39
+ return { isDirectory: () => true };
40
+ }
41
+ // if it's in knownPaths but not in knownDirs => treat it as a file
42
+ if (knownPaths.includes(inputPath)) {
43
+ return { isDirectory: () => false };
44
+ }
45
+ // otherwise, it's not found => throw
46
+ throw new Error(`No such file or directory: ${inputPath}`);
47
+ });
48
+ }
49
+ const rootDirPages = (0, path_1.join)(__dirname, "..", "..", "src", "pages");
50
+ const rootDirApp = (0, path_1.join)(__dirname, "..", "..", "src", "app");
51
+ describe("routesCheck", () => {
52
+ beforeEach(() => {
53
+ jest.clearAllMocks();
54
+ });
55
+ it("should handle multiple valid routes at once (pages + app)", () => {
56
+ /**
57
+ * 1) /product => pages/product/index.tsx
58
+ * 2) /product/edit => pages/product/edit.tsx
59
+ * 3) /product/[id] => pages/product/[id].tsx
60
+ * 4) /dashboard => app/dashboard/page.tsx
61
+ * 5) /settings/privacy => app/settings/privacy/page.tsx
62
+ */
63
+ mockFileExists([
64
+ // pages
65
+ (0, path_1.join)(rootDirPages, "product", "index.tsx"),
66
+ (0, path_1.join)(rootDirPages, "product", "edit.tsx"),
67
+ (0, path_1.join)(rootDirPages, "product", "[id].tsx"),
68
+ // app
69
+ (0, path_1.join)(rootDirApp, "dashboard", "page.tsx"),
70
+ (0, path_1.join)(rootDirApp, "settings", "privacy", "page.tsx"),
71
+ ], [
72
+ // directories
73
+ (0, path_1.join)(rootDirPages, "product"),
74
+ (0, path_1.join)(rootDirApp, "dashboard"),
75
+ (0, path_1.join)(rootDirApp, "settings"),
76
+ (0, path_1.join)(rootDirApp, "settings", "privacy"),
77
+ ]);
78
+ const routes = {
79
+ product: "/product",
80
+ productEdit: "/product/edit",
81
+ productDetail: "/product/[id]",
82
+ dashboard: "/dashboard",
83
+ privacy: "/settings/privacy",
84
+ };
85
+ expect(() => (0, routes_check_1.routesCheck)(routes, false)).not.toThrow();
86
+ });
87
+ it("should fail when invalid routes are provided", () => {
88
+ /**
89
+ * We'll test multiple routes that do not exist in either pages or app:
90
+ * 1) /unknown
91
+ * 2) /product/unknown
92
+ */
93
+ // Intentionally not mocking any matching paths here
94
+ mockFileExists([]);
95
+ const routes = {
96
+ unknown: "/unknown",
97
+ productUnknown: "/product/unknown",
98
+ };
99
+ expect(() => (0, routes_check_1.routesCheck)(routes, false)).toThrow();
100
+ });
101
+ it("Pages folder: should detect collisions (file + folder) in multiple segments", () => {
102
+ /**
103
+ * Testing collision scenarios together:
104
+ * 1) /payments/export in pages => collision with payments.tsx + payments/ folder
105
+ */
106
+ mockFileExists([
107
+ // pages collisions
108
+ (0, path_1.join)(rootDirPages, "payments.tsx"),
109
+ (0, path_1.join)(rootDirPages, "payments", "export.tsx"),
110
+ ], [(0, path_1.join)(rootDirPages, "payments")]);
111
+ const routes = {
112
+ paymentsExport: "/payments/export",
113
+ };
114
+ expect(() => (0, routes_check_1.routesCheck)(routes, false)).toThrow();
115
+ });
116
+ it("App folder: should detect that page.tsx is not created for the route", () => {
117
+ /**
118
+ * Testing collision scenarios together:
119
+ * 1) /admin/analytics in app => collision with analytics.tsx + analytics/ folder
120
+ */
121
+ mockFileExists([
122
+ // app collisions
123
+ (0, path_1.join)(rootDirApp, "admin", "analytics.tsx"),
124
+ ], [(0, path_1.join)(rootDirApp, "admin"), (0, path_1.join)(rootDirApp, "admin", "analytics")]);
125
+ const routes = {
126
+ adminAnalytics: "/admin/analytics",
127
+ };
128
+ expect(() => (0, routes_check_1.routesCheck)(routes, false)).toThrow();
129
+ });
130
+ });