@granite-js/plugin-router 0.1.30 → 0.1.32
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/CHANGELOG.md +17 -0
- package/dist/index.cjs +37 -40
- package/dist/index.d.cts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +30 -34
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @granite-js/plugin-router
|
|
2
2
|
|
|
3
|
+
## 0.1.32
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 7572713: bump version up babel
|
|
8
|
+
- Updated dependencies [7572713]
|
|
9
|
+
- @granite-js/plugin-core@0.1.32
|
|
10
|
+
|
|
11
|
+
## 0.1.31
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- 37ae3f3: feat(router): supports standard schema
|
|
16
|
+
- Updated dependencies [9bf8b50]
|
|
17
|
+
- Updated dependencies [e957833]
|
|
18
|
+
- @granite-js/plugin-core@0.1.31
|
|
19
|
+
|
|
3
20
|
## 0.1.30
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -21,12 +21,13 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
21
|
}) : target, mod));
|
|
22
22
|
|
|
23
23
|
//#endregion
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
let fs = require("fs");
|
|
25
|
+
let path = require("path");
|
|
26
|
+
let __swc_core = require("@swc/core");
|
|
27
|
+
let es_toolkit = require("es-toolkit");
|
|
28
|
+
let fs_promises = require("fs/promises");
|
|
29
|
+
let chokidar = require("chokidar");
|
|
30
|
+
chokidar = __toESM(chokidar);
|
|
30
31
|
|
|
31
32
|
//#region src/checkExportRoute.ts
|
|
32
33
|
/**
|
|
@@ -38,20 +39,18 @@ function checkExportRoute(path$1) {
|
|
|
38
39
|
syntax: "typescript",
|
|
39
40
|
tsx: true
|
|
40
41
|
});
|
|
41
|
-
|
|
42
|
+
if (ast.body.some((node) => {
|
|
42
43
|
if (node.type !== "ExportNamedDeclaration") return false;
|
|
43
44
|
return node.specifiers?.some((specifier) => {
|
|
44
45
|
if (specifier.type !== "ExportSpecifier") return false;
|
|
45
46
|
return specifier.orig?.value === "Route";
|
|
46
47
|
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const hasExportNamedVariable = ast.body.some((node) => {
|
|
48
|
+
})) return true;
|
|
49
|
+
return ast.body.some((node) => {
|
|
50
50
|
if (node.type !== "ExportDeclaration") return false;
|
|
51
51
|
if (node.declaration.type !== "VariableDeclaration") return false;
|
|
52
52
|
return node.declaration.declarations.some((declaration) => declaration.id.type === "Identifier" && declaration.id.value === "Route");
|
|
53
53
|
});
|
|
54
|
-
return hasExportNamedVariable;
|
|
55
54
|
} catch {
|
|
56
55
|
return false;
|
|
57
56
|
}
|
|
@@ -60,11 +59,9 @@ function checkExportRoute(path$1) {
|
|
|
60
59
|
//#endregion
|
|
61
60
|
//#region src/getComponentName.ts
|
|
62
61
|
function getComponentName(filePath) {
|
|
63
|
-
const
|
|
64
|
-
const segments = path$1.split("/").filter(Boolean);
|
|
62
|
+
const segments = filePath.replace(/^pages\//, "").replace(/\.(tsx|ts)$/, "").split("/").filter(Boolean);
|
|
65
63
|
if (segments[segments.length - 1] === "index") segments.pop();
|
|
66
|
-
|
|
67
|
-
return componentName || "Index";
|
|
64
|
+
return segments.map((segment) => (0, es_toolkit.pascalCase)(segment)).join("") || "Index";
|
|
68
65
|
}
|
|
69
66
|
|
|
70
67
|
//#endregion
|
|
@@ -100,6 +97,10 @@ const ROUTER_GEN_TEMPLATE = `/* eslint-disable */
|
|
|
100
97
|
%%pageImports%%
|
|
101
98
|
|
|
102
99
|
declare module '@granite-js/react-native' {
|
|
100
|
+
interface RegisterScreenInput {
|
|
101
|
+
%%pageInputRoutes%%
|
|
102
|
+
}
|
|
103
|
+
|
|
103
104
|
interface RegisterScreen {
|
|
104
105
|
%%pageRoutes%%
|
|
105
106
|
}
|
|
@@ -141,8 +142,7 @@ function transformTemplate(templateString, values) {
|
|
|
141
142
|
function generateRouterFile() {
|
|
142
143
|
const cwd = process.cwd();
|
|
143
144
|
function getPageFiles(dir, prefix = "") {
|
|
144
|
-
|
|
145
|
-
return files.reduce((acc, file) => {
|
|
145
|
+
return (0, fs.readdirSync)((0, path.join)(cwd, dir), { withFileTypes: true }).reduce((acc, file) => {
|
|
146
146
|
if (file.isDirectory()) return [...acc, ...getPageFiles(`${dir}/${file.name}`, `${prefix}${file.name}/`)];
|
|
147
147
|
if (file.name.endsWith(".tsx") || file.name.endsWith(".ts")) {
|
|
148
148
|
const name = (0, path.parse)(file.name).name;
|
|
@@ -155,30 +155,29 @@ function generateRouterFile() {
|
|
|
155
155
|
const allPages = getPageFiles("pages");
|
|
156
156
|
const exportRouteMap = new Map(allPages.map((page) => [page, checkExportRoute((0, path.join)(cwd, "pages", page))]));
|
|
157
157
|
const pageFiles = allPages.filter((page) => !page.startsWith("_") && exportRouteMap.get(page));
|
|
158
|
-
const pageImports = pageFiles.map((page) => {
|
|
159
|
-
const componentName = getComponentName(page);
|
|
160
|
-
const pagePath = getPath(page);
|
|
161
|
-
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
162
|
-
componentName,
|
|
163
|
-
pagePath
|
|
164
|
-
});
|
|
165
|
-
}).join("\n");
|
|
166
|
-
const pageRoutes = pageFiles.map((page) => {
|
|
167
|
-
const componentName = getComponentName(page);
|
|
168
|
-
const pagePath = getPath(page);
|
|
169
|
-
return transformTemplate(" '%%pagePath%%': ReturnType<typeof _%%componentName%%Route.useParams>;", {
|
|
170
|
-
componentName,
|
|
171
|
-
pagePath
|
|
172
|
-
});
|
|
173
|
-
}).join("\n");
|
|
174
158
|
const generatedContent = transformTemplate(ROUTER_GEN_TEMPLATE, {
|
|
175
|
-
pageImports
|
|
176
|
-
|
|
159
|
+
pageImports: pageFiles.map((page) => {
|
|
160
|
+
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
161
|
+
componentName: getComponentName(page),
|
|
162
|
+
pagePath: getPath(page)
|
|
163
|
+
});
|
|
164
|
+
}).join("\n"),
|
|
165
|
+
pageInputRoutes: pageFiles.map((page) => {
|
|
166
|
+
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_inputType'];", {
|
|
167
|
+
componentName: getComponentName(page),
|
|
168
|
+
pagePath: getPath(page)
|
|
169
|
+
});
|
|
170
|
+
}).join("\n"),
|
|
171
|
+
pageRoutes: pageFiles.map((page) => {
|
|
172
|
+
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_outputType'];", {
|
|
173
|
+
componentName: getComponentName(page),
|
|
174
|
+
pagePath: getPath(page)
|
|
175
|
+
});
|
|
176
|
+
}).join("\n")
|
|
177
177
|
});
|
|
178
178
|
const routerFilePath = (0, path.join)(cwd, "src", "router.gen.ts");
|
|
179
179
|
if ((0, fs.existsSync)(routerFilePath)) {
|
|
180
|
-
|
|
181
|
-
if (existingContent === generatedContent) return;
|
|
180
|
+
if ((0, fs.readFileSync)(routerFilePath, "utf-8") === generatedContent) return;
|
|
182
181
|
}
|
|
183
182
|
(0, fs.writeFileSync)(routerFilePath, generatedContent);
|
|
184
183
|
console.log("✅ Router file generated successfully!");
|
|
@@ -221,9 +220,7 @@ function watchRouter() {
|
|
|
221
220
|
cwd: process.cwd()
|
|
222
221
|
});
|
|
223
222
|
const handleAdd = async (path$1) => {
|
|
224
|
-
|
|
225
|
-
const code = await (0, fs_promises.readFile)(file, "utf8");
|
|
226
|
-
if (code !== "") return;
|
|
223
|
+
if (await (0, fs_promises.readFile)((0, path.join)(process.cwd(), path$1), "utf8") !== "") return;
|
|
227
224
|
const filename = (0, path.parse)(path$1).name;
|
|
228
225
|
if (filename.startsWith("_")) switch (filename) {
|
|
229
226
|
case "_layout":
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,20 +15,18 @@ function checkExportRoute(path) {
|
|
|
15
15
|
syntax: "typescript",
|
|
16
16
|
tsx: true
|
|
17
17
|
});
|
|
18
|
-
|
|
18
|
+
if (ast.body.some((node) => {
|
|
19
19
|
if (node.type !== "ExportNamedDeclaration") return false;
|
|
20
20
|
return node.specifiers?.some((specifier) => {
|
|
21
21
|
if (specifier.type !== "ExportSpecifier") return false;
|
|
22
22
|
return specifier.orig?.value === "Route";
|
|
23
23
|
});
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const hasExportNamedVariable = ast.body.some((node) => {
|
|
24
|
+
})) return true;
|
|
25
|
+
return ast.body.some((node) => {
|
|
27
26
|
if (node.type !== "ExportDeclaration") return false;
|
|
28
27
|
if (node.declaration.type !== "VariableDeclaration") return false;
|
|
29
28
|
return node.declaration.declarations.some((declaration) => declaration.id.type === "Identifier" && declaration.id.value === "Route");
|
|
30
29
|
});
|
|
31
|
-
return hasExportNamedVariable;
|
|
32
30
|
} catch {
|
|
33
31
|
return false;
|
|
34
32
|
}
|
|
@@ -37,11 +35,9 @@ function checkExportRoute(path) {
|
|
|
37
35
|
//#endregion
|
|
38
36
|
//#region src/getComponentName.ts
|
|
39
37
|
function getComponentName(filePath) {
|
|
40
|
-
const
|
|
41
|
-
const segments = path.split("/").filter(Boolean);
|
|
38
|
+
const segments = filePath.replace(/^pages\//, "").replace(/\.(tsx|ts)$/, "").split("/").filter(Boolean);
|
|
42
39
|
if (segments[segments.length - 1] === "index") segments.pop();
|
|
43
|
-
|
|
44
|
-
return componentName || "Index";
|
|
40
|
+
return segments.map((segment) => pascalCase(segment)).join("") || "Index";
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
//#endregion
|
|
@@ -77,6 +73,10 @@ const ROUTER_GEN_TEMPLATE = `/* eslint-disable */
|
|
|
77
73
|
%%pageImports%%
|
|
78
74
|
|
|
79
75
|
declare module '@granite-js/react-native' {
|
|
76
|
+
interface RegisterScreenInput {
|
|
77
|
+
%%pageInputRoutes%%
|
|
78
|
+
}
|
|
79
|
+
|
|
80
80
|
interface RegisterScreen {
|
|
81
81
|
%%pageRoutes%%
|
|
82
82
|
}
|
|
@@ -118,8 +118,7 @@ function transformTemplate(templateString, values) {
|
|
|
118
118
|
function generateRouterFile() {
|
|
119
119
|
const cwd = process.cwd();
|
|
120
120
|
function getPageFiles(dir, prefix = "") {
|
|
121
|
-
|
|
122
|
-
return files.reduce((acc, file) => {
|
|
121
|
+
return readdirSync(join(cwd, dir), { withFileTypes: true }).reduce((acc, file) => {
|
|
123
122
|
if (file.isDirectory()) return [...acc, ...getPageFiles(`${dir}/${file.name}`, `${prefix}${file.name}/`)];
|
|
124
123
|
if (file.name.endsWith(".tsx") || file.name.endsWith(".ts")) {
|
|
125
124
|
const name = parse(file.name).name;
|
|
@@ -132,30 +131,29 @@ function generateRouterFile() {
|
|
|
132
131
|
const allPages = getPageFiles("pages");
|
|
133
132
|
const exportRouteMap = new Map(allPages.map((page) => [page, checkExportRoute(join(cwd, "pages", page))]));
|
|
134
133
|
const pageFiles = allPages.filter((page) => !page.startsWith("_") && exportRouteMap.get(page));
|
|
135
|
-
const pageImports = pageFiles.map((page) => {
|
|
136
|
-
const componentName = getComponentName(page);
|
|
137
|
-
const pagePath = getPath(page);
|
|
138
|
-
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
139
|
-
componentName,
|
|
140
|
-
pagePath
|
|
141
|
-
});
|
|
142
|
-
}).join("\n");
|
|
143
|
-
const pageRoutes = pageFiles.map((page) => {
|
|
144
|
-
const componentName = getComponentName(page);
|
|
145
|
-
const pagePath = getPath(page);
|
|
146
|
-
return transformTemplate(" '%%pagePath%%': ReturnType<typeof _%%componentName%%Route.useParams>;", {
|
|
147
|
-
componentName,
|
|
148
|
-
pagePath
|
|
149
|
-
});
|
|
150
|
-
}).join("\n");
|
|
151
134
|
const generatedContent = transformTemplate(ROUTER_GEN_TEMPLATE, {
|
|
152
|
-
pageImports
|
|
153
|
-
|
|
135
|
+
pageImports: pageFiles.map((page) => {
|
|
136
|
+
return transformTemplate("import { Route as _%%componentName%%Route } from '../pages%%pagePath%%';", {
|
|
137
|
+
componentName: getComponentName(page),
|
|
138
|
+
pagePath: getPath(page)
|
|
139
|
+
});
|
|
140
|
+
}).join("\n"),
|
|
141
|
+
pageInputRoutes: pageFiles.map((page) => {
|
|
142
|
+
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_inputType'];", {
|
|
143
|
+
componentName: getComponentName(page),
|
|
144
|
+
pagePath: getPath(page)
|
|
145
|
+
});
|
|
146
|
+
}).join("\n"),
|
|
147
|
+
pageRoutes: pageFiles.map((page) => {
|
|
148
|
+
return transformTemplate(" '%%pagePath%%': (typeof _%%componentName%%Route)['_outputType'];", {
|
|
149
|
+
componentName: getComponentName(page),
|
|
150
|
+
pagePath: getPath(page)
|
|
151
|
+
});
|
|
152
|
+
}).join("\n")
|
|
154
153
|
});
|
|
155
154
|
const routerFilePath = join(cwd, "src", "router.gen.ts");
|
|
156
155
|
if (existsSync(routerFilePath)) {
|
|
157
|
-
|
|
158
|
-
if (existingContent === generatedContent) return;
|
|
156
|
+
if (readFileSync(routerFilePath, "utf-8") === generatedContent) return;
|
|
159
157
|
}
|
|
160
158
|
writeFileSync(routerFilePath, generatedContent);
|
|
161
159
|
console.log("✅ Router file generated successfully!");
|
|
@@ -198,9 +196,7 @@ function watchRouter() {
|
|
|
198
196
|
cwd: process.cwd()
|
|
199
197
|
});
|
|
200
198
|
const handleAdd = async (path) => {
|
|
201
|
-
|
|
202
|
-
const code = await readFile(file, "utf8");
|
|
203
|
-
if (code !== "") return;
|
|
199
|
+
if (await readFile(join(process.cwd(), path), "utf8") !== "") return;
|
|
204
200
|
const filename = parse(path).name;
|
|
205
201
|
if (filename.startsWith("_")) switch (filename) {
|
|
206
202
|
case "_layout":
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@granite-js/plugin-router",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.32",
|
|
5
5
|
"description": "A Route Generator for Granite project",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
@@ -35,13 +35,13 @@
|
|
|
35
35
|
],
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/node": "^22.10.2",
|
|
38
|
-
"@vitest/coverage-v8": "^
|
|
39
|
-
"tsdown": "^0.
|
|
38
|
+
"@vitest/coverage-v8": "^4.0.12",
|
|
39
|
+
"tsdown": "^0.16.5",
|
|
40
40
|
"typescript": "5.8.3",
|
|
41
|
-
"vitest": "^
|
|
41
|
+
"vitest": "^4.0.12"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@granite-js/plugin-core": "0.1.
|
|
44
|
+
"@granite-js/plugin-core": "0.1.32",
|
|
45
45
|
"@swc/core": "1.5.24",
|
|
46
46
|
"chokidar": "4.0.1",
|
|
47
47
|
"es-toolkit": "^1.39.8"
|