@ohos-ports/tarojs-cli 0.0.0-stage → 4.3.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 (223) hide show
  1. package/LICENSE +174 -0
  2. package/README.md +0 -3
  3. package/bin/taro +7 -0
  4. package/dist/cli.d.ts +6 -0
  5. package/dist/cli.js +252 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/commands/customCommand.d.ts +5 -0
  8. package/dist/commands/customCommand.js +23 -0
  9. package/dist/commands/customCommand.js.map +1 -0
  10. package/dist/config/index.d.ts +8 -0
  11. package/dist/config/index.js +10 -0
  12. package/dist/config/index.js.map +1 -0
  13. package/dist/config/packagesManagement.d.ts +19 -0
  14. package/dist/config/packagesManagement.js +22 -0
  15. package/dist/config/packagesManagement.js.map +1 -0
  16. package/dist/create/constants.d.ts +1 -0
  17. package/dist/create/constants.js +5 -0
  18. package/dist/create/constants.js.map +1 -0
  19. package/dist/create/creator.d.ts +9 -0
  20. package/dist/create/creator.js +31 -0
  21. package/dist/create/creator.js.map +1 -0
  22. package/dist/create/fetchTemplate.d.ts +8 -0
  23. package/dist/create/fetchTemplate.js +137 -0
  24. package/dist/create/fetchTemplate.js.map +1 -0
  25. package/dist/create/page.d.ts +65 -0
  26. package/dist/create/page.js +252 -0
  27. package/dist/create/page.js.map +1 -0
  28. package/dist/create/plugin.d.ts +16 -0
  29. package/dist/create/plugin.js +48 -0
  30. package/dist/create/plugin.js.map +1 -0
  31. package/dist/create/project.d.ts +80 -0
  32. package/dist/create/project.js +519 -0
  33. package/dist/create/project.js.map +1 -0
  34. package/dist/doctor/index.d.ts +4 -0
  35. package/dist/doctor/index.js +32 -0
  36. package/dist/doctor/index.js.map +1 -0
  37. package/dist/index.d.ts +16 -0
  38. package/dist/index.js +21 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/presets/commands/build.d.ts +3 -0
  41. package/dist/presets/commands/build.js +239 -0
  42. package/dist/presets/commands/build.js.map +1 -0
  43. package/dist/presets/commands/config.d.ts +3 -0
  44. package/dist/presets/commands/config.js +88 -0
  45. package/dist/presets/commands/config.js.map +1 -0
  46. package/dist/presets/commands/create.d.ts +3 -0
  47. package/dist/presets/commands/create.js +91 -0
  48. package/dist/presets/commands/create.js.map +1 -0
  49. package/dist/presets/commands/global-config.d.ts +3 -0
  50. package/dist/presets/commands/global-config.js +139 -0
  51. package/dist/presets/commands/global-config.js.map +1 -0
  52. package/dist/presets/commands/help.d.ts +3 -0
  53. package/dist/presets/commands/help.js +29 -0
  54. package/dist/presets/commands/help.js.map +1 -0
  55. package/dist/presets/commands/info.d.ts +3 -0
  56. package/dist/presets/commands/info.js +79 -0
  57. package/dist/presets/commands/info.js.map +1 -0
  58. package/dist/presets/commands/init.d.ts +3 -0
  59. package/dist/presets/commands/init.js +59 -0
  60. package/dist/presets/commands/init.js.map +1 -0
  61. package/dist/presets/commands/inspect.d.ts +3 -0
  62. package/dist/presets/commands/inspect.js +118 -0
  63. package/dist/presets/commands/inspect.js.map +1 -0
  64. package/dist/presets/commands/update.d.ts +3 -0
  65. package/dist/presets/commands/update.js +171 -0
  66. package/dist/presets/commands/update.js.map +1 -0
  67. package/dist/presets/constant/hooks.d.ts +13 -0
  68. package/dist/presets/constant/hooks.js +17 -0
  69. package/dist/presets/constant/hooks.js.map +1 -0
  70. package/dist/presets/constant/index.d.ts +1 -0
  71. package/dist/presets/constant/index.js +18 -0
  72. package/dist/presets/constant/index.js.map +1 -0
  73. package/dist/presets/files/generateFrameworkInfo.d.ts +3 -0
  74. package/dist/presets/files/generateFrameworkInfo.js +30 -0
  75. package/dist/presets/files/generateFrameworkInfo.js.map +1 -0
  76. package/dist/presets/files/generateProjectConfig.d.ts +3 -0
  77. package/dist/presets/files/generateProjectConfig.js +40 -0
  78. package/dist/presets/files/generateProjectConfig.js.map +1 -0
  79. package/dist/presets/files/writeFileToDist.d.ts +3 -0
  80. package/dist/presets/files/writeFileToDist.js +17 -0
  81. package/dist/presets/files/writeFileToDist.js.map +1 -0
  82. package/dist/presets/hooks/build.d.ts +3 -0
  83. package/dist/presets/hooks/build.js +22 -0
  84. package/dist/presets/hooks/build.js.map +1 -0
  85. package/dist/presets/hooks/create.d.ts +3 -0
  86. package/dist/presets/hooks/create.js +11 -0
  87. package/dist/presets/hooks/create.js.map +1 -0
  88. package/dist/presets/index.d.ts +4 -0
  89. package/dist/presets/index.js +17 -0
  90. package/dist/presets/index.js.map +1 -0
  91. package/dist/presets/platforms/plugin.d.ts +3 -0
  92. package/dist/presets/platforms/plugin.js +76 -0
  93. package/dist/presets/platforms/plugin.js.map +1 -0
  94. package/dist/presets/platforms/rn.d.ts +3 -0
  95. package/dist/presets/platforms/rn.js +101 -0
  96. package/dist/presets/platforms/rn.js.map +1 -0
  97. package/dist/util/appConfig.d.ts +11 -0
  98. package/dist/util/appConfig.js +25 -0
  99. package/dist/util/appConfig.js.map +1 -0
  100. package/dist/util/createPage.d.ts +9 -0
  101. package/dist/util/createPage.js +117 -0
  102. package/dist/util/createPage.js.map +1 -0
  103. package/dist/util/defineConfig.d.ts +17 -0
  104. package/dist/util/defineConfig.js +12 -0
  105. package/dist/util/defineConfig.js.map +1 -0
  106. package/dist/util/index.d.ts +23 -0
  107. package/dist/util/index.js +125 -0
  108. package/dist/util/index.js.map +1 -0
  109. package/dist/util/types.d.ts +58 -0
  110. package/dist/util/types.js +3 -0
  111. package/dist/util/types.js.map +1 -0
  112. package/global.d.ts +4 -0
  113. package/index.js +3 -0
  114. package/package.json +76 -4
  115. package/postinstall.js +14 -0
  116. package/src/__tests__/__mocks__/presets.ts +15 -0
  117. package/src/__tests__/build-config.spec.ts +99 -0
  118. package/src/__tests__/cli.spec.ts +233 -0
  119. package/src/__tests__/config.spec.ts +232 -0
  120. package/src/__tests__/doctor-config.spec.ts +805 -0
  121. package/src/__tests__/doctor-eslint.spec.ts +31 -0
  122. package/src/__tests__/doctor-recommand.spec.ts +158 -0
  123. package/src/__tests__/doctor.spec.ts +77 -0
  124. package/src/__tests__/dotenv-parse.spec.ts +105 -0
  125. package/src/__tests__/env/.env +2 -0
  126. package/src/__tests__/fixtures/default/.env +3 -0
  127. package/src/__tests__/fixtures/default/.env.development +1 -0
  128. package/src/__tests__/fixtures/default/.env.local +1 -0
  129. package/src/__tests__/fixtures/default/.env.pre +3 -0
  130. package/src/__tests__/fixtures/default/.env.production +1 -0
  131. package/src/__tests__/fixtures/default/.env.uat +5 -0
  132. package/src/__tests__/fixtures/default/.env.uat.local +1 -0
  133. package/src/__tests__/fixtures/default/babel.config.js +10 -0
  134. package/src/__tests__/fixtures/default/config/dev.js +9 -0
  135. package/src/__tests__/fixtures/default/config/index.js +84 -0
  136. package/src/__tests__/fixtures/default/config/prod.js +18 -0
  137. package/src/__tests__/fixtures/default/package.json +38 -0
  138. package/src/__tests__/fixtures/default/src/app.config.js +11 -0
  139. package/src/__tests__/fixtures/default/src/app.js +16 -0
  140. package/src/__tests__/fixtures/default/src/app.scss +0 -0
  141. package/src/__tests__/fixtures/default/src/index.html +19 -0
  142. package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
  143. package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
  144. package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
  145. package/src/__tests__/info.spec.ts +74 -0
  146. package/src/__tests__/inspect.spec.ts +163 -0
  147. package/src/__tests__/project.spec.ts +304 -0
  148. package/src/__tests__/update.spec.ts +324 -0
  149. package/src/__tests__/utils/index.ts +54 -0
  150. package/src/cli.ts +250 -0
  151. package/src/commands/customCommand.ts +26 -0
  152. package/src/config/index.ts +7 -0
  153. package/src/config/manifest.default.json +35 -0
  154. package/src/config/packagesManagement.ts +20 -0
  155. package/src/config/rn-stylelint.json +5 -0
  156. package/src/config/tsconfig.json +24 -0
  157. package/src/create/constants.ts +1 -0
  158. package/src/create/creator.ts +37 -0
  159. package/src/create/fetchTemplate.ts +152 -0
  160. package/src/create/page.ts +299 -0
  161. package/src/create/plugin.ts +55 -0
  162. package/src/create/project.ts +591 -0
  163. package/src/doctor/index.ts +27 -0
  164. package/src/index.ts +24 -0
  165. package/src/presets/commands/build.ts +227 -0
  166. package/src/presets/commands/config.ts +86 -0
  167. package/src/presets/commands/create.ts +105 -0
  168. package/src/presets/commands/global-config.ts +140 -0
  169. package/src/presets/commands/help.ts +19 -0
  170. package/src/presets/commands/info.ts +77 -0
  171. package/src/presets/commands/init.ts +66 -0
  172. package/src/presets/commands/inspect.ts +138 -0
  173. package/src/presets/commands/update.ts +179 -0
  174. package/src/presets/constant/hooks.ts +14 -0
  175. package/src/presets/constant/index.ts +1 -0
  176. package/src/presets/files/generateFrameworkInfo.ts +30 -0
  177. package/src/presets/files/generateProjectConfig.ts +41 -0
  178. package/src/presets/files/writeFileToDist.ts +17 -0
  179. package/src/presets/hooks/build.ts +22 -0
  180. package/src/presets/hooks/create.ts +11 -0
  181. package/src/presets/index.ts +15 -0
  182. package/src/presets/platforms/plugin.ts +81 -0
  183. package/src/presets/platforms/rn.ts +113 -0
  184. package/src/util/appConfig.ts +29 -0
  185. package/src/util/createPage.ts +132 -0
  186. package/src/util/defineConfig.ts +22 -0
  187. package/src/util/index.ts +127 -0
  188. package/src/util/types.ts +81 -0
  189. package/templates/default/.husky/commit-msg +4 -0
  190. package/templates/default/_editorconfig +12 -0
  191. package/templates/default/_env.development +2 -0
  192. package/templates/default/_env.production +1 -0
  193. package/templates/default/_env.test +1 -0
  194. package/templates/default/_eslintrc +11 -0
  195. package/templates/default/_gitignore +8 -0
  196. package/templates/default/babel.config.js +14 -0
  197. package/templates/default/commitlint.config.mjs +1 -0
  198. package/templates/default/config/dev.js +10 -0
  199. package/templates/default/config/index.js +106 -0
  200. package/templates/default/config/prod.js +46 -0
  201. package/templates/default/package.json.tmpl +158 -0
  202. package/templates/default/project.config.json +15 -0
  203. package/templates/default/src/app.config.js +11 -0
  204. package/templates/default/src/app.css +0 -0
  205. package/templates/default/src/app.js +44 -0
  206. package/templates/default/src/index.html +17 -0
  207. package/templates/default/src/pages/index/index.config.js +3 -0
  208. package/templates/default/src/pages/index/index.css +0 -0
  209. package/templates/default/src/pages/index/index.jsx +15 -0
  210. package/templates/default/src/pages/index/index.vue +23 -0
  211. package/templates/default/stylelint.config.mjs +4 -0
  212. package/templates/default/template_creator.js +77 -0
  213. package/templates/default/tsconfig.json +32 -0
  214. package/templates/default/types/global.d.ts +35 -0
  215. package/templates/default/types/solid.d.ts +12 -0
  216. package/templates/default/types/vue.d.ts +10 -0
  217. package/templates/global-config/index.json +4 -0
  218. package/templates/global-config/package.json +5 -0
  219. package/templates/plugin-compile/README.md +32 -0
  220. package/templates/plugin-compile/index.js +3 -0
  221. package/templates/plugin-compile/package.json.tmpl +43 -0
  222. package/templates/plugin-compile/src/index.ts +153 -0
  223. package/templates/plugin-compile/tsconfig.json +29 -0
