@modern-js/main-doc 2.0.0-beta.1 → 2.0.0-beta.2

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 (157) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/build.md +1 -1
  3. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/dev.md +1 -1
  4. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/lint.md +1 -1
  5. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/new.md +1 -2
  6. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/start.md +1 -1
  7. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/test.md +1 -1
  8. package/en/docusaurus-plugin-content-docs/current/apis/app/overview.md +1 -1
  9. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/auto-actions.md +1 -1
  10. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/connect.md +1 -1
  11. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/create-app.md +1 -1
  12. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/create-store.md +1 -1
  13. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/handle-effect.md +1 -1
  14. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/model_.md +1 -1
  15. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-local-model.md +1 -1
  16. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-model.md +1 -1
  17. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-static-model.md +1 -1
  18. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-store.md +1 -1
  19. package/en/docusaurus-plugin-content-docs/current/components/command-tip.md +56 -0
  20. package/en/docusaurus-plugin-content-docs/current/components/reduck-tip.md +5 -0
  21. package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-css-extract.md +12 -0
  22. package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/router.md +1 -1
  23. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/_category_.json +4 -0
  24. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/_category_.json +4 -0
  25. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/_category_.json +4 -0
  26. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/app.md +152 -0
  27. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/ejs.md +56 -0
  28. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/fs.md +54 -0
  29. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/git.md +49 -0
  30. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/handlebars.md +58 -0
  31. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/json.md +59 -0
  32. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/npm.md +49 -0
  33. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/develop.md +52 -0
  34. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/introduce.md +46 -0
  35. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/run-in-js.md +48 -0
  36. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/structure.md +87 -0
  37. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/_category_.json +4 -0
  38. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/common.md +106 -0
  39. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/module.md +39 -0
  40. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/monorepo.md +28 -0
  41. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/mwa.md +89 -0
  42. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/_category_.json +4 -0
  43. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/abstract.md +26 -0
  44. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/_category_.json +4 -0
  45. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/_category_.json +4 -0
  46. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/addFile.md +52 -0
  47. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/addHelper.md +26 -0
  48. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/addManyFile.md +58 -0
  49. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/addPartial.md +26 -0
  50. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/introduce.md +38 -0
  51. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/rmDir.md +24 -0
  52. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/rmFile.md +24 -0
  53. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/updateJSONFile.md +54 -0
  54. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/updateModernConfig.md +26 -0
  55. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/updateTextRawFile.md +33 -0
  56. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/git/_category_.json +4 -0
  57. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/git/gitAddAndCommit.md +20 -0
  58. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/git/initGitRepo.md +16 -0
  59. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/git/isInGitRepo.md +16 -0
  60. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/hook/_category_.json +4 -0
  61. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/hook/afterForged.md +35 -0
  62. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/hook/onForged.md +36 -0
  63. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/info/_category_.json +4 -0
  64. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/info/isFileExit.md +22 -0
  65. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/info/locale.md +17 -0
  66. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/info/readDir.md +22 -0
  67. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/_category_.json +4 -0
  68. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/addInputAfter.md +57 -0
  69. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/addInputBefore.md +56 -0
  70. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/setInput.md +42 -0
  71. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/setInputValue.md +30 -0
  72. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/type.md +65 -0
  73. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/introduce.md +91 -0
  74. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/_category_.json +4 -0
  75. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/createElement.md +33 -0
  76. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/createSubProject.md +35 -0
  77. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/enableFunc.md +44 -0
  78. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/introduce.md +15 -0
  79. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/npm/_category_.json +4 -0
  80. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/npm/install.md +18 -0
  81. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/develop.md +125 -0
  82. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/use.md +61 -0
  83. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/project.md +118 -0
  84. package/en/docusaurus-plugin-content-docs/current.json +39 -3
  85. package/package.json +3 -3
  86. package/scripts/summary.en.json +1 -1
  87. package/scripts/summary.zh.json +1 -1
  88. package/zh/apis/app/commands/new.md +0 -1
  89. package/zh/apis/app/hooks/server/test.md +2 -2
  90. package/zh/apis/app/runtime/web-server/hook.md +4 -4
  91. package/zh/apis/app/runtime/web-server/middleware.md +4 -4
  92. package/zh/configure/app/output/disable-css-extract.md +12 -0
  93. package/zh/guides/advanced-features/bff/_category_.json +5 -1
  94. package/zh/guides/advanced-features/bff/bff-proxy.md +5 -3
  95. package/zh/guides/advanced-features/bff/frameworks.md +72 -51
  96. package/zh/guides/advanced-features/bff/function.md +16 -30
  97. package/zh/guides/advanced-features/bff/index.md +20 -0
  98. package/zh/guides/advanced-features/bff/type.md +43 -0
  99. package/zh/guides/advanced-features/custom-app.md +63 -1
  100. package/zh/guides/advanced-features/web-server.md +21 -110
  101. package/zh/guides/basic-features/data-fetch.md +61 -0
  102. package/zh/guides/basic-features/env-vars.md +88 -41
  103. package/zh/guides/basic-features/routes.md +267 -0
  104. package/zh/guides/concept/entries.md +112 -0
  105. package/zh/guides/concept/lifecycle.md +10 -0
  106. package/zh/guides/get-started/quick-start.md +199 -1
  107. package/zh/guides/get-started/upgrade.md +75 -1
  108. package/zh/guides/topic-detail/generator/codesmith/api/_category_.json +4 -0
  109. package/zh/guides/topic-detail/generator/codesmith/api/app.md +152 -0
  110. package/zh/guides/topic-detail/generator/codesmith/api/ejs.md +56 -0
  111. package/zh/guides/topic-detail/generator/codesmith/api/fs.md +54 -0
  112. package/zh/guides/topic-detail/generator/codesmith/api/git.md +48 -0
  113. package/zh/guides/topic-detail/generator/codesmith/api/handlebars.md +56 -0
  114. package/zh/guides/topic-detail/generator/codesmith/api/json.md +58 -0
  115. package/zh/guides/topic-detail/generator/codesmith/api/npm.md +45 -0
  116. package/zh/guides/topic-detail/generator/codesmith/develop.md +52 -0
  117. package/zh/guides/topic-detail/generator/codesmith/introduce.md +42 -1
  118. package/zh/guides/topic-detail/generator/codesmith/run-in-js.md +48 -0
  119. package/zh/guides/topic-detail/generator/codesmith/structure.md +86 -0
  120. package/zh/guides/topic-detail/generator/config/mwa.md +4 -8
  121. package/zh/guides/topic-detail/generator/plugin/api/file/addFile.md +1 -1
  122. package/zh/guides/topic-detail/generator/plugin/api/file/addHelper.md +1 -1
  123. package/zh/guides/topic-detail/generator/plugin/api/file/addPartial.md +2 -2
  124. package/zh/guides/topic-detail/generator/plugin/api/file/introduce.md +1 -1
  125. package/zh/guides/topic-detail/generator/plugin/api/file/rmDir.md +1 -1
  126. package/zh/guides/topic-detail/generator/plugin/api/file/rmFile.md +1 -1
  127. package/zh/guides/topic-detail/generator/plugin/api/file/updateJSONFile.md +3 -2
  128. package/zh/guides/topic-detail/generator/plugin/api/file/updateModernConfig.md +2 -2
  129. package/zh/guides/topic-detail/generator/plugin/api/file/updateTextRawFile.md +1 -1
  130. package/zh/guides/topic-detail/generator/plugin/api/hook/afterForged.md +3 -3
  131. package/zh/guides/topic-detail/generator/plugin/api/hook/onForged.md +3 -3
  132. package/zh/guides/topic-detail/generator/plugin/api/input/addInputAfter.md +1 -1
  133. package/zh/guides/topic-detail/generator/plugin/api/input/addInputBefore.md +1 -1
  134. package/zh/guides/topic-detail/generator/plugin/api/input/type.md +1 -1
  135. package/zh/guides/topic-detail/generator/plugin/api/new/createElement.md +1 -1
  136. package/zh/guides/topic-detail/generator/plugin/api/new/createSubProject.md +1 -1
  137. package/zh/guides/topic-detail/generator/plugin/api/new/enableFunc.md +1 -1
  138. package/zh/guides/topic-detail/generator/plugin/api/npm/install.md +1 -1
  139. package/zh/guides/topic-detail/generator/plugin/develop.md +11 -7
  140. package/zh/guides/topic-detail/monorepo/sub-project-interface.md +0 -2
  141. package/zh/tutorials/first-app/c01-getting-started/1.1-prerequisites.md +2 -2
  142. package/zh/tutorials/first-app/c02-generator-and-studio/2.1-generator.md +0 -8
  143. package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +15 -7
  144. package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +1 -1
  145. package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +1 -5
  146. package/zh/tutorials/first-app/c08-client-side-routing/8.2-file-based-routing.md +0 -8
  147. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/env.md +0 -72
  148. package/zh/apis/app/runtime/env.md +0 -70
  149. package/zh/components/deploy.md +0 -60
  150. package/zh/components/dev-ide.md +0 -1
  151. package/zh/components/entry-name.md +0 -15
  152. package/zh/components/env-prepare.md +0 -1
  153. package/zh/components/launch-bff-choices.md +0 -6
  154. package/zh/guides/advanced-features/bff/bff-server.md +0 -142
  155. package/zh/guides/advanced-features/typescript.md +0 -4
  156. package/zh/guides/concept/integrated.md +0 -4
  157. package/zh/guides/concept/solutions.md +0 -4
