@peng_kai/kit 0.1.2 → 0.1.3

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,99 @@
1
+ import fs from 'node:fs/promises';
2
+ import fg from 'fast-glob';
3
+ import parser from '@babel/parser';
4
+ import traverse from '@babel/traverse';
5
+ import generator from '@babel/generator';
6
+ import types from '@babel/types';
7
+ import chokidar from 'chokidar';
8
+ import debounce from 'lodash-es/debounce';
9
+
10
+ const routeMap = new Map<string, string[]>();
11
+ const routeWatcher = chokidar.watch('src/pages/**/route.ts');
12
+ const routeTypePath = fg.sync('**/**/router.startup.ts')?.[0];
13
+
14
+ if (routeTypePath)
15
+ console.log('🧐 监听路由文件变化中...');
16
+ else
17
+ throw new Error('未发现 router.startup.ts 文件');
18
+
19
+ routeWatcher.on('add', routeFileChangeHandler);
20
+ routeWatcher.on('change', routeFileChangeHandler);
21
+ routeWatcher.on('unlink', routeFileChangeHandler);
22
+
23
+ async function routeFileChangeHandler(path: string) {
24
+ const access = await fs.access(path).then(() => true).catch(() => false);
25
+
26
+ if (access) {
27
+ const names = await getRouteNames(path);
28
+ routeMap.set(path, names);
29
+ }
30
+ else {
31
+ routeMap.delete(path);
32
+ }
33
+
34
+ let allNames = [...routeMap.values()].flat();
35
+ allNames = [...new Set(allNames).values()].sort();
36
+ await writeRouteName(allNames);
37
+ }
38
+
39
+ async function getRouteNames(filePath: string) {
40
+ const code = await fs.readFile(filePath, 'utf8');
41
+ const ast = parser.parse(code, {
42
+ sourceType: 'module',
43
+ plugins: ['typescript'],
44
+ });
45
+ const names: string[] = [];
46
+
47
+ traverse.default(ast, {
48
+ enter(path) {
49
+ if (
50
+ path.node.type === 'ObjectProperty'
51
+ && path.node.key.name === 'name'
52
+ && typeof path.node.value.value === 'string'
53
+ )
54
+ names.push(path.node.value.value);
55
+ },
56
+ });
57
+
58
+ return names;
59
+ }
60
+
61
+ const writeRouteName = debounce(async (names: string[]) => {
62
+ const typeCode = await fs.readFile(routeTypePath, 'utf8');
63
+ const typeAst = parser.parse(typeCode, {
64
+ sourceType: 'module',
65
+ plugins: ['typescript'],
66
+ });
67
+ let appRouteNamesNode: any = null;
68
+
69
+ traverse.default(typeAst, {
70
+ enter(path) {
71
+ if (
72
+ path.node.type === 'TSInterfaceDeclaration'
73
+ && path.node.id.name === 'AppRouteNames'
74
+ )
75
+ appRouteNamesNode = path.node;
76
+ },
77
+ });
78
+
79
+ if (appRouteNamesNode) {
80
+ // 删除现有属性
81
+ appRouteNamesNode.body.body = appRouteNamesNode.body.body.filter(
82
+ node => !types.isTSPropertySignature(node),
83
+ );
84
+
85
+ // 添加新属性
86
+ const newProperties = names.map(name =>
87
+ types.tsPropertySignature(
88
+ types.identifier(name),
89
+ types.tsTypeAnnotation(types.tsBooleanKeyword()),
90
+ ),
91
+ );
92
+ appRouteNamesNode.body.body.push(...newProperties);
93
+
94
+ // 生成修改后的代码
95
+ const modifiedCode = generator.default(typeAst, {}, typeCode).code;
96
+
97
+ fs.writeFile(routeTypePath, modifiedCode);
98
+ }
99
+ }, 2000);
package/eslint.config.js CHANGED
@@ -1,3 +1,3 @@
1
- import { config } from '@peng_kai/lint/antfu.mjs';
1
+ import { getAntFuConfig } from '@peng_kai/lint/antfu.mjs';
2
2
 
3
- export default config;
3
+ export default await getAntFuConfig();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peng_kai/kit",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "ISC",
@@ -23,12 +23,18 @@
23
23
  "vue-router": "4.2.5"
24
24
  },
25
25
  "dependencies": {
26
+ "@babel/generator": "^7.23.5",
27
+ "@babel/parser": "^7.23.5",
28
+ "@babel/traverse": "^7.23.5",
29
+ "@babel/types": "^7.23.5",
26
30
  "@tanstack/vue-query": "^4.37.1",
27
31
  "@vueuse/core": "^10.6.1",
28
32
  "ant-design-vue": "^4.0.7",
29
33
  "axios": "^1.6.2",
30
34
  "bignumber.js": "^9.1.2",
35
+ "chokidar": "^3.5.3",
31
36
  "dayjs": "^1.11.10",
37
+ "fast-glob": "^3.3.2",
32
38
  "lodash-es": "^4.17.21",
33
39
  "nprogress": "^0.2.0",
34
40
  "vue": "^3.3.8",
@@ -36,10 +42,11 @@
36
42
  },
37
43
  "devDependencies": {
38
44
  "@antfu/eslint-config": "^1.2.1",
39
- "@peng_kai/lint": "^0.0.13",
45
+ "@peng_kai/lint": "^0.1.0",
40
46
  "@types/lodash-es": "^4.17.11",
41
47
  "@types/node": "18",
42
48
  "@types/nprogress": "^0.2.3",
49
+ "@unocss/eslint-plugin": "^0.57.7",
43
50
  "type-fest": "^4.8.1",
44
51
  "typescript": "^5.2.2",
45
52
  "vue-component-type-helpers": "^1.8.22"