@@ -0,0 +1,227 @@
1
+ import {
2
+ MessageKind,
3
+ validateConfig
4
+ } from '@tarojs/plugin-doctor'
5
+
6
+ import { extractCompileEntry } from '../../util/appConfig'
7
+ import * as hooks from '../constant'
8
+
9
+ import type { IPluginContext } from '@tarojs/service'
10
+
11
+ export default (ctx: IPluginContext) => {
12
+ ctx.registerCommand({
13
+ name: 'build',
14
+ optionsMap: {
15
+ '--type [typeName]': 'Build type, weapp/swan/alipay/tt/qq/jd/h5/rn',
16
+ '--watch': 'Watch mode',
17
+ '--env [env]': 'Value for process.env.NODE_ENV',
18
+ '--pages': 'Specify the pages to be compiled, separate multiple by comma',
19
+ '--components': 'Specify the components to be compiled, separate multiple by comma',
20
+ '--mode [mode]': 'Value of dotenv extname',
21
+ '-p, --port [port]': 'Specified port',
22
+ '--no-build': 'Do not build project',
23
+ '--platform': '[rn] Specific React-Native build target: android / ios, android is default value',
24
+ '--reset-cache': '[rn] Clear transform cache',
25
+ '--public-path': '[rn] Assets public path',
26
+ '--bundle-output': '[rn] File name where to store the resulting bundle',
27
+ '--sourcemap-output': '[rn] File name where to store the sourcemap file for resulting bundle',
28
+ '--sourcemap-use-absolute-path': '[rn] Report SourceMapURL using its full path',
29
+ '--sourcemap-sources-root': '[rn] Path to make sourcemaps sources entries relative to',
30
+ '--assets-dest': '[rn] Directory name where to store assets referenced in the bundle',
31
+ '--qr': '[rn] Print qrcode of React-Native bundle server',
32
+ '--blended': 'Blended Taro project in an original MiniApp project',
33
+ '--new-blended': 'Blended Taro project in an original MiniApp project while supporting building components independently',
34
+ '--plugin [typeName]': 'Build Taro plugin project, weapp',
35
+ '--env-prefix [envPrefix]': "Provide the dotEnv varables's prefix",
36
+ '--no-inject-global-style': '[H5] Do not inject global style',
37
+ '--no-check': 'Do not check config is valid or not',
38
+ },
39
+ synopsisList: [
40
+ 'taro build --type weapp',
41
+ 'taro build --type weapp --watch',
42
+ 'taro build --type weapp --watch --pages pages/index/index',
43
+ 'taro build --type weapp --env production',
44
+ 'taro build --type weapp --blended',
45
+ 'taro build --type weapp --no-build',
46
+ 'taro build native-components --type weapp',
47
+ 'taro build --type weapp --new-blended',
48
+ 'taro build --plugin weapp --watch',
49
+ 'taro build --plugin weapp',
50
+ 'taro build --type weapp --mode prepare --env-prefix TARO_APP_',
51
+ ],
52
+ async fn(opts) {
53
+ const { options, config, _ } = opts
54
+ const { platform, isWatch, blended, newBlended, withoutBuild, noInjectGlobalStyle, noCheck } = options
55
+ const { fs, chalk, PROJECT_CONFIG } = ctx.helper
56
+ const { outputPath, configPath } = ctx.paths
57
+ const { args } = options
58
+
59
+ if (!configPath || !fs.existsSync(configPath)) {
60
+ console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`))
61
+ process.exit(1)
62
+ }
63
+
64
+ if (typeof platform !== 'string') {
65
+ console.log(chalk.red('请传入正确的编译类型!'))
66
+ process.exit(0)
67
+ }
68
+
69
+ // 校验 Taro 项目配置
70
+ if (!noCheck) {
71
+ const checkResult = await checkConfig({
72
+ projectConfig: ctx.initialConfig,
73
+ helper: ctx.helper
74
+ })
75
+ if (!checkResult.isValid) {
76
+ const ERROR = chalk.red('[✗] ')
77
+ const WARNING = chalk.yellow('[!] ')
78
+ const SUCCESS = chalk.green('[✓] ')
79
+
80
+ const lineChalk = chalk.hex('#fff')
81
+ const errorChalk = chalk.hex('#f00')
82
+ console.log(errorChalk(`Taro 配置有误,请检查! (${configPath})`))
83
+ checkResult.messages.forEach((message) => {
84
+ switch (message.kind) {
85
+ case MessageKind.Error:
86
+ console.log(' ' + ERROR + lineChalk(message.content))
87
+ break
88
+ case MessageKind.Success:
89
+ console.log(' ' + SUCCESS + lineChalk(message.content))
90
+ break
91
+ case MessageKind.Warning:
92
+ console.log(' ' + WARNING + lineChalk(message.content))
93
+ break
94
+ case MessageKind.Manual:
95
+ console.log(' ' + lineChalk(message.content))
96
+ break
97
+ default:
98
+ break
99
+ }
100
+ })
101
+ console.log('')
102
+ process.exit(0)
103
+ }
104
+ }
105
+
106
+ const isProduction = process.env.NODE_ENV === 'production' || !isWatch
107
+
108
+ // dist folder
109
+ fs.ensureDirSync(outputPath)
110
+
111
+ // is build native components mode?
112
+ const isBuildNativeComp = _[1] === 'native-components'
113
+
114
+ await ctx.applyPlugins(hooks.ON_BUILD_START)
115
+ await ctx.applyPlugins({
116
+ name: platform,
117
+ opts: {
118
+ config: {
119
+ ...config,
120
+ isWatch,
121
+ mode: isProduction ? 'production' : 'development',
122
+ blended,
123
+ isBuildNativeComp,
124
+ withoutBuild,
125
+ newBlended,
126
+ noInjectGlobalStyle,
127
+ async modifyAppConfig (appConfig) {
128
+ extractCompileEntry(appConfig, args, ctx)
129
+
130
+ await ctx.applyPlugins({
131
+ name: hooks.MODIFY_APP_CONFIG,
132
+ opts: {
133
+ appConfig
134
+ }
135
+ })
136
+ },
137
+ async modifyWebpackChain (chain, webpack, data) {
138
+ await ctx.applyPlugins({
139
+ name: hooks.MODIFY_WEBPACK_CHAIN,
140
+ initialVal: chain,
141
+ opts: {
142
+ chain,
143
+ webpack,
144
+ data,
145
+ },
146
+ })
147
+ },
148
+ async modifyViteConfig(viteConfig, data, viteCompilerContext) {
149
+ await ctx.applyPlugins({
150
+ name: hooks.MODIFY_VITE_CONFIG,
151
+ initialVal: viteConfig,
152
+ opts: {
153
+ viteConfig,
154
+ data,
155
+ viteCompilerContext
156
+ },
157
+ })
158
+ },
159
+ async modifyBuildAssets(assets, miniPlugin) {
160
+ await ctx.applyPlugins({
161
+ name: hooks.MODIFY_BUILD_ASSETS,
162
+ initialVal: assets,
163
+ opts: {
164
+ assets,
165
+ miniPlugin,
166
+ },
167
+ })
168
+ },
169
+ async modifyMiniConfigs(configMap) {
170
+ await ctx.applyPlugins({
171
+ name: hooks.MODIFY_MINI_CONFIGS,
172
+ initialVal: configMap,
173
+ opts: {
174
+ configMap,
175
+ },
176
+ })
177
+ },
178
+ async modifyComponentConfig(componentConfig, config) {
179
+ await ctx.applyPlugins({
180
+ name: hooks.MODIFY_COMPONENT_CONFIG,
181
+ opts: {
182
+ componentConfig,
183
+ config,
184
+ },
185
+ })
186
+ },
187
+ async onCompilerMake(compilation, compiler, plugin) {
188
+ await ctx.applyPlugins({
189
+ name: hooks.ON_COMPILER_MAKE,
190
+ opts: {
191
+ compilation,
192
+ compiler,
193
+ plugin,
194
+ },
195
+ })
196
+ },
197
+ async onParseCreateElement(nodeName, componentConfig) {
198
+ await ctx.applyPlugins({
199
+ name: hooks.ON_PARSE_CREATE_ELEMENT,
200
+ opts: {
201
+ nodeName,
202
+ componentConfig,
203
+ },
204
+ })
205
+ },
206
+ async onBuildFinish({ error, stats, isWatch }) {
207
+ await ctx.applyPlugins({
208
+ name: hooks.ON_BUILD_FINISH,
209
+ opts: {
210
+ error,
211
+ stats,
212
+ isWatch,
213
+ },
214
+ })
215
+ },
216
+ },
217
+ },
218
+ })
219
+ await ctx.applyPlugins(hooks.ON_BUILD_COMPLETE)
220
+ },
221
+ })
222
+ }
223
+
224
+ async function checkConfig ({ projectConfig, helper }) {
225
+ const result = await validateConfig(projectConfig, helper)
226
+ return result
227
+ }
@@ -0,0 +1,86 @@
1
+ import * as path from 'node:path'
2
+
3
+ import type { IPluginContext } from '@tarojs/service'
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ ctx.registerCommand({
7
+ name: 'config',
8
+ optionsMap: {
9
+ '--json': '以 JSON 形式输出'
10
+ },
11
+ synopsisList: [
12
+ 'taro config set <key> <value>',
13
+ 'taro config get <key>',
14
+ 'taro config delete <key>',
15
+ 'taro config list [--json]'
16
+ ],
17
+ fn ({ _, options }) {
18
+ const [, cmd, key, value] = _
19
+ const json = !!options.json
20
+ const { fs, getUserHomeDir, TARO_CONFIG_FOLDER, TARO_BASE_CONFIG } = ctx.helper
21
+ const homedir = getUserHomeDir()
22
+ const configPath = path.join(homedir, `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`)
23
+ if (!homedir) return console.log('找不到用户根目录')
24
+
25
+ function displayConfigPath (configPath) {
26
+ console.log(`Config path: ${configPath}`)
27
+ console.log()
28
+ }
29
+
30
+ switch (cmd) {
31
+ case 'get':
32
+ if (!key) return console.log('Usage: taro config get <key>')
33
+ if (fs.existsSync(configPath)) {
34
+ displayConfigPath(configPath)
35
+ const config = fs.readJSONSync(configPath)
36
+ console.log(`key: ${key}, value: ${config[key]}`)
37
+ }
38
+ break
39
+ case 'set':
40
+ if (!key || !value) return console.log('Usage: taro config set <key> <value>')
41
+
42
+ if (fs.existsSync(configPath)) {
43
+ displayConfigPath(configPath)
44
+ const config = fs.readJSONSync(configPath)
45
+ config[key] = value
46
+ fs.writeJSONSync(configPath, config)
47
+ } else {
48
+ fs.ensureFileSync(configPath)
49
+ fs.writeJSONSync(configPath, {
50
+ [key]: value
51
+ })
52
+ }
53
+ console.log(`set key: ${key}, value: ${value}`)
54
+ break
55
+ case 'delete':
56
+ if (!key) return console.log('Usage: taro config delete <key>')
57
+
58
+ if (fs.existsSync(configPath)) {
59
+ displayConfigPath(configPath)
60
+ const config = fs.readJSONSync(configPath)
61
+ delete config[key]
62
+ fs.writeJSONSync(configPath, config)
63
+ }
64
+ console.log(`deleted: ${key}`)
65
+ break
66
+ case 'list':
67
+ case 'ls':
68
+ if (fs.existsSync(configPath)) {
69
+ displayConfigPath(configPath)
70
+ console.log('Config info:')
71
+ const config = fs.readJSONSync(configPath)
72
+ if (json) {
73
+ console.log(JSON.stringify(config, null, 2))
74
+ } else {
75
+ for (const key in config) {
76
+ console.log(`${key}=${config[key]}`)
77
+ }
78
+ }
79
+ }
80
+ break
81
+ default:
82
+ break
83
+ }
84
+ }
85
+ })
86
+ }
@@ -0,0 +1,105 @@
1
+ import * as hooks from '../constant'
2
+
3
+ import type { IPluginContext } from '@tarojs/service'
4
+ import type { TSetCustomTemplateConfig } from '../../create/page'
5
+
6
+ declare const enum createTemplateTypeEnum {
7
+ /**
8
+ * taro页面,taro使用者使用
9
+ */
10
+ PAGE = 'page',
11
+ /**
12
+ * taro插件,用于扩展命令行
13
+ */
14
+ PLUGIN_COMMAND = 'plugin-command',
15
+ /**
16
+ * taro插件,用于扩展编译过程
17
+ */
18
+ PLUGIN_BUILD = 'plugin-build',
19
+ /**
20
+ * taro插件,用于扩展 taro create 自定义模版
21
+ */
22
+ PLUGIN_TEMPLATE = 'plugin-template'
23
+ }
24
+
25
+ export default (ctx: IPluginContext) => {
26
+ ctx.registerCommand({
27
+ name: 'create',
28
+ optionsMap: {
29
+ '--name [name]': '名称',
30
+ '--dir [dir]': '路径',
31
+ '--subpkg [subpkg]': '分包路径',
32
+ '--description [description]': '介绍',
33
+ '--type [type]': '模版类型(page(默认)|plugin-command|plugin-build|plugin-template)'
34
+ },
35
+ synopsisList: [
36
+ 'taro create page',
37
+ 'taro create --name=page --description=desc',
38
+ 'taro create my-plugin --type=plugin-command',
39
+ ],
40
+ fn ({ _, options }) {
41
+ const type = options.type || createTemplateTypeEnum.PAGE
42
+ const name = _[1] || options.name
43
+ const description = options.description || ''
44
+ const afterCreate = options.afterCreate
45
+ const templateSource = options.templateSource
46
+ const framework = options.framework
47
+ const css = options.css
48
+ const typescript = options.typescript
49
+ const clone = options.clone
50
+ const { chalk } = ctx.helper
51
+ const { appPath } = ctx.paths
52
+
53
+ switch (type) {
54
+ case createTemplateTypeEnum.PAGE: {
55
+ if (typeof name !== 'string') {
56
+ return console.log(chalk.red('请输入需要创建的页面名称'))
57
+ }
58
+
59
+ const Page = require('../../create/page').default
60
+ const page = new Page({
61
+ clone,
62
+ subPkg: options.subpkg,
63
+ framework,
64
+ css,
65
+ typescript,
66
+ pageDir: options.dir,
67
+ pageName: name,
68
+ projectDir: appPath,
69
+ description,
70
+ templateSource,
71
+ afterCreate,
72
+ async modifyCustomTemplateConfig (cb: TSetCustomTemplateConfig) {
73
+ await ctx.applyPlugins({ name: hooks.MODIFY_CREATE_TEMPLATE, opts: cb })
74
+ }
75
+ })
76
+
77
+ page.create()
78
+ break
79
+ }
80
+ case createTemplateTypeEnum.PLUGIN_COMMAND:
81
+ case createTemplateTypeEnum.PLUGIN_BUILD:
82
+ case createTemplateTypeEnum.PLUGIN_TEMPLATE:
83
+ {
84
+ if (typeof name !== 'string') {
85
+ return console.log(chalk.red('请输入需要创建的插件名称'))
86
+ }
87
+
88
+ const Plugin = require('../../create/plugin').default
89
+ const plugin = new Plugin({
90
+ pluginName: name,
91
+ projectDir: appPath,
92
+ type,
93
+ description,
94
+ template: 'plugin-compile'
95
+ })
96
+
97
+ plugin.create()
98
+ break
99
+ }
100
+ default:
101
+ break
102
+ }
103
+ }
104
+ })
105
+ }
@@ -0,0 +1,140 @@
1
+ import * as path from 'node:path'
2
+
3
+ import * as ora from 'ora'
4
+ import * as validatePkgName from 'validate-npm-package-name'
5
+
6
+ import { execCommand, getPkgNameByFilterVersion, getRootPath } from '../../util'
7
+
8
+ import type { IPluginContext } from '@tarojs/service'
9
+
10
+ type TPresetOrPluginAction = 'install' | 'uninstall'
11
+ type TPluginType = 'plugin' | 'preset'
12
+
13
+ const PRESET_OR_PLUGIN_COMMAND_CHINESE_MAP_MAP = {
14
+ install: '添加',
15
+ uninstall: '删除'
16
+ }
17
+ const PRESET_OR_PLUGIN_CHINESE_NAME_MAP = {
18
+ plugin: '插件',
19
+ preset: '插件集'
20
+ }
21
+
22
+ const PLUGIN_TYPE_TO_CONFIG_KEY = {
23
+ plugin: 'plugins',
24
+ preset: 'presets'
25
+ }
26
+
27
+ export default (ctx: IPluginContext) => {
28
+ ctx.registerCommand({
29
+ name: 'global-config',
30
+ synopsisList: [
31
+ 'taro global-config add-plugin [pluginName]',
32
+ 'taro global-config remove-plugin [pluginName]',
33
+ 'taro global-config add-preset [presetName]',
34
+ 'taro global-config remove-preset [presetName]',
35
+ 'taro global-config reset',
36
+ ],
37
+ optionsMap: {
38
+ '-r --registry [url]': '指定 npm registry',
39
+ '-h, --help': 'output usage information'
40
+ },
41
+ fn ({ _, options }) {
42
+ const [, action, pluginName] = _
43
+ const { getUserHomeDir, TARO_GLOBAL_CONFIG_DIR, fs, TARO_GLOBAL_CONFIG_FILE } = ctx.helper
44
+ const homedir = getUserHomeDir()
45
+ const globalPluginConfigDir = path.join(homedir, TARO_GLOBAL_CONFIG_DIR)
46
+ if (!homedir) return console.log('找不到用户根目录')
47
+ const rootPath = getRootPath()
48
+ const templatePath = path.join(rootPath, 'templates', 'global-config')
49
+ const registry = options.registry || options.r
50
+ function makeSureConfigExists () {
51
+ if (!fs.existsSync(globalPluginConfigDir)) {
52
+ const spinner = ora(`目录不存在,全局配置初始化`).start()
53
+ try {
54
+ fs.copySync(templatePath, globalPluginConfigDir)
55
+ spinner.succeed(`全局配置初始化成功,${globalPluginConfigDir}`)
56
+ } catch (e) {
57
+ spinner.fail(`全局配置初始化失败,${e}`)
58
+ process.exit(1)
59
+ }
60
+ }
61
+ }
62
+ function addOrRemovePresetOrPlugin (actionType: TPresetOrPluginAction, pluginType: TPluginType) {
63
+ makeSureConfigExists()
64
+ const presetOrPluginChineseName = PRESET_OR_PLUGIN_CHINESE_NAME_MAP[pluginType]
65
+ const chineseCommand = PRESET_OR_PLUGIN_COMMAND_CHINESE_MAP_MAP[actionType]
66
+ if (!pluginName) {
67
+ console.error(`缺少要${chineseCommand}的${presetOrPluginChineseName}`)
68
+ process.exit(1)
69
+ }
70
+
71
+ const spinner = ora(`开始${chineseCommand}${presetOrPluginChineseName} ${pluginName}`).start()
72
+ const pluginWithoutVersionName = getPkgNameByFilterVersion(pluginName)
73
+ if (!validatePkgName(pluginWithoutVersionName).validForNewPackages) {
74
+ spinner.fail('安装的插件名不合规!')
75
+ process.exit(1)
76
+ }
77
+ let command = `cd ${globalPluginConfigDir} && npm ${actionType} ${pluginName}`
78
+ if (registry) {
79
+ command += ` --registry=${registry}`
80
+ }
81
+ execCommand({
82
+ command,
83
+ successCallback (data) {
84
+ console.log(data.replace(/\n$/, ''))
85
+ spinner.start(`开始修改${presetOrPluginChineseName}配置`)
86
+ const configFilePath = path.join(globalPluginConfigDir, TARO_GLOBAL_CONFIG_FILE)
87
+ let globalConfig
88
+ try {
89
+ globalConfig = fs.readJSONSync(configFilePath)
90
+ } catch (e) {
91
+ spinner.fail('获取配置文件失败')
92
+ }
93
+ const configKey = PLUGIN_TYPE_TO_CONFIG_KEY[pluginType]
94
+ const configItem = globalConfig[configKey] || []
95
+ const pluginIndex = configItem.findIndex((item) => {
96
+ if (typeof item === 'string') return item === pluginWithoutVersionName
97
+ if (item instanceof Array) return item?.[0] === pluginWithoutVersionName
98
+ })
99
+ const shouldChangeFile = !(Number(pluginIndex !== -1) ^ Number(actionType === 'uninstall'))
100
+ if (shouldChangeFile) {
101
+ actionType === 'install' ? configItem.push(pluginWithoutVersionName) : configItem.splice(pluginIndex, 1)
102
+ try {
103
+ fs.writeJSONSync(configFilePath, {
104
+ [configKey]: configItem
105
+ })
106
+ } catch (e) {
107
+ spinner.fail(`修改配置文件失败:${e}`)
108
+ }
109
+ }
110
+ spinner.succeed('修改配置文件成功')
111
+ },
112
+ failCallback (data) {
113
+ spinner.stop()
114
+ spinner.warn(data.replace(/\n$/, ''))
115
+ }
116
+ })
117
+ }
118
+ switch (action) {
119
+ case 'add-plugin':
120
+ addOrRemovePresetOrPlugin('install', 'plugin')
121
+ break
122
+ case 'remove-plugin' :
123
+ addOrRemovePresetOrPlugin('uninstall', 'plugin')
124
+ break
125
+ case 'add-preset':
126
+ addOrRemovePresetOrPlugin('install', 'preset')
127
+ break
128
+ case 'remove-preset' :
129
+ addOrRemovePresetOrPlugin('uninstall', 'preset')
130
+ break
131
+ case 'reset':
132
+ if (fs.existsSync(globalPluginConfigDir)) fs.removeSync(globalPluginConfigDir)
133
+ fs.copySync(templatePath, globalPluginConfigDir)
134
+ break
135
+ default:
136
+ console.error('请输出正确的参数')
137
+ }
138
+ }
139
+ })
140
+ }
@@ -0,0 +1,19 @@
1
+ import { chalk } from '@tarojs/helper'
2
+
3
+ import type { IPluginContext } from '@tarojs/service'
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ ctx.registerCommand({
7
+ name: 'help',
8
+ synopsisList: [
9
+ 'taro help [cmd]'
10
+ ],
11
+ async fn ({ _ }) {
12
+ const cmd = _[1]
13
+
14
+ if (!cmd) return console.log(chalk.yellow('用法:taro help [cmd],cmd 不存在!请输入需要查看帮助的命令名称。'))
15
+
16
+ ctx.ctx.runHelp(cmd)
17
+ }
18
+ })
19
+ }
@@ -0,0 +1,77 @@
1
+ import * as path from 'node:path'
2
+
3
+ import * as envinfo from 'envinfo'
4
+
5
+ import { getPkgVersion } from '../../util'
6
+
7
+ import type { IPluginContext } from '@tarojs/service'
8
+
9
+ export default (ctx: IPluginContext) => {
10
+ ctx.registerCommand({
11
+ name: 'info',
12
+ synopsisList: [
13
+ 'taro info',
14
+ 'taro info rn'
15
+ ],
16
+ async fn ({ _ }) {
17
+ const rn = _[1] === 'rn'
18
+ const { fs, chalk, PROJECT_CONFIG } = ctx.helper
19
+ const { appPath, configPath } = ctx.paths
20
+
21
+ if (!configPath || !fs.existsSync(configPath)) {
22
+ console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`))
23
+ process.exit(1)
24
+ }
25
+
26
+ if (rn) {
27
+ const tempPath = path.join(appPath, '.rn_temp')
28
+ if (fs.lstatSync(tempPath).isDirectory()) {
29
+ process.chdir('.rn_temp')
30
+ }
31
+ }
32
+
33
+ await info({}, ctx)
34
+ }
35
+ })
36
+ }
37
+
38
+ // 单独function获取@jdtaro相关的包
39
+ function getJdtaroPackages(ctx) {
40
+ try {
41
+ const { appPath } = ctx.paths
42
+ const fs = ctx.helper.fs
43
+ const packageJsonPath = path.join(appPath, 'package.json')
44
+
45
+ if (fs.existsSync(packageJsonPath)) {
46
+ const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8')
47
+ const packageJson = JSON.parse(packageJsonContent)
48
+ const dependencies = Object.assign({}, packageJson.dependencies || {}, packageJson.devDependencies || {})
49
+
50
+ // 筛选出@jdtaro相关的包
51
+ return Object.keys(dependencies).filter(pkg => pkg.startsWith('@jdtaro/'))
52
+ }
53
+ } catch (error) {
54
+ // 记录错误但不中断程序执行(添加trycatch)
55
+ console.error('读取或解析package.json时发生错误:', error.message)
56
+ }
57
+
58
+ return []
59
+ }
60
+
61
+ async function info (options, ctx) {
62
+ let npmPackages = ctx.helper.UPDATE_PACKAGE_LIST.concat(['react', 'react-native', 'expo', 'taro-ui'])
63
+
64
+ // 调用新函数获取@jdtaro相关包
65
+ const jdtaroPackages = getJdtaroPackages(ctx)
66
+ npmPackages = npmPackages.concat(jdtaroPackages)
67
+
68
+ const info = await envinfo.run(Object.assign({}, {
69
+ System: ['OS', 'Shell'],
70
+ Binaries: ['Node', 'Yarn', 'npm'],
71
+ npmPackages,
72
+ npmGlobalPackages: ['typescript']
73
+ }, options), {
74
+ title: `Taro CLI ${getPkgVersion()} environment info`
75
+ })
76
+ console.log(info)
77
+ }