@@ -0,0 +1,152 @@
1
+ ---
2
+ sidebar_position: 1
3
+ ---
4
+
5
+ # @modern-js/codesmith-api-app
6
+
7
+ 微生成器开发过程中常用 API 的组合封装,包含 fs、git、npm 等其他包的 API 封装,在能满足需求时,推荐使用该 npm 包的 API。
8
+
9
+ ## 使用姿势
10
+
11
+ ```typescript
12
+ import { AppAPI } from '@modern-js/codesmith-api-app';
13
+
14
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
15
+ const appApi = new AppAPI(context, generator);
16
+ await appApi.runInstall();
17
+ }
18
+ ```
19
+
20
+ - 创建 AppAPI 实例,参数和微生成器函数参数一致,为 context 和 generator,具体介绍请看[微生成器项目组成](/docs/guides/topic-detail/generator/codesmith/structure)。
21
+ - 调用其实例上 API 即可。
22
+
23
+ ## API
24
+
25
+ ### checkEnvironment
26
+
27
+ 检查当前生成器运行环境,检查项为:
28
+
29
+ 1. node 及 node 版本,默认大于 12.22.12,可传递参数执行 node 版本。
30
+ 2. 可使用 yarn、pnpm 或者 npm。
31
+
32
+ 参数:
33
+
34
+ - nodeVersion?: `string` 校验的 node 版本。
35
+
36
+ ### runInstall
37
+
38
+ 安装依赖,可传入安装依赖命令,默认根据 config 中的 `packageManager` 值进行。
39
+
40
+ 参数:
41
+
42
+ - command?: `string` 安装依赖命令。
43
+
44
+ ### runGitAndInstall
45
+
46
+ 该函数完成以下动作:
47
+
48
+ 1. 校验当前生成器执行目录是否为一个 git 仓库。
49
+ 2. 如果不是一个 git 仓库,初始化为一个 git 仓库。
50
+ 3. 安装依赖。
51
+ 4. 在非 monorepo 项目(判断条件,config 中 `isMonorepoSubProject` 不存在或者为 false)中提交初始 commit,commit 信息为 feat: init,支持自定义。
52
+
53
+ 参数:
54
+ - commitMessage?: `string` 初始化 commit message 信息。
55
+ - installFunc?: `() => Promise<void>` 安装依赖函数。
56
+
57
+ ### forgeTemplate
58
+
59
+ 渲染生成器模板文件。
60
+
61
+ 参数:
62
+
63
+ - templatePattern: string 模板文件匹配正则,例如: `templates/base-templates/**/*` 。
64
+ - filter?: `(resourceKey: string) => boolean` 过滤函数,参数为 templatePattern 匹配的文件路径,返回 true 表示渲染该文件,返回 false 表示不渲染该文件。
65
+ - rename?: `(resourceKey: string) => string` 重命名函数,参数为 templatePattern 匹配的文件路径,返回新文件名。默认会替换 resourceKey 开头的 templates 目录和结尾的 .handlebars 后缀。
66
+ - parameters?: `Record<string, any>` 渲染参数,当模板中存在 handlebars 或者 ejs 变量时,使用其传递对应变量值。
67
+ - type?: `'handlebars' | 'ejs'` 模板文件类型,默认为 handlebars。
68
+
69
+ 例如:
70
+
71
+ ```typescript
72
+ await appApi.forgeTemplate(
73
+ 'templates/base-templates/**/*',
74
+ undefined,
75
+ resourceKey =>
76
+ resourceKey
77
+ .replace('templates/base-templates/', '')
78
+ .replace('.handlebars', ''),
79
+ );
80
+
81
+ await appApi.forgeTemplate(
82
+ 'templates/base-template/**/*',
83
+ resourceKey => !resourceKey.include('eslintrc.json'),
84
+ resourceKey =>
85
+ resourceKey
86
+ .replace('templates/base-template/', projectPath)
87
+ .replace('language', language as string)
88
+ .replace('.handlebars', ''),
89
+ {
90
+ name: packageName as string,
91
+ language,
92
+ isTs: language === Language.TS,
93
+ packageManager: getPackageManagerText(packageManager as any),
94
+ },
95
+ );
96
+ ```
97
+
98
+ ### showSuccessInfo
99
+
100
+ 展示成功信息。
101
+
102
+ 参数:
103
+
104
+ - successInfo?: `string`,默认为 Success || 成功。
105
+
106
+ ### runSubGenerator
107
+
108
+ 运行子生成器。
109
+
110
+ 参数:
111
+
112
+ - subGenerator: `string` 子生成器名称或者路径。
113
+ - relativePwdPath?: `string` 子生成器运行的相对路径。
114
+ - config?: `Record<string, unknown>` 子生成器运行的默认 config 配置。
115
+
116
+ 例如:
117
+
118
+ ```typescript
119
+ await appApi.runSubGenerator(
120
+ getGeneratorPath('@modern-js/repo-generator', context.config.distTag),
121
+ undefined,
122
+ { ...context.config, hasPlugin: false },
123
+ );
124
+ ```
125
+
126
+ ### getInputBySchema
127
+
128
+ 通过 schema 完成用户交互输入。
129
+
130
+ 参数:
131
+
132
+ - schema: `FormilySchema | Question[]` 问题列表,支持 Formily schema 和 inquirer 类型。
133
+ - type: `'formily' | 'inquirer'` 类型,默认值为 formily。
134
+ - configValue: `Record<string, unknown> = {}` schema 默认值,传入该值的 schema 字段对应的问题将不再和用户交互。
135
+ - validateMap?: `Record<string, (input: unknown, data?: Record<string, unknown>) => { success: boolean; error?: string }>` schema 中特殊字段的验证函数。
136
+ - initValue?: `Record<string, any>` schema 中字段的初始化值。
137
+
138
+ Formily Schema 类型支持方式可参考[自定义输入相关类型定义](/docs/guides/topic-detail/generator/plugin/api/input/type)。
139
+
140
+ ### getInputBySchemaFunc
141
+
142
+ 通过 schema 完成用户交互输入,schema 参数值为函数,用户处理国际化问题,仅支持 Formily schema。
143
+
144
+
145
+ 参数:
146
+
147
+ - schema: `config?: Record<string, any>) => FormilySchema` 获取问题列表函数,config 参数为当前生成器中的 config 配置信息。
148
+ - configValue: `Record<string, unknown> = {}` schema 默认值,传入该值的 schema 字段对应的问题将不再和用户交互。
149
+ - validateMap?: `Record<string, (input: unknown, data?: Record<string, unknown>) => { success: boolean; error?: string }>` schema 中特殊字段的验证函数。
150
+ - initValue?: `Record<string, any>` schema 中字段的初始化值。
151
+
152
+ Formily Schema 类型支持方式可参考[自定义输入相关类型定义](/docs/guides/topic-detail/generator/plugin/api/input/type)。
@@ -0,0 +1,56 @@
1
+ ---
2
+ sidebar_position: 6
3
+ ---
4
+
5
+ # @modern-js/codesmith-api-ejs
6
+
7
+ 微生成器中使用 [ejs](https://ejs.co/) 进行文件操作的 API 封装,提供渲染单个模板文件和文件夹的方法。
8
+
9
+ ## 使用姿势
10
+
11
+ ```typescript
12
+ import { EjsAPI } from '@modern-js/codesmith-api-ejs';
13
+
14
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
15
+ const ejsAPI = new EjsAPI(generator);
16
+ await ejsAPI.renderTemplate(
17
+ material.get('templates/a.js'),
18
+ target: 'b.js',
19
+ { data: "data" }
20
+ );
21
+ }
22
+ ```
23
+
24
+ - 创建 EjsAPI 实例,参数为微生成器函数参数的 generator,具体介绍请看微生成器项目组成 。
25
+ - 调用其实例上 API 即可。
26
+
27
+ ## API
28
+
29
+ ### renderTemplate
30
+
31
+ 渲染单个模板文件。
32
+
33
+ 参数:
34
+
35
+ - templateResource: `FsResource` 模板文件资源,通过 `context.materials.get(<filename>)` 使用。
36
+ - target: `string` 目标文件路径名称。
37
+ - parameters?: `Record<string, string>` 渲染参数。
38
+
39
+ ### renderTemplateDir
40
+
41
+ 渲染模板文件夹。
42
+
43
+ 参数:
44
+
45
+ - material: `FsMaterial` 当前微生成器执行的 material 上下文。
46
+ - findGlob: `string` 模板文件匹配正则。
47
+ - target: `(globMatch: string) => string` 目标文件路径生成函数,参数为 `resourceKey。`
48
+ - options?: `RenderTemplateDirOptions` glob 查找文件配置,具体函数可查看这里 [glob](https://www.npmjs.com/package/glob)。
49
+
50
+ ```typescript
51
+ type RenderTemplateDirOptions = {
52
+ nodir?: boolean;
53
+ dot?: boolean;
54
+ ignore?: string | readonly string[];
55
+ };
56
+ ```
@@ -0,0 +1,54 @@
1
+ ---
2
+ sidebar_position: 3
3
+ ---
4
+
5
+ # @modern-js/codesmith-api-fs
6
+
7
+ 微生成器中文件操作的 API 封装,提供渲染单个模板文件和文件夹的方法。FsAPI 通常用于处理二进制文件或者不能使用 handlebars、ejs 处理的模板文件。
8
+
9
+ ## 使用姿势
10
+
11
+ ```typescript
12
+ import { FsAPI } from '@modern-js/codesmith-api-fs';
13
+
14
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
15
+ const handlebarApi = new HandlebarsAPI(generator);
16
+ await handlebarsAPI.renderFile(
17
+ material.get('templates/a.js'),
18
+ target: 'b.js'
19
+ );
20
+ }
21
+ ```
22
+
23
+ - 创建 FsAPI 实例,参数为微生成器函数参数的 generator,具体介绍请看微生成器项目组成 。
24
+ - 调用其实例上 API 即可。
25
+
26
+ ## API
27
+
28
+ ### renderFile
29
+
30
+ 渲染单个模板文件。
31
+
32
+ 参数:
33
+
34
+ - resource: `FsResource` 模板文件资源,通过 `context.materials.get(<filename>)` 使用。
35
+ - target: `string` 目标文件路径名称。
36
+
37
+ ### renderDir
38
+
39
+ 渲染模板文件夹。
40
+
41
+ 参数:
42
+
43
+ - material: `FsMaterial` 当前微生成器执行的 material 上下文。
44
+ - findGlob: `string` 模板文件匹配正则。
45
+ - target: `(globMatch: string) => string` 目标文件路径生成函数,参数为 `resourceKey。`
46
+ - options?: `RenderTemplateDirOptions` glob 查找文件配置,具体函数可查看这里 [glob](https://www.npmjs.com/package/glob)。
47
+
48
+ ```typescript
49
+ type RenderTemplateDirOptions = {
50
+ nodir?: boolean;
51
+ dot?: boolean;
52
+ ignore?: string | readonly string[];
53
+ };
54
+ ```
@@ -0,0 +1,48 @@
1
+ ---
2
+ sidebar_position: 4
3
+ ---
4
+
5
+ # @modern-js/codesmith-api-git
6
+
7
+ 微生成器中使用 git 相关操作的 API 封装,提供判断 git 仓库,初始化及提交代码等方法。
8
+
9
+ ## 使用姿势
10
+
11
+ ```typescript
12
+ import { GitAPI } from '@modern-js/codesmith-api-git';
13
+
14
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
15
+ const gitApi = new GitAPI(generatorCore, generatorContext);
16
+ await gitApi.initGitRepo();
17
+ }
18
+ ```
19
+
20
+ - 创建 GitAPI 实例,参数和微生成器函数参数一致,为 context 和 generator,具体介绍请看微生成器项目组成
21
+ - 调用其实例上 API 即可。
22
+
23
+ ## API
24
+
25
+ ### isInGitRepo
26
+
27
+ 当前目录是否为 git 仓库。
28
+
29
+ 参数:
30
+
31
+ - cwd?: `string` git 命令的执行目录,默认为微生成器 `outputPath`。
32
+
33
+ ### initGitRepo
34
+
35
+ 初始化为 git 仓库。init 的默认分支名可通过生成器 config 中的 defaultBranch 进行配置。
36
+
37
+ 参数:
38
+
39
+ - cwd?: `string` git 命令的执行目录,默认为微生成器 `outputPath`。
40
+ - force?: `boolean` 当前目录已经为 git 仓库时使用该参数会强制执行 git init。
41
+
42
+ ### addAndCommit
43
+
44
+ 执行 `git add .` 和 `git commit` 提交当前变更。
45
+
46
+ 参数:
47
+ - commitMessage: `string` commit 信息。
48
+ - cwd?: `string` git 命令的执行目录,默认为微生成器 `outputPath`。
@@ -0,0 +1,56 @@
1
+ ---
2
+ sidebar_position: 5
3
+ ---
4
+
5
+ # @modern-js/codesmith-api-handlebars
6
+
7
+ 微生成器中使用 [handlebars](https://handlebarsjs.com/) 进行文件操作的 API 封装,提供渲染单个模板文件和文件夹的方法。
8
+
9
+ ## 使用姿势
10
+
11
+ ```typescript
12
+ import { HandlebarsAPI } from '@modern-js/codesmith-api-handlebars';
13
+
14
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
15
+ const handlebarApi = new HandlebarsAPI(generator);
16
+ await handlebarsAPI.renderTemplate(
17
+ material.get('templates/a.js'),
18
+ target: 'b.js',
19
+ { data: "data" }
20
+ );
21
+ }
22
+ ```
23
+
24
+ - 创建 HandlebarsAPI 实例,参数为微生成器函数参数的 generator,具体介绍请看微生成器项目组成 。
25
+ - 调用其实例上 API 即可。
26
+
27
+ ## API
28
+
29
+ ### renderTemplate
30
+
31
+ 渲染单个模板文件。
32
+
33
+ 参数:
34
+
35
+ - templateResource: `FsResource` 模板文件资源,通过 `context.materials.get(<filename>)` 使用。
36
+ - target: `string` 目标文件路径名称。
37
+ - parameters?: `Record<string, string>` 渲染参数。
38
+
39
+ ### renderTemplateDir
40
+
41
+ 渲染模板文件夹。
42
+
43
+ 参数:
44
+
45
+ - material: `FsMaterial` 当前微生成器执行的 material 上下文。
46
+ - findGlob: `string` 模板文件匹配正则。
47
+ - target: `(globMatch: string) => string` 目标文件路径生成函数,参数为 `resourceKey。`
48
+ - options?: `RenderTemplateDirOptions` glob 查找文件配置,具体函数可查看这里 [glob](https://www.npmjs.com/package/glob)。
49
+
50
+ ```typescript
51
+ type RenderTemplateDirOptions = {
52
+ nodir?: boolean;
53
+ dot?: boolean;
54
+ ignore?: string | readonly string[];
55
+ };
56
+ ```
@@ -0,0 +1,58 @@
1
+ ---
2
+ sidebar_position: 2
3
+ ---
4
+
5
+ # @modern-js/codesmith-api-json
6
+
7
+ 微生成器中 JSON API 封装,提供常见的 JSON 文件操作方法。
8
+
9
+ ## 使用姿势
10
+
11
+ ```typescript
12
+ import { JsonAPI } from '@modern-js/codesmith-api-json';
13
+
14
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
15
+ const jsonAPI = new JsonAPI(generator);
16
+ await jsonAPI.update(
17
+ context.materials.default.get('package.json'),
18
+ {
19
+ query: {},
20
+ update: {
21
+ $set: {
22
+ 'dependencies.@modern-js/plugin-bff': `^2.0.0`,
23
+ },
24
+ },
25
+ },
26
+ );
27
+ }
28
+ ```
29
+
30
+ - 创建 JsonAPI 实例,参数为微生成器函数参数的 generator,具体介绍请看[微生成器项目组成](/docs/guides/topic-detail/generator/codesmith/structure)。
31
+ - 调用其实例上 API 即可。
32
+
33
+ ## API
34
+
35
+ ### get
36
+
37
+ 获取 JSON 文件内容。
38
+
39
+ 参数:
40
+ - resource: `FsResource`,文件资源,通过 `context.materials.default.get(<filename>)` 获取。
41
+
42
+ ### extend
43
+
44
+ 合并对象至 JSON 文件。
45
+
46
+ 参数:
47
+
48
+ - resource: `FsResource` 通过 `context.materials.default.get(<filename>)` 获取。
49
+ - obj: `Record<string, any>` 需合并对象。
50
+
51
+ ### update
52
+
53
+ 更新对象字段至 JSON 文件。
54
+
55
+ 参数:
56
+
57
+ - resource: `FsResource` 通过 `context.materials.default.get(<filename>)` 获取。
58
+ - operation: `{ query: Record<string, any>; update: Record<string, any> }` 更新操作,详细使用姿势查看 [declaration-update](https://www.npmjs.com/package/declaration-update)。
@@ -0,0 +1,45 @@
1
+ ---
2
+ sidebar_position: 7
3
+ ---
4
+
5
+ # @modern-js/codesmith-api-npm
6
+
7
+ 微生成器中 NPM API 封装,提供常见的 NPM 不同包管理工具安装依赖的方法。
8
+
9
+ ## 使用姿势
10
+
11
+ ```typescript
12
+ import { NpmAPI } from '@modern-js/codesmith-api-npm';
13
+
14
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
15
+ const npmApi = new NpmAPI(generator);
16
+ await npmApi.pnpmInstall();
17
+ }
18
+ ```
19
+
20
+ - 创建 NpmAPI 实例,参数为微生成器函数参数的 generator,具体介绍请看微生成器项目组成 。
21
+ - 调用其实例上 API 即可。
22
+
23
+ ## API
24
+
25
+ ### npmInstall
26
+
27
+ 使用 npm 安装依赖。
28
+
29
+ 参数:
30
+
31
+ - cwd?: `string` install 命令的执行目录,默认为微生成器 `outputPath`。
32
+
33
+ ### yarnInstall
34
+
35
+ 使用 yarn 安装依赖。
36
+
37
+ 参数:
38
+ - cwd?: `string` install 命令的执行目录,默认为微生成器 `outputPath`。
39
+
40
+ ### pnpmInstall
41
+
42
+ 使用 pnpm 安装依赖。
43
+
44
+ 参数:
45
+ - cwd?: `string` install 命令的执行目录,默认为微生成器 `outputPath`。
@@ -0,0 +1,52 @@
1
+ ---
2
+ sidebar_position: 3
3
+ ---
4
+
5
+ # 开发微生成器项目
6
+
7
+ ## 创建项目
8
+
9
+ Modern.js 提供了创建微生成器项目的微生成器,可以直接通过以下命令创建微生成器项目:
10
+
11
+ ```bash
12
+ $ mkdir <projectDir>
13
+ $ npx @modern-js/codesmith-cli @modern-js/generator-generator
14
+
15
+ ? 请填写项目名称 <generatorName>
16
+ ? 请选择包管理工具 pnpm
17
+ ? 请选择开发语言 TS
18
+ ```
19
+
20
+ ## 添加模板文件
21
+
22
+ 在项目目录的 templates 文件夹,用于存放生成器需要的模板文件,如果没有模板文件,该步骤可跳过。
23
+
24
+ ## 开发生成器核心逻辑
25
+
26
+ 默认生成的的项目中,已经实现直接拷贝 templates 目录文件至目标项目,可直接在 `handleTemplateFile` 函数中完成生成器的核心逻辑开发。
27
+
28
+ ## 测试微生成器
29
+
30
+ 微生成器开发完成后,需要执行 `pnpm run build` 对代码进行编译,在开发模式下可以使用 `pnpm run build --watch`
31
+
32
+ codesmith 提供了 cli 工具用于运行和测试微生成器,在目标目录执行使用如下命令执行即可:
33
+
34
+ ```bash
35
+ npx @modern-js/codesmith-cli <generatorPath>
36
+ ```
37
+
38
+ 其中 generatorPath 为上述生成器项目的绝对路径。
39
+
40
+ ## 运行微生成器
41
+
42
+ 生成器发布 npm 包之后,使用如下命令执行:
43
+
44
+ ```bash
45
+ npx @modern-js/codesmith-cli <generatorName>
46
+ ```
47
+
48
+ 其中 generatorName 为生成器包名。
49
+
50
+ :::info
51
+ `@modern-js/create` 也支持运行微生成器,可以使用 `npx @modern-js/create --generator <generatorName>` 运行自定义的微生成器。
52
+ :::
@@ -2,4 +2,45 @@
2
2
  sidebar_position: 1
3
3
  ---
4
4
 
5
- # 介绍
5
+ # 什么是微生成器
6
+
7
+ Modern.js 提供了 `@modern-js/create` 工具和 `new` 命令分别用于初始化创建项目和提供开启一些插件功能的能力,这些工具都是基于微生成器也就是 codesmith 实现的。
8
+
9
+ codesmith 致力于提供一套微生成器的运行机制,并维护微生成器执行过程中的上下文信息,从而实现各种基于文件、命令等操作,完成复杂的操作。
10
+
11
+ codesmith 还将常用的方法进行了封装,可以直接使用其提供的 API 函数,可以更方便的实现自身的微生成器。
12
+
13
+ ## 核心概念
14
+
15
+ ### GeneratorCore
16
+
17
+ GeneratorCore 是 codesmith 运行生成器的核心,维护这生成器执行过程中的上下文及运行方法。
18
+
19
+ ```typescript
20
+ class GeneratorCore {
21
+ logger: ILogger; // 日志
22
+ materialsManager: MaterialsManager; // 资源
23
+ outputPath: string; // 输出路径
24
+ output: { // 输出方法
25
+ fs: (file: string | number, data: any, options?: fs.WriteFileOptions | string) => Promise<void>;
26
+ };
27
+ _context: GeneratorContext; // 上下文信息
28
+ addMaterial(key: string, material: FsMaterial): void; // 添加资源
29
+ runGenerator(generator: string, config?: Record<string, unknown>): Promise<void>; // 运行生成器
30
+ runSubGenerator(subGenerator: string, relativePwdPath?: string, config?: Record<string, any>): Promise<void>; // 运行子生成器
31
+ }
32
+ ```
33
+
34
+ ### Materials
35
+
36
+ 微生成器资源信息,目前只包含文件资源(fsMaterials),通过该字段可以获取到生成器执行过程中的 template 资源,并通过 API 对其进行操作。
37
+
38
+ ```typescript
39
+ class MaterialsManager {
40
+ materialMap: { // 资源映射关系
41
+ [materialUri: string]: FsMaterial;
42
+ };
43
+ loadLocalGenerator(generator: string): Promise<FsMaterial>; // 加载本地生成器资源(绝对路径)
44
+ loadRemoteGenerator(generator: string): Promise<FsMaterial>; // 加载远程生成器资源(npm 包)
45
+ }
46
+ ```
@@ -0,0 +1,48 @@
1
+ ---
2
+ sidebar_position: 4
3
+ ---
4
+
5
+
6
+ # 使用 JS 代码执行微生成器
7
+
8
+ 除了使用 cli 的方式执行微生成器,我们免不了需要在代码中执行微生成器,下面就介绍在 js 代码中如何执行微生成器。
9
+
10
+ ## 安装 codesmith 依赖
11
+
12
+ ```bash
13
+ pnpm add @modern-js/codesmith
14
+ ```
15
+
16
+ ## 创建 codesmith 实例
17
+
18
+ ```typescript
19
+ import { CodeSmith, Logger } from '@modern-js/codesmith';
20
+
21
+ const smith = new CodeSmith({
22
+ debug: false, // 是否开启 debug 模式,为 true 时会展示生成器中的 debug 信息
23
+ });
24
+ ```
25
+
26
+ ## 调用 forge 方法执行生成器
27
+
28
+ ```typescript
29
+ type RunnerTask = Array<{
30
+ name: string;
31
+ config: Record<string, any>;
32
+ }>;
33
+
34
+ const task: RunnerTask = [
35
+ {
36
+ name: "generator", // 生成器 npm 包名称
37
+ config: {}, // 生成器执行的默认配置
38
+ },
39
+ ];
40
+
41
+ await smith.forge({
42
+ tasks: task.map(runner => ({
43
+ generator: runner.name,
44
+ config: runner.config,
45
+ })),
46
+ pwd: '.', // 生成器执行路径
47
+ });
48
+ ```