Skip to content

Commit 47a8fe5

Browse files
author
xiazhigang
committed
新增 新增配置路由入口位置项
1 parent 22f0f2e commit 47a8fe5

File tree

7 files changed

+31
-23
lines changed

7 files changed

+31
-23
lines changed

.codegen/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"routerEntry": "content",
23
"iconRoot": "./example/icons/source",
34
"iconTarget": "./example/icons",
45
"templateFile": "./.codegen/templates/icon.handlebars",

example/routes/routes.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"tabs": {
3-
"Home": "pages/tabs/Home/index"
3+
"home": "pages/tabs/home/index"
44
},
55
"packages": {
66
"pages/packages/pkgname": [
@@ -20,36 +20,36 @@
2020
}
2121
],
2222
"pages": [
23-
"pages/tabs/Home/index",
2423
"pages/content/index",
25-
"pages/question/index"
24+
"pages/question/index",
25+
"pages/tabs/home/index"
2626
],
2727
"source": {
2828
"content": "pages/content/index",
2929
"center": "pages/packages/pkgname/center/index",
3030
"user": "pages/packages/pkgname/user/index",
3131
"question": "pages/question/index",
32-
"Home": "pages/tabs/Home/index"
32+
"home": "pages/tabs/home/index"
3333
},
3434
"routeMap": {
3535
"content": "/pages/content/index",
3636
"center": "/pages/packages/pkgname/center/index",
3737
"user": "/pages/packages/pkgname/user/index",
3838
"question": "/pages/question/index",
39-
"Home": "/pages/tabs/Home/index"
39+
"home": "/pages/tabs/home/index"
4040
},
4141
"names": [
4242
"content",
4343
"center",
4444
"user",
4545
"question",
46-
"Home"
46+
"home"
4747
],
4848
"customRoutes": {
4949
"/pages/content/index": "/content",
5050
"/pages/packages/pkgname/center/index": "/center",
5151
"/pages/packages/pkgname/user/index": "/user",
5252
"/pages/question/index": "/question",
53-
"/pages/tabs/Home/index": "/-home"
53+
"/pages/tabs/home/index": "/home"
5454
}
5555
}

example/routes/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export type RoutesName = "content" | "center" | "user" | "question" | "Home"
1+
export type RoutesName = "content" | "center" | "user" | "question" | "home"
22

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yocdev/tarocodegen",
3-
"version": "1.1.10",
3+
"version": "1.1.11",
44
"author": "[email protected]",
55
"license": "MIT",
66
"bin": {

src/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ export const addToConfig = (configPart: any) => {
2727
return { ...config, ...configPart };
2828
};
2929

30-
export default config
30+
export default config;

src/helper/readConfig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import readExtraConfig from "../util/readExtraConfig";
66
const configSchema = {
77
type: "object",
88
properties: {
9+
routerEntry: { type: "string" },
910
iconRoot: { type: "string" },
1011
iconTarget: { type: "string" },
1112
templateFile: { type: "string" },
12-
prefix: {type: 'string'},
13+
prefix: { type: "string" },
1314
svgr: {
1415
type: "object",
1516
properties: {

src/routes/index.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import rd from "rd";
44
import Ajv from "ajv";
55
import chalk from "chalk";
66
import config from "../config";
7+
import readConfig from "../helper/readConfig";
78

89
const packageConfig = {
910
type: "object",
@@ -18,14 +19,17 @@ const pkgConfigValidate = ajv.compile(packageConfig);
1819

1920
const normalRouterReg = /pages\/([a-zA-Z]+)\/index.(tsx|js|ts|jsx)$/;
2021
const tabRouterReg = /pages\/tabs\/([a-zA-Z]+)\/index.(tsx|js|ts|jsx)$/;
21-
const packageRouterReg = /pages\/packages\/([a-zA-Z]+)\/([a-zA-Z]+)\/index.(tsx|js|ts|jsx)$/;
22+
const packageRouterReg =
23+
/pages\/packages\/([a-zA-Z]+)\/([a-zA-Z]+)\/index.(tsx|js|ts|jsx)$/;
2224

2325
enum RouteType {
2426
NORMAL = "normal",
2527
TAB = "tab",
2628
PACKAGE = "package",
2729
}
2830

31+
const extraConfig = readConfig();
32+
2933
export default function generateRoutes() {
3034
// 获得当前执行node命令时候的文件夹目录名
3135
const commandPath = process.cwd();
@@ -91,10 +95,11 @@ export default function generateRoutes() {
9195
routes.source[name] = routeUsefulPath;
9296
routes.routeMap[name] = `/${routeUsefulPath}`;
9397
const shortLink = name.replace(/([A-Z])/g, "-$1").toLowerCase();
94-
routes.customRoutes[`/${routeUsefulPath}`] =
95-
`/${shortLink.startsWith('-') ? shortLink.slice(1) : shortLink}`;
98+
routes.customRoutes[`/${routeUsefulPath}`] = `/${
99+
shortLink.startsWith("-") ? shortLink.slice(1) : shortLink
100+
}`;
96101
routes.names.push(name);
97-
routes.pages = moveHomeToFirst(routes.pages)
102+
routes.pages = moveHomeToFirst(routes.pages, extraConfig.routerEntry);
98103
}
99104
}
100105
});
@@ -177,17 +182,18 @@ function removeExtname(filepath: string) {
177182

178183
// 入口页面置顶
179184
// @ts-ignore
180-
function moveHomeToFirst(pages: string[]) {
181-
const pagesCopy = pages
182-
const homeObject = pages.filter((it) =>
183-
/home|Home/.test(it)
184-
);
185+
function moveHomeToFirst(pages: string[], entryPage?: string) {
186+
const entryPageReg = entryPage
187+
? new RegExp(`/home|Home|${entryPage}/`)
188+
: new RegExp(`/home|Home/`);
189+
const pagesCopy = pages;
190+
const homeObject = pages.filter((it) => entryPageReg.test(it));
185191
if (homeObject.length > 0) {
186-
const homeIndex = pages.indexOf(homeObject[0] as string)
192+
const homeIndex = pages.indexOf(homeObject[0] as string);
187193
if (homeIndex !== 0) {
188-
pagesCopy.splice(homeIndex,1)
194+
pagesCopy.splice(homeIndex, 1);
189195
pagesCopy.unshift(homeObject[0] as string);
190196
}
191197
}
192-
return pagesCopy
198+
return pagesCopy;
193199
}

0 commit comments

Comments
 (0)