@moluoxixi/create-app 2.0.4 → 2.0.402

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.
Files changed (259) hide show
  1. package/LICENSE.txt +21 -21
  2. package/README.md +129 -129
  3. package/dist/commands/create.d.ts +2 -1
  4. package/dist/commands/create.js +12 -8
  5. package/dist/commands/index.d.ts +1 -2
  6. package/dist/commands/index.js +1 -2
  7. package/dist/constants/index.d.ts +17 -0
  8. package/dist/constants/index.js +17 -0
  9. package/dist/generators/index.d.ts +3 -4
  10. package/dist/generators/index.js +3 -4
  11. package/dist/generators/project.d.ts +3 -2
  12. package/dist/generators/project.js +5 -4
  13. package/dist/generators/react.d.ts +4 -2
  14. package/dist/generators/react.js +10 -30
  15. package/dist/generators/vue.d.ts +4 -2
  16. package/dist/generators/vue.js +10 -31
  17. package/dist/index.d.ts +0 -1
  18. package/dist/index.js +21 -3
  19. package/dist/test.d.ts +5 -2
  20. package/dist/test.js +252 -206
  21. package/dist/types/ejs.d.ts +0 -1
  22. package/dist/types/ejs.js +0 -1
  23. package/dist/types/features.d.ts +0 -1
  24. package/dist/types/features.js +0 -1
  25. package/dist/types/index.d.ts +2 -4
  26. package/dist/types/index.js +2 -4
  27. package/dist/types/packageJson.d.ts +24 -0
  28. package/dist/types/packageJson.js +4 -0
  29. package/dist/types/viteConfig.d.ts +11 -9
  30. package/dist/types/viteConfig.js +0 -1
  31. package/dist/utils/deepMerge.d.ts +3 -1
  32. package/dist/utils/deepMerge.js +8 -1
  33. package/dist/utils/ejs.d.ts +3 -2
  34. package/dist/utils/ejs.js +2 -1
  35. package/dist/utils/featureMapping.d.ts +44 -0
  36. package/dist/utils/featureMapping.js +119 -0
  37. package/dist/utils/file.d.ts +22 -2
  38. package/dist/utils/file.js +35 -2
  39. package/dist/utils/generateFrameworkProject.d.ts +27 -0
  40. package/dist/utils/generateFrameworkProject.js +47 -0
  41. package/dist/utils/index.d.ts +11 -9
  42. package/dist/utils/index.js +11 -9
  43. package/dist/utils/install.d.ts +7 -4
  44. package/dist/utils/install.js +55 -12
  45. package/dist/utils/npmConfig.d.ts +15 -0
  46. package/dist/utils/npmConfig.js +79 -0
  47. package/dist/utils/prompts.d.ts +3 -2
  48. package/dist/utils/prompts.js +51 -5
  49. package/dist/utils/renderFeatures.d.ts +3 -2
  50. package/dist/utils/renderFeatures.js +41 -37
  51. package/dist/utils/renderTemplate.d.ts +13 -1
  52. package/dist/utils/renderTemplate.js +128 -61
  53. package/dist/utils/sortDependencies.d.ts +3 -2
  54. package/dist/utils/sortDependencies.js +3 -2
  55. package/dist/utils/viteConfigRender.d.ts +11 -14
  56. package/dist/utils/viteConfigRender.js +10 -173
  57. package/package.json +4 -2
  58. package/templates/common/base/.env +7 -7
  59. package/templates/common/base/.env.development +5 -5
  60. package/templates/common/base/.env.production +5 -5
  61. package/templates/common/base/package.json +23 -27
  62. package/templates/common/base/scripts/build.mts +37 -31
  63. package/templates/common/base/src/apis/index.ts +8 -8
  64. package/templates/common/base/src/apis/request.ts +43 -43
  65. package/templates/common/base/src/apis/services/example.ts +61 -40
  66. package/templates/common/base/src/apis/services/user.ts +33 -23
  67. package/templates/common/base/src/apis/types/common.ts +43 -28
  68. package/templates/common/base/src/apis/types/example.ts +15 -3
  69. package/templates/common/base/src/apis/types/index.ts +7 -7
  70. package/templates/common/base/src/apis/types/user.ts +39 -24
  71. package/templates/common/base/src/assets/fonts/index.css +7 -7
  72. package/templates/common/base/src/assets/styles/base.scss +72 -72
  73. package/templates/common/base/src/assets/styles/custom.scss +7 -7
  74. package/templates/common/base/src/assets/styles/tailwind.scss +6 -6
  75. package/templates/common/base/src/constants/index.ts +29 -29
  76. package/templates/common/base/src/utils/index.ts +88 -83
  77. package/templates/{vue/base/vite.config.ts → common/base/vite.config.ts.ejs} +28 -11
  78. package/templates/common/features/husky/.cz-config.cjs +35 -35
  79. package/templates/common/features/husky/commitlint.config.ts +32 -32
  80. package/templates/common/features/husky/node_modules/.bin/commitizen +17 -0
  81. package/templates/common/features/husky/node_modules/.bin/commitizen.CMD +12 -0
  82. package/templates/common/features/husky/node_modules/.bin/commitizen.ps1 +41 -0
  83. package/templates/common/features/husky/node_modules/.bin/commitlint +17 -0
  84. package/templates/common/features/husky/node_modules/.bin/commitlint.CMD +12 -0
  85. package/templates/common/features/husky/node_modules/.bin/commitlint.ps1 +41 -0
  86. package/templates/common/features/husky/node_modules/.bin/cz +17 -0
  87. package/templates/common/features/husky/node_modules/.bin/cz-cust +17 -0
  88. package/templates/common/features/husky/node_modules/.bin/cz-cust.CMD +12 -0
  89. package/templates/common/features/husky/node_modules/.bin/cz-cust.ps1 +41 -0
  90. package/templates/common/features/husky/node_modules/.bin/cz-customizable +17 -0
  91. package/templates/common/features/husky/node_modules/.bin/cz-customizable.CMD +12 -0
  92. package/templates/common/features/husky/node_modules/.bin/cz-customizable.ps1 +41 -0
  93. package/templates/common/features/husky/node_modules/.bin/cz.CMD +12 -0
  94. package/templates/common/features/husky/node_modules/.bin/cz.ps1 +41 -0
  95. package/templates/common/features/husky/node_modules/.bin/git-cz +17 -0
  96. package/templates/common/features/husky/node_modules/.bin/git-cz.CMD +12 -0
  97. package/templates/common/features/husky/node_modules/.bin/git-cz.ps1 +41 -0
  98. package/templates/common/features/husky/node_modules/.bin/husky +17 -0
  99. package/templates/common/features/husky/node_modules/.bin/husky.CMD +12 -0
  100. package/templates/common/features/husky/node_modules/.bin/husky.ps1 +41 -0
  101. package/templates/common/features/husky/node_modules/.bin/lint-staged +17 -0
  102. package/templates/common/features/husky/node_modules/.bin/lint-staged.CMD +12 -0
  103. package/templates/common/features/husky/node_modules/.bin/lint-staged.ps1 +41 -0
  104. package/templates/common/features/husky/package.json +6 -6
  105. package/templates/react/base/env.d.ts +19 -19
  106. package/templates/react/base/index.html +14 -14
  107. package/templates/react/base/node_modules/.bin/vite +17 -0
  108. package/templates/react/base/node_modules/.bin/vite.CMD +12 -0
  109. package/templates/react/base/node_modules/.bin/vite.ps1 +41 -0
  110. package/templates/react/base/package.json +18 -15
  111. package/templates/react/base/src/App.tsx +18 -18
  112. package/templates/react/base/src/assets/styles/main.scss +8 -8
  113. package/templates/react/base/src/pages/about/index.tsx +14 -14
  114. package/templates/react/base/src/pages/home/index.tsx +14 -14
  115. package/templates/react/base/tsconfig.app.json +17 -17
  116. package/templates/react/base/tsconfig.base.json +23 -23
  117. package/templates/react/base/tsconfig.json +12 -12
  118. package/templates/react/base/tsconfig.node.json +15 -15
  119. package/templates/react/features/ant-design/package.json +6 -7
  120. package/templates/react/features/eslint/node_modules/.bin/eslint +17 -0
  121. package/templates/react/features/eslint/node_modules/.bin/eslint.CMD +12 -0
  122. package/templates/react/features/eslint/node_modules/.bin/eslint.ps1 +41 -0
  123. package/templates/react/features/eslint/package.json +3 -3
  124. package/templates/react/features/i18n/package.json +6 -7
  125. package/templates/react/features/i18n/src/locales/index.ts +7 -0
  126. package/templates/{common/base → react/features/i18n}/src/locales/lang/en.ts +30 -30
  127. package/templates/{common/base → react/features/i18n}/src/locales/lang/es.ts +30 -30
  128. package/templates/{common/base → react/features/i18n}/src/locales/lang/zh.ts +30 -30
  129. package/templates/react/features/manualRoutes/src/router/routes.tsx +13 -0
  130. package/templates/react/features/pageRoutes/package.json +1 -2
  131. package/templates/react/features/router/package.json +1 -1
  132. package/templates/react/features/sentry/package.json +6 -7
  133. package/templates/react/features/sentry/src/utils/sentry.ts +10 -0
  134. package/templates/react/features/zustand/package.json +1 -1
  135. package/templates/react/features/zustand/src/stores/user.ts +42 -6
  136. package/templates/react/features/zustand/src/types/stores.ts +8 -1
  137. package/templates/vue/base/env.d.ts +27 -27
  138. package/templates/vue/base/index.html +14 -14
  139. package/templates/vue/base/node_modules/.bin/vite +17 -0
  140. package/templates/vue/base/node_modules/.bin/vite.CMD +12 -0
  141. package/templates/vue/base/node_modules/.bin/vite.ps1 +41 -0
  142. package/templates/vue/base/node_modules/.bin/vue-tsc +17 -0
  143. package/templates/vue/base/node_modules/.bin/vue-tsc.CMD +12 -0
  144. package/templates/vue/base/node_modules/.bin/vue-tsc.ps1 +41 -0
  145. package/templates/vue/base/package.json +16 -13
  146. package/templates/vue/base/src/App.vue +13 -13
  147. package/templates/vue/base/src/assets/styles/main.scss +9 -9
  148. package/templates/vue/base/src/directives/index.ts +36 -36
  149. package/templates/vue/base/src/layouts/index.ts +5 -7
  150. package/templates/vue/base/src/pages/about/index.vue +31 -31
  151. package/templates/vue/base/src/pages/home/index.vue +31 -31
  152. package/templates/vue/base/tsconfig.app.json +17 -17
  153. package/templates/vue/base/tsconfig.base.json +23 -23
  154. package/templates/vue/base/tsconfig.json +12 -12
  155. package/templates/vue/base/tsconfig.node.json +15 -15
  156. package/templates/vue/features/ant-design-vue/package.json +6 -7
  157. package/templates/vue/{base → features/ant-design-vue}/src/layouts/AntDesign.vue +86 -86
  158. package/templates/vue/features/element-plus/package.json +6 -7
  159. package/templates/vue/{base → features/element-plus}/src/assets/styles/element/index.scss +27 -27
  160. package/templates/vue/features/element-plus/src/components/SubMenu/index.ts +9 -0
  161. package/templates/vue/{base → features/element-plus}/src/components/SubMenu/src/_types/index.ts +6 -6
  162. package/templates/vue/{base → features/element-plus}/src/components/SubMenu/src/_types/props.ts +11 -11
  163. package/templates/vue/{base → features/element-plus}/src/components/SubMenu/src/index.vue +24 -24
  164. package/templates/vue/{base → features/element-plus}/src/layouts/element.vue +81 -81
  165. package/templates/vue/features/eslint/node_modules/.bin/eslint +17 -0
  166. package/templates/vue/features/eslint/node_modules/.bin/eslint.CMD +12 -0
  167. package/templates/vue/features/eslint/node_modules/.bin/eslint.ps1 +41 -0
  168. package/templates/vue/features/eslint/package.json +3 -3
  169. package/templates/vue/features/i18n/package.json +5 -5
  170. package/templates/vue/features/i18n/src/locales/index.ts +23 -16
  171. package/templates/vue/features/i18n/src/locales/lang/en.ts +30 -0
  172. package/templates/vue/features/i18n/src/locales/lang/es.ts +30 -0
  173. package/templates/vue/features/i18n/src/locales/lang/zh.ts +30 -0
  174. package/templates/vue/features/manualRoutes/src/router/routes.ts +35 -27
  175. package/templates/vue/features/pageRoutes/package.json +5 -5
  176. package/templates/vue/features/pinia/package.json +3 -2
  177. package/templates/vue/features/pinia/src/stores/index.ts +8 -0
  178. package/templates/vue/features/pinia/src/stores/system.ts +60 -0
  179. package/templates/vue/features/pinia/src/stores/user.ts +33 -13
  180. package/templates/vue/features/qiankun/package.json +5 -5
  181. package/templates/vue/features/qiankun/src/qiankun/index.ts +19 -0
  182. package/templates/vue/features/router/package.json +1 -1
  183. package/templates/vue/features/sentry/package.json +6 -6
  184. package/templates/vue/features/sentry/src/utils/sentry.ts +12 -0
  185. package/dist/commands/create.d.ts.map +0 -1
  186. package/dist/commands/create.js.map +0 -1
  187. package/dist/commands/index.d.ts.map +0 -1
  188. package/dist/commands/index.js.map +0 -1
  189. package/dist/generators/index.d.ts.map +0 -1
  190. package/dist/generators/index.js.map +0 -1
  191. package/dist/generators/project.d.ts.map +0 -1
  192. package/dist/generators/project.js.map +0 -1
  193. package/dist/generators/react.d.ts.map +0 -1
  194. package/dist/generators/react.js.map +0 -1
  195. package/dist/generators/vue.d.ts.map +0 -1
  196. package/dist/generators/vue.js.map +0 -1
  197. package/dist/index.d.ts.map +0 -1
  198. package/dist/index.js.map +0 -1
  199. package/dist/test.d.ts.map +0 -1
  200. package/dist/test.js.map +0 -1
  201. package/dist/types/ejs.d.ts.map +0 -1
  202. package/dist/types/ejs.js.map +0 -1
  203. package/dist/types/features.d.ts.map +0 -1
  204. package/dist/types/features.js.map +0 -1
  205. package/dist/types/index.d.ts.map +0 -1
  206. package/dist/types/index.js.map +0 -1
  207. package/dist/types/viteConfig.d.ts.map +0 -1
  208. package/dist/types/viteConfig.js.map +0 -1
  209. package/dist/utils/deepMerge.d.ts.map +0 -1
  210. package/dist/utils/deepMerge.js.map +0 -1
  211. package/dist/utils/ejs.d.ts.map +0 -1
  212. package/dist/utils/ejs.js.map +0 -1
  213. package/dist/utils/file.d.ts.map +0 -1
  214. package/dist/utils/file.js.map +0 -1
  215. package/dist/utils/index.d.ts.map +0 -1
  216. package/dist/utils/index.js.map +0 -1
  217. package/dist/utils/install.d.ts.map +0 -1
  218. package/dist/utils/install.js.map +0 -1
  219. package/dist/utils/prompts.d.ts.map +0 -1
  220. package/dist/utils/prompts.js.map +0 -1
  221. package/dist/utils/renderFeatures.d.ts.map +0 -1
  222. package/dist/utils/renderFeatures.js.map +0 -1
  223. package/dist/utils/renderTemplate.d.ts.map +0 -1
  224. package/dist/utils/renderTemplate.js.map +0 -1
  225. package/dist/utils/sortDependencies.d.ts.map +0 -1
  226. package/dist/utils/sortDependencies.js.map +0 -1
  227. package/dist/utils/viteConfigRender.d.ts.map +0 -1
  228. package/dist/utils/viteConfigRender.js.map +0 -1
  229. package/templates/common/base/pnpm-workspace.yaml +0 -37
  230. package/templates/common/features/husky/pnpm-workspace.yaml +0 -9
  231. package/templates/react/base/pnpm-workspace.yaml +0 -9
  232. package/templates/react/base/src/stores/user.ts +0 -39
  233. package/templates/react/base/vite.config.ts +0 -52
  234. package/templates/react/features/ant-design/pnpm-workspace.yaml +0 -5
  235. package/templates/react/features/eslint/pnpm-workspace.yaml +0 -6
  236. package/templates/react/features/i18n/pnpm-workspace.yaml +0 -5
  237. package/templates/react/features/pageRoutes/pnpm-workspace.yaml +0 -4
  238. package/templates/react/features/pageRoutes/vite.config.data.ts +0 -10
  239. package/templates/react/features/router/pnpm-workspace.yaml +0 -3
  240. package/templates/react/features/sentry/pnpm-workspace.yaml +0 -5
  241. package/templates/react/features/sentry/vite.config.data.ts +0 -23
  242. package/templates/react/features/zustand/pnpm-workspace.yaml +0 -3
  243. package/templates/vue/base/pnpm-workspace.yaml +0 -6
  244. package/templates/vue/base/src/components/SubMenu/index.ts +0 -9
  245. package/templates/vue/base/src/stores/index.ts +0 -16
  246. package/templates/vue/base/src/stores/modules/system.ts +0 -52
  247. package/templates/vue/base/src/stores/modules/user.ts +0 -48
  248. package/templates/vue/features/ant-design-vue/pnpm-workspace.yaml +0 -5
  249. package/templates/vue/features/element-plus/pnpm-workspace.yaml +0 -5
  250. package/templates/vue/features/element-plus/vite.config.data.ts +0 -15
  251. package/templates/vue/features/eslint/pnpm-workspace.yaml +0 -6
  252. package/templates/vue/features/i18n/pnpm-workspace.yaml +0 -3
  253. package/templates/vue/features/pageRoutes/pnpm-workspace.yaml +0 -4
  254. package/templates/vue/features/pageRoutes/vite.config.data.ts +0 -9
  255. package/templates/vue/features/pinia/pnpm-workspace.yaml +0 -4
  256. package/templates/vue/features/qiankun/pnpm-workspace.yaml +0 -3
  257. package/templates/vue/features/router/pnpm-workspace.yaml +0 -3
  258. package/templates/vue/features/sentry/pnpm-workspace.yaml +0 -4
  259. package/templates/vue/features/sentry/vite.config.data.ts +0 -23
@@ -1,96 +1,164 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import yaml from 'yaml';
4
- import { deepMerge } from './deepMerge.js';
5
- import { sortDependencies } from './sortDependencies.js';
3
+ import { FILE_CONSTANTS } from "../constants/index.js";
4
+ import { validatePath } from "./file.js";
5
+ import { deepMerge } from "./deepMerge.js";
6
+ import { sortDependencies } from "./sortDependencies.js";
6
7
  /**
7
8
  * 渲染模板到目标目录
8
9
  * 支持物理路径合并和配置文件深度合并
10
+ * @param src 源路径
11
+ * @param dest 目标路径
12
+ * @throws {Error} 如果路径不安全、文件操作失败
9
13
  */
10
14
  export function renderTemplate(src, dest) {
11
- const stats = fs.statSync(src);
12
- if (stats.isDirectory()) {
13
- // 如果是目录,递归处理
14
- if (path.basename(src) === 'node_modules') {
15
+ // 验证路径安全性
16
+ validatePath(src);
17
+ validatePath(dest);
18
+ try {
19
+ const stats = fs.statSync(src);
20
+ // 处理目录
21
+ if (stats.isDirectory()) {
22
+ // 跳过 node_modules 目录
23
+ if (path.basename(src) === FILE_CONSTANTS.NODE_MODULES) {
24
+ return;
25
+ }
26
+ fs.mkdirSync(dest, { recursive: true });
27
+ for (const file of fs.readdirSync(src)) {
28
+ // 验证文件名安全性
29
+ if (file.includes('..') || file.includes('~')) {
30
+ throw new Error(`不安全的文件名: ${file}`);
31
+ }
32
+ renderTemplate(path.resolve(src, file), path.resolve(dest, file));
33
+ }
15
34
  return;
16
35
  }
17
- fs.mkdirSync(dest, { recursive: true });
18
- for (const file of fs.readdirSync(src)) {
19
- renderTemplate(path.resolve(src, file), path.resolve(dest, file));
36
+ // 处理文件
37
+ const filename = path.basename(src);
38
+ // 跳过 .ejs 文件(由生成器单独处理)
39
+ if (filename.endsWith(FILE_CONSTANTS.EJS_EXTENSION)) {
40
+ return;
20
41
  }
21
- return;
22
- }
23
- const filename = path.basename(src);
24
- // 跳过 .ejs 文件(由生成器单独处理)
25
- if (filename.endsWith('.ejs')) {
26
- return;
42
+ // 处理 package.json - 深度合并
43
+ if (filename === FILE_CONSTANTS.PACKAGE_JSON) {
44
+ renderPackageJson(src, dest);
45
+ return;
46
+ }
47
+ // 跳过 pnpm-workspace.yaml(不再使用)
48
+ if (filename === FILE_CONSTANTS.PNPM_WORKSPACE_YAML) {
49
+ return;
50
+ }
51
+ // 处理特殊文件名转换(如 _gitignore -> .gitignore)
52
+ const targetFilename = renameFile(filename);
53
+ const targetPath = path.resolve(path.dirname(dest), targetFilename);
54
+ // 普通文件直接复制(后面的会覆盖前面的)
55
+ fs.copyFileSync(src, targetPath);
27
56
  }
28
- // 跳过 vite.config.data.ts 文件(用于数据驱动合并)
29
- if (filename === 'vite.config.data.ts') {
30
- return;
57
+ catch (error) {
58
+ const errorMessage = error instanceof Error ? error.message : String(error);
59
+ throw new Error(`渲染模板失败: ${src} -> ${dest}\n错误: ${errorMessage}`, {
60
+ cause: error,
61
+ });
31
62
  }
32
- // 处理 package.json - 深度合并
33
- if (filename === 'package.json') {
34
- renderPackageJson(src, dest);
35
- return;
63
+ }
64
+ /**
65
+ * 对 package.json 的依赖进行排序
66
+ * @param packageJson package.json 对象(会被修改)
67
+ */
68
+ function sortPackageJsonDependencies(packageJson) {
69
+ if (packageJson.dependencies) {
70
+ packageJson.dependencies = sortDependencies(packageJson.dependencies);
36
71
  }
37
- // 处理 pnpm-workspace.yaml - 深度合并
38
- if (filename === 'pnpm-workspace.yaml') {
39
- renderPnpmWorkspace(src, dest);
40
- return;
72
+ if (packageJson.devDependencies) {
73
+ packageJson.devDependencies = sortDependencies(packageJson.devDependencies);
41
74
  }
42
- // 处理特殊文件名转换(如 _gitignore -> .gitignore)
43
- const targetFilename = renameFile(filename);
44
- const targetPath = path.resolve(path.dirname(dest), targetFilename);
45
- // 普通文件直接复制(后面的会覆盖前面的)
46
- fs.copyFileSync(src, targetPath);
47
75
  }
48
76
  /**
49
77
  * 渲染 package.json - 深度合并
78
+ * @param src 源文件路径
79
+ * @param dest 目标文件路径
80
+ * @throws {Error} 如果文件读取失败、JSON 解析失败或写入失败
50
81
  */
51
82
  function renderPackageJson(src, dest) {
52
- const newPackage = JSON.parse(fs.readFileSync(src, 'utf-8'));
53
- if (fs.existsSync(dest)) {
54
- const existingPackage = JSON.parse(fs.readFileSync(dest, 'utf-8'));
55
- const merged = deepMerge(existingPackage, newPackage);
56
- // 排序依赖
57
- if (merged.dependencies) {
58
- merged.dependencies = sortDependencies(merged.dependencies);
83
+ try {
84
+ const srcContent = fs.readFileSync(src, 'utf-8');
85
+ const newPackage = JSON.parse(srcContent);
86
+ if (fs.existsSync(dest)) {
87
+ const destContent = fs.readFileSync(dest, 'utf-8');
88
+ const existingPackage = JSON.parse(destContent);
89
+ const merged = deepMerge(existingPackage, newPackage);
90
+ sortPackageJsonDependencies(merged);
91
+ fs.writeFileSync(dest, `${JSON.stringify(merged, null, 2)}\n`);
59
92
  }
60
- if (merged.devDependencies) {
61
- merged.devDependencies = sortDependencies(merged.devDependencies);
93
+ else {
94
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
95
+ fs.writeFileSync(dest, `${JSON.stringify(newPackage, null, 2)}\n`);
62
96
  }
63
- fs.writeFileSync(dest, `${JSON.stringify(merged, null, 2)}\n`);
64
97
  }
65
- else {
66
- fs.mkdirSync(path.dirname(dest), { recursive: true });
67
- fs.writeFileSync(dest, `${JSON.stringify(newPackage, null, 2)}\n`);
98
+ catch (error) {
99
+ const errorMessage = error instanceof Error ? error.message : String(error);
100
+ if (error instanceof SyntaxError) {
101
+ throw new TypeError(`package.json 解析失败: ${src}\n错误: ${errorMessage}`, { cause: error });
102
+ }
103
+ throw new Error(`处理 package.json 失败: ${src} -> ${dest}\n错误: ${errorMessage}`, { cause: error });
68
104
  }
69
105
  }
70
106
  /**
71
- * 渲染 pnpm-workspace.yaml - 深度合并
107
+ * 获取包管理器的默认版本号
108
+ * @param packageManager 包管理器类型
109
+ * @returns 包管理器版本字符串
72
110
  */
73
- function renderPnpmWorkspace(src, dest) {
74
- const newWorkspace = yaml.parse(fs.readFileSync(src, 'utf-8')) || {};
75
- if (fs.existsSync(dest)) {
76
- const existingWorkspace = yaml.parse(fs.readFileSync(dest, 'utf-8')) || {};
77
- const merged = deepMerge(existingWorkspace, newWorkspace);
78
- // 排序 catalogs 中的依赖
79
- if (merged.catalogs) {
80
- for (const key of Object.keys(merged.catalogs)) {
81
- merged.catalogs[key] = sortDependencies(merged.catalogs[key]);
82
- }
111
+ function getPackageManagerVersion(packageManager) {
112
+ const versions = {
113
+ pnpm: '10.8.0',
114
+ npm: '10.9.0',
115
+ yarn: '4.1.0',
116
+ };
117
+ return versions[packageManager] || versions.pnpm;
118
+ }
119
+ /**
120
+ * 更新 package.json 的元数据字段(name, description, author, packageManager)
121
+ * @param packageJsonPath package.json 文件路径
122
+ * @param projectName 项目名称
123
+ * @param description 项目描述
124
+ * @param author 作者
125
+ * @param packageManager 包管理器类型
126
+ * @throws {Error} 如果文件读取失败、JSON 解析失败或写入失败
127
+ */
128
+ export function updatePackageJsonMetadata(packageJsonPath, projectName, description, author, packageManager) {
129
+ if (!fs.existsSync(packageJsonPath)) {
130
+ return;
131
+ }
132
+ try {
133
+ const content = fs.readFileSync(packageJsonPath, 'utf-8');
134
+ const packageJson = JSON.parse(content);
135
+ // 更新元数据字段
136
+ packageJson.name = projectName;
137
+ if (description) {
138
+ packageJson.description = description;
83
139
  }
84
- fs.writeFileSync(dest, yaml.stringify(merged));
140
+ if (author) {
141
+ packageJson.author = author;
142
+ }
143
+ // 更新 packageManager 字段
144
+ packageJson.packageManager = `${packageManager}@${getPackageManagerVersion(packageManager)}`;
145
+ // 排序依赖
146
+ sortPackageJsonDependencies(packageJson);
147
+ fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`);
85
148
  }
86
- else {
87
- fs.mkdirSync(path.dirname(dest), { recursive: true });
88
- fs.writeFileSync(dest, yaml.stringify(newWorkspace));
149
+ catch (error) {
150
+ const errorMessage = error instanceof Error ? error.message : String(error);
151
+ if (error instanceof SyntaxError) {
152
+ throw new TypeError(`package.json 解析失败: ${packageJsonPath}\n错误: ${errorMessage}`, { cause: error });
153
+ }
154
+ throw new Error(`更新 package.json 元数据失败: ${packageJsonPath}\n错误: ${errorMessage}`, { cause: error });
89
155
  }
90
156
  }
91
157
  /**
92
158
  * 重命名特殊文件
93
159
  * 某些文件不能以 . 开头存在于模板中,需要特殊处理
160
+ * @param name 原始文件名
161
+ * @returns 转换后的文件名
94
162
  */
95
163
  function renameFile(name) {
96
164
  // _开头的文件转换为.开头
@@ -99,4 +167,3 @@ function renameFile(name) {
99
167
  }
100
168
  return name;
101
169
  }
102
- //# sourceMappingURL=renderTemplate.js.map
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * 对依赖对象进行排序
3
- * 按字母顺序排列
3
+ * 按字母顺序排列,@ 开头的包放在前面
4
+ * @param dependencies 依赖对象
5
+ * @returns 排序后的依赖对象
4
6
  */
5
7
  export declare function sortDependencies(dependencies: Record<string, string>): Record<string, string>;
6
- //# sourceMappingURL=sortDependencies.d.ts.map
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * 对依赖对象进行排序
3
- * 按字母顺序排列
3
+ * 按字母顺序排列,@ 开头的包放在前面
4
+ * @param dependencies 依赖对象
5
+ * @returns 排序后的依赖对象
4
6
  */
5
7
  export function sortDependencies(dependencies) {
6
8
  const sorted = {};
@@ -19,4 +21,3 @@ export function sortDependencies(dependencies) {
19
21
  }
20
22
  return sorted;
21
23
  }
22
- //# sourceMappingURL=sortDependencies.js.map
@@ -1,19 +1,16 @@
1
1
  /**
2
2
  * Vite 配置渲染工具
3
- * 参考 create-vue 的数据驱动方式
4
- * 从各 feature 的 vite.config.data.ts 中读取配置
3
+ * 简化版:直接返回配置数据给 EJS 模板处理
5
4
  */
6
- import type { ProjectConfigType, ViteConfigData } from '../types';
5
+ import type { ProjectConfigType } from '../types/index.ts';
7
6
  /**
8
- * 收集所有启用的 feature 的 vite.config.data
7
+ * 生成 vite.config.ts 的 EJS 模板数据
8
+ * @param config 项目配置对象
9
+ * @returns EJS 模板数据对象,包含框架类型、Sentry 配置、UI 库和路由模式
9
10
  */
10
- export declare function collectViteConfigData(config: ProjectConfigType): ViteConfigData[];
11
- /**
12
- * 合并 ViteConfigData 数组
13
- */
14
- export declare function mergeViteConfigData(dataList: ViteConfigData[]): ViteConfigData;
15
- /**
16
- * 生成 vite.config.ts 内容
17
- */
18
- export declare function renderViteConfig(config: ProjectConfigType): string;
19
- //# sourceMappingURL=viteConfigRender.d.ts.map
11
+ export declare function getViteConfigEjsData(config: ProjectConfigType): {
12
+ framework: string;
13
+ sentry?: boolean;
14
+ uiLibrary?: string;
15
+ routeMode?: string;
16
+ };
@@ -1,180 +1,17 @@
1
1
  /**
2
2
  * Vite 配置渲染工具
3
- * 参考 create-vue 的数据驱动方式
4
- * 从各 feature 的 vite.config.data.ts 中读取配置
3
+ * 简化版:直接返回配置数据给 EJS 模板处理
5
4
  */
6
- import fs from 'node:fs';
7
- import path from 'node:path';
8
- import { createJiti } from 'jiti';
9
- import { getTemplatesDir } from './file.js';
10
- /** Jiti 实例,用于动态加载 TypeScript 配置文件 */
11
- const jiti = createJiti(import.meta.url);
12
5
  /**
13
- * feature 目录读取 vite.config.data.ts
6
+ * 生成 vite.config.ts 的 EJS 模板数据
7
+ * @param config 项目配置对象
8
+ * @returns EJS 模板数据对象,包含框架类型、Sentry 配置、UI 库和路由模式
14
9
  */
15
- function readViteConfigData(framework, feature) {
16
- const templatesDir = getTemplatesDir();
17
- const dataPath = path.join(templatesDir, framework, 'features', feature, 'vite.config.data.ts');
18
- if (!fs.existsSync(dataPath)) {
19
- return null;
20
- }
21
- try {
22
- const module = jiti(dataPath);
23
- return module.default;
24
- }
25
- catch (error) {
26
- console.error(`Failed to load ${dataPath}:`, error);
27
- return null;
28
- }
29
- }
30
- /**
31
- * 收集所有启用的 feature 的 vite.config.data
32
- */
33
- export function collectViteConfigData(config) {
34
- const framework = config.framework;
35
- const dataList = [];
36
- // 根据配置收集各 feature 的配置数据
37
- const enabledFeatures = [];
38
- if (config.sentry) {
39
- enabledFeatures.push('sentry');
40
- }
41
- if (config.routeMode === 'file-system') {
42
- enabledFeatures.push('pageRoutes');
43
- }
44
- if (framework === 'vue' && config.uiLibrary === 'element-plus') {
45
- enabledFeatures.push('element-plus');
46
- }
47
- for (const feature of enabledFeatures) {
48
- const data = readViteConfigData(framework, feature);
49
- if (data) {
50
- dataList.push(data);
51
- }
52
- }
53
- return dataList;
54
- }
55
- /**
56
- * 合并 ViteConfigData 数组
57
- */
58
- export function mergeViteConfigData(dataList) {
59
- const merged = {
60
- imports: [],
61
- options: {},
62
- plugins: [],
10
+ export function getViteConfigEjsData(config) {
11
+ return {
12
+ framework: config.framework,
13
+ sentry: config.sentry,
14
+ uiLibrary: config.uiLibrary,
15
+ routeMode: config.routeMode,
63
16
  };
64
- for (const data of dataList) {
65
- if (data.imports) {
66
- merged.imports.push(...data.imports);
67
- }
68
- if (data.options) {
69
- Object.assign(merged.options, data.options);
70
- }
71
- if (data.plugins) {
72
- merged.plugins.push(...data.plugins);
73
- }
74
- if (data.css) {
75
- merged.css = data.css;
76
- }
77
- }
78
- return merged;
79
- }
80
- /**
81
- * 生成 vite.config.ts 内容
82
- */
83
- export function renderViteConfig(config) {
84
- const dataList = collectViteConfigData(config);
85
- const merged = mergeViteConfigData(dataList);
86
- const lines = [];
87
- lines.push('/**');
88
- lines.push(' * Vite 配置文件');
89
- lines.push(' * 基于 @moluoxixi/vite-config 的配置');
90
- lines.push(' */');
91
- lines.push('');
92
- lines.push('import path from \'node:path\'');
93
- lines.push('import process from \'node:process\'');
94
- lines.push('import cssModuleGlobalRootPlugin from \'@moluoxixi/css-module-global-root-plugin\'');
95
- lines.push('import { ViteConfig, wrapperEnv } from \'@moluoxixi/vite-config\'');
96
- // 添加 feature 导入
97
- if (merged.imports && merged.imports.length > 0) {
98
- for (const imp of merged.imports) {
99
- lines.push(imp);
100
- }
101
- }
102
- lines.push('import { loadEnv } from \'vite\'');
103
- lines.push('');
104
- lines.push('export default ViteConfig(');
105
- lines.push(' ({ mode }) => {');
106
- lines.push(' const env = loadEnv(mode!, process.cwd())');
107
- lines.push(' const viteEnv = wrapperEnv(env)');
108
- lines.push(' const rootPath = path.resolve()');
109
- lines.push(' const appCode = viteEnv.VITE_APP_CODE');
110
- lines.push(' const appTitle = viteEnv.VITE_APP_TITLE');
111
- lines.push(' const port = viteEnv.VITE_APP_PORT');
112
- lines.push(' return {');
113
- lines.push(' rootPath,');
114
- lines.push(' appTitle,');
115
- lines.push(' appCode,');
116
- lines.push(' port,');
117
- // 框架配置
118
- if (config.framework === 'vue') {
119
- lines.push(' vue: true,');
120
- }
121
- else {
122
- lines.push(' react: true,');
123
- }
124
- lines.push(' autoComponent: true,');
125
- // 添加 feature options
126
- if (merged.options) {
127
- for (const [key, value] of Object.entries(merged.options)) {
128
- if (typeof value === 'boolean') {
129
- lines.push(` ${key}: ${value},`);
130
- }
131
- else if (typeof value === 'string') {
132
- lines.push(` ${key}: '${value}',`);
133
- }
134
- else {
135
- lines.push(` ${key}: ${JSON.stringify(value)},`);
136
- }
137
- }
138
- }
139
- lines.push(' viteConfig: {');
140
- lines.push(' plugins: [');
141
- // 添加 feature plugins
142
- if (merged.plugins && merged.plugins.length > 0) {
143
- for (const plugin of merged.plugins) {
144
- lines.push(` ${plugin},`);
145
- }
146
- }
147
- lines.push(' ],');
148
- lines.push(' server: {');
149
- lines.push(' proxy: {');
150
- lines.push(' \'/api\': {');
151
- lines.push(' changeOrigin: true,');
152
- lines.push(' target: \'http://localhost:3000\',');
153
- lines.push(' },');
154
- lines.push(' },');
155
- lines.push(' },');
156
- lines.push(' css: {');
157
- lines.push(' postcss: {');
158
- lines.push(' plugins: [');
159
- lines.push(' cssModuleGlobalRootPlugin,');
160
- lines.push(' ],');
161
- lines.push(' },');
162
- lines.push(' preprocessorOptions: {');
163
- lines.push(' scss: {');
164
- lines.push(' silenceDeprecations: [\'legacy-js-api\'],');
165
- lines.push(' api: \'modern-compiler\',');
166
- // 添加 CSS additionalData
167
- if (merged.css?.additionalData) {
168
- lines.push(` ${merged.css.additionalData}`);
169
- }
170
- lines.push(' },');
171
- lines.push(' },');
172
- lines.push(' },');
173
- lines.push(' },');
174
- lines.push(' }');
175
- lines.push(' },');
176
- lines.push(')');
177
- lines.push('');
178
- return lines.join('\n');
179
17
  }
180
- //# sourceMappingURL=viteConfigRender.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/create-app",
3
- "version": "2.0.4",
3
+ "version": "2.0.402",
4
4
  "type": "module",
5
5
  "description": "Create Vue/React projects with atomic layered architecture",
6
6
  "main": "dist/index.js",
@@ -49,6 +49,7 @@
49
49
  "yaml": "^2.6.0"
50
50
  },
51
51
  "devDependencies": {
52
+ "vite": "^6.2.4",
52
53
  "@commitlint/cli": "^19.8.0",
53
54
  "@commitlint/config-conventional": "^19.8.0",
54
55
  "@moluoxixi/eslint-config": "latest",
@@ -56,6 +57,7 @@
56
57
  "@types/fs-extra": "^11.0.4",
57
58
  "@types/inquirer": "^9.0.7",
58
59
  "@types/lodash-es": "^4.17.12",
60
+ "@vue-macros/volar": "^3.0.0-beta.15",
59
61
  "@types/node": "^22.14.0",
60
62
  "commitizen": "^4.3.1",
61
63
  "cz-customizable": "^7.4.0",
@@ -70,7 +72,7 @@
70
72
  },
71
73
  "scripts": {
72
74
  "dev": "tsx src/index.ts",
73
- "build": "tsc",
75
+ "build": "tsc --build tsconfig.build.json",
74
76
  "test": "tsx src/test.ts --min",
75
77
  "test:all": "tsx src/test.ts",
76
78
  "type-check": "tsc --noEmit",
@@ -1,7 +1,7 @@
1
- # 环境变量配置
2
- # 所有环境通用
3
-
4
- VITE_APP_CODE=app
5
- VITE_APP_TITLE=My App
6
- VITE_APP_PORT=3000
7
- VITE_APP_VERSION=0.0.0
1
+ # 环境变量配置
2
+ # 所有环境通用
3
+
4
+ VITE_APP_CODE=app
5
+ VITE_APP_TITLE=My App
6
+ VITE_APP_PORT=3000
7
+ VITE_APP_VERSION=0.0.0
@@ -1,5 +1,5 @@
1
- # 开发环境变量配置
2
-
3
- VITE_APP_ENV=development
4
- VITE_API_BASE_URL=http://localhost:3000
5
- VITE_SENTRY=false
1
+ # 开发环境变量配置
2
+
3
+ VITE_APP_ENV=development
4
+ VITE_API_BASE_URL=http://localhost:3000
5
+ VITE_SENTRY=false
@@ -1,5 +1,5 @@
1
- # 生产环境变量配置
2
-
3
- VITE_APP_ENV=production
4
- VITE_API_BASE_URL=https://api.example.com
5
- VITE_SENTRY=true
1
+ # 生产环境变量配置
2
+
3
+ VITE_APP_ENV=production
4
+ VITE_API_BASE_URL=https://api.example.com
5
+ VITE_SENTRY=true
@@ -21,36 +21,32 @@
21
21
  }
22
22
  },
23
23
  "dependencies": {
24
- "@moluoxixi/ajax-package": "catalog:build",
25
- "@moluoxixi/class-names": "catalog:build",
26
- "crypto-js": "catalog:build",
27
- "dayjs": "catalog:build",
28
- "lodash-es": "catalog:build",
29
- "radash": "catalog:build",
30
- "uuid": "catalog:build"
24
+ "@moluoxixi/ajax-package": "latest",
25
+ "@moluoxixi/class-names": "latest",
26
+ "crypto-js": "^4.2.0",
27
+ "dayjs": "^1.11.13",
28
+ "lodash-es": "^4.17.21",
29
+ "radash": "^12.1.0",
30
+ "uuid": "^11.1.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@moluoxixi/ajax-package": "catalog:dev",
34
- "@moluoxixi/css-module-global-root-plugin": "catalog:dev",
35
- "@moluoxixi/vite-config": "catalog:dev",
36
- "@types/crypto-js": "catalog:type",
37
- "@types/lodash-es": "catalog:type",
38
- "@types/node": "catalog:type",
39
- "@types/uuid": "catalog:type",
40
- "autoprefixer": "catalog:dev",
41
- "compressing": "catalog:dev",
42
- "dotenv": "catalog:dev",
43
- "jiti": "catalog:dev",
44
- "postcss": "catalog:dev",
45
- "sass": "catalog:dev",
46
- "sass-embedded": "catalog:dev",
47
- "sass-loader": "catalog:dev",
48
- "tailwindcss": "catalog:dev",
49
- "tsx": "catalog:dev",
50
- "typescript": "catalog:dev",
51
- "vite": "catalog:dev"
33
+ "@types/crypto-js": "^4.2.2",
34
+ "@types/lodash-es": "^4.17.12",
35
+ "@types/node": "^22.14.0",
36
+ "@types/uuid": "^10.0.0",
37
+ "autoprefixer": "^10.4.20",
38
+ "compressing": "^1.10.1",
39
+ "dotenv": "^16.5.0",
40
+ "jiti": "^2.4.2",
41
+ "postcss": "^8.5.3",
42
+ "sass": "^1.87.0",
43
+ "sass-embedded": "^1.87.0",
44
+ "sass-loader": "^16.0.5",
45
+ "tailwindcss": "^4.0.0",
46
+ "tsx": "^4.20.5",
47
+ "typescript": "~5.8.0",
48
+ "vite": "^6.2.4"
52
49
  },
53
50
  "author": "",
54
51
  "license": "MIT"
55
52
  }
56
-