@ohos-ports/tarojs-cli 0.0.0-stage → 4.3.0-beta.1

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,324 @@
1
+ import * as path from 'node:path'
2
+
3
+ import {
4
+ chalk,
5
+ fs,
6
+ PROJECT_CONFIG,
7
+ shouldUseCnpm,
8
+ shouldUseYarn
9
+ } from '@tarojs/helper'
10
+ import { exec } from 'child_process'
11
+
12
+ import { getPkgVersion } from '../util'
13
+ import { run } from './utils'
14
+
15
+ const runUpdate = run('update', ['commands/update'])
16
+ const lastestVersion = getPkgVersion()
17
+
18
+ jest.mock('child_process', () => {
19
+ const exec = jest.fn()
20
+ exec.mockReturnValue({
21
+ stdout: {
22
+ on () {}
23
+ },
24
+ stderr: {
25
+ on () {}
26
+ }
27
+ })
28
+ return {
29
+ __esModule: true,
30
+ exec
31
+ }
32
+ })
33
+
34
+ jest.mock('ora', () => {
35
+ const ora = jest.fn()
36
+ ora.mockReturnValue({
37
+ start () {
38
+ return {
39
+ stop () {},
40
+ warn () {},
41
+ succeed () {}
42
+ }
43
+ }
44
+ })
45
+ return ora
46
+ })
47
+
48
+ jest.mock('@tarojs/helper', () => {
49
+ const helper = jest.requireActual('@tarojs/helper')
50
+ const fs = helper.fs
51
+ return {
52
+ __esModule: true,
53
+ ...helper,
54
+ shouldUseCnpm: jest.fn(),
55
+ shouldUseYarn: jest.fn(),
56
+ chalk: {
57
+ red: jest.fn(),
58
+ green () {}
59
+ },
60
+ fs: {
61
+ ...fs,
62
+ writeJson: jest.fn()
63
+ }
64
+ }
65
+ })
66
+
67
+ jest.mock('latest-version', () => () => lastestVersion)
68
+
69
+ function updatePkg (pkgPath: string, version: string) {
70
+ let packageMap = require(pkgPath)
71
+ packageMap = {
72
+ ...packageMap,
73
+ dependencies: {
74
+ ...packageMap.dependencies,
75
+ '@tarojs/shared': version,
76
+ '@tarojs/taro': version,
77
+ '@tarojs/cli': version,
78
+ '@tarojs/components': version,
79
+ '@tarojs/api': version,
80
+ '@tarojs/taro-h5': version,
81
+ '@tarojs/helper': version,
82
+ '@tarojs/taro-loader': version,
83
+ '@tarojs/react': version,
84
+ '@tarojs/router': version,
85
+ '@tarojs/runner-utils': version,
86
+ '@tarojs/runtime': version,
87
+ '@tarojs/service': version,
88
+ '@tarojs/with-weapp': version,
89
+ '@tarojs/taroize': version,
90
+ '@tarojs/plugin-platform-weapp': version,
91
+ '@tarojs/plugin-platform-alipay': version,
92
+ '@tarojs/plugin-platform-swan': version,
93
+ '@tarojs/plugin-platform-tt': version,
94
+ '@tarojs/plugin-platform-jd': version,
95
+ '@tarojs/plugin-platform-qq': version,
96
+ '@tarojs/plugin-platform-h5': version
97
+ },
98
+ devDependencies: {
99
+ ...packageMap.devDependencies,
100
+ 'babel-preset-taro': version,
101
+ 'eslint-config-taro': version,
102
+ 'babel-plugin-transform-taroapi': version,
103
+ 'postcss-plugin-constparse': version,
104
+ 'postcss-pxtransform': version
105
+ }
106
+ }
107
+ return packageMap
108
+ }
109
+
110
+ describe('update', () => {
111
+ const execMocked = (exec as unknown) as jest.Mock<any>
112
+ const shouldUseCnpmMocked = shouldUseCnpm as jest.Mock<any>
113
+ const shouldUseYarnMocked = shouldUseYarn as jest.Mock<any>
114
+ const writeJson = fs.writeJson as jest.Mock<any>
115
+
116
+ beforeEach(() => {
117
+ shouldUseCnpmMocked.mockReturnValue(false)
118
+ shouldUseYarnMocked.mockReturnValue(false)
119
+ })
120
+
121
+ afterEach(() => {
122
+ execMocked.mockClear()
123
+ shouldUseCnpmMocked.mockReset()
124
+ shouldUseYarnMocked.mockReset()
125
+ writeJson.mockClear()
126
+ })
127
+
128
+ it('should log errors', async () => {
129
+ const spy = jest.spyOn(console, 'log')
130
+ spy.mockImplementation(() => {})
131
+ await runUpdate('', {
132
+ options: {
133
+ npm: 'npm',
134
+ disableGlobalConfig: true
135
+ }
136
+ })
137
+ expect(spy).toBeCalledTimes(3)
138
+ spy.mockRestore()
139
+ })
140
+
141
+ it('should update self', async () => {
142
+ await runUpdate('', {
143
+ args: ['self'],
144
+ options: {
145
+ npm: 'npm',
146
+ disableGlobalConfig: true
147
+ }
148
+ })
149
+ expect(execMocked).toBeCalledWith(`npm i -g @tarojs/cli@${lastestVersion}`)
150
+ })
151
+
152
+ it('should update self using yarn', async () => {
153
+ shouldUseCnpmMocked.mockReturnValue(true)
154
+ await runUpdate('', {
155
+ args: ['self'],
156
+ options: {
157
+ npm: 'yarn',
158
+ disableGlobalConfig: true
159
+ }
160
+ })
161
+ expect(execMocked).toBeCalledWith(`yarn global add @tarojs/cli@${lastestVersion}`)
162
+ })
163
+
164
+ it('should update self using pnpm', async () => {
165
+ shouldUseCnpmMocked.mockReturnValue(true)
166
+ await runUpdate('', {
167
+ args: ['self'],
168
+ options: {
169
+ npm: 'pnpm',
170
+ disableGlobalConfig: true
171
+ }
172
+ })
173
+ expect(execMocked).toBeCalledWith(`pnpm add -g @tarojs/cli@${lastestVersion}`)
174
+ })
175
+
176
+ it('should update self using cnpm', async () => {
177
+ shouldUseCnpmMocked.mockReturnValue(true)
178
+ await runUpdate('', {
179
+ args: ['self'],
180
+ options: {
181
+ npm: 'cnpm',
182
+ disableGlobalConfig: true
183
+ }
184
+ })
185
+ expect(execMocked).toBeCalledWith(`cnpm i -g @tarojs/cli@${lastestVersion}`)
186
+ })
187
+
188
+ it('should update self to specific version', async () => {
189
+ const version = '3.0.0-beta.0'
190
+ await runUpdate('', {
191
+ args: ['self', version],
192
+ options: {
193
+ npm: 'npm',
194
+ disableGlobalConfig: true
195
+ }
196
+ })
197
+ expect(execMocked).toBeCalledWith(`npm i -g @tarojs/cli@${version}`)
198
+ })
199
+
200
+ it('should throw when there isn\'t a Taro project', async () => {
201
+ const chalkMocked = (chalk.red as unknown) as jest.Mock<any>
202
+ const exitSpy = jest.spyOn(process, 'exit')
203
+ const logSpy = jest.spyOn(console, 'log')
204
+ exitSpy.mockImplementation(() => {
205
+ throw new Error()
206
+ })
207
+ logSpy.mockImplementation(() => {})
208
+ try {
209
+ await runUpdate('', {
210
+ args: ['project'],
211
+ options: {
212
+ npm: 'npm',
213
+ disableGlobalConfig: true
214
+ }
215
+ })
216
+ } catch (error) {} // eslint-disable-line no-empty
217
+ expect(exitSpy).toBeCalledWith(1)
218
+ expect(chalkMocked).toBeCalledWith(`找不到项目配置文件 ${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`)
219
+ exitSpy.mockRestore()
220
+ logSpy.mockRestore()
221
+ })
222
+
223
+ it('should update project', async () => {
224
+ const appPath = path.resolve(__dirname, 'fixtures/default')
225
+ const pkgPath = path.join(appPath, 'package.json')
226
+ const packageMap = updatePkg(pkgPath, lastestVersion)
227
+
228
+ const logSpy = jest.spyOn(console, 'log')
229
+ logSpy.mockImplementation(() => {})
230
+
231
+ await runUpdate(appPath, {
232
+ args: ['project'],
233
+ options: {
234
+ npm: 'npm',
235
+ disableGlobalConfig: true
236
+ }
237
+ })
238
+ expect(writeJson.mock.calls[0][0]).toEqual(pkgPath)
239
+ expect(writeJson.mock.calls[0][1]).toEqual(packageMap)
240
+ expect(execMocked).toBeCalledWith('npm install')
241
+
242
+ logSpy.mockRestore()
243
+ })
244
+
245
+ it('should update project to specific version', async () => {
246
+ const version = '3.0.0-beta.4'
247
+ const appPath = path.resolve(__dirname, 'fixtures/default')
248
+ const pkgPath = path.join(appPath, 'package.json')
249
+ const packageMap = updatePkg(pkgPath, version)
250
+
251
+ const logSpy = jest.spyOn(console, 'log')
252
+ logSpy.mockImplementation(() => {})
253
+
254
+ await runUpdate(appPath, {
255
+ args: ['project', version],
256
+ options: {
257
+ npm: 'npm',
258
+ disableGlobalConfig: true
259
+ }
260
+ })
261
+ expect(writeJson.mock.calls[0][0]).toEqual(pkgPath)
262
+ expect(writeJson.mock.calls[0][1]).toEqual(packageMap)
263
+ expect(execMocked).toBeCalledWith('npm install')
264
+
265
+ logSpy.mockRestore()
266
+ })
267
+
268
+ it('should update project with yarn', async () => {
269
+ const appPath = path.resolve(__dirname, 'fixtures/default')
270
+
271
+ const logSpy = jest.spyOn(console, 'log')
272
+ logSpy.mockImplementation(() => {})
273
+ shouldUseYarnMocked.mockReturnValue(true)
274
+
275
+ await runUpdate(appPath, {
276
+ args: ['project'],
277
+ options: {
278
+ npm: 'yarn',
279
+ disableGlobalConfig: true
280
+ }
281
+ })
282
+ expect(execMocked).toBeCalledWith('yarn install')
283
+
284
+ logSpy.mockRestore()
285
+ })
286
+
287
+ it('should update project with pnpm', async () => {
288
+ const appPath = path.resolve(__dirname, 'fixtures/default')
289
+
290
+ const logSpy = jest.spyOn(console, 'log')
291
+ logSpy.mockImplementation(() => {})
292
+ shouldUseCnpmMocked.mockReturnValue(true)
293
+
294
+ await runUpdate(appPath, {
295
+ args: ['project'],
296
+ options: {
297
+ npm: 'pnpm',
298
+ disableGlobalConfig: true
299
+ }
300
+ })
301
+ expect(execMocked).toBeCalledWith('pnpm install')
302
+
303
+ logSpy.mockRestore()
304
+ })
305
+
306
+ it('should update project with cnpm', async () => {
307
+ const appPath = path.resolve(__dirname, 'fixtures/default')
308
+
309
+ const logSpy = jest.spyOn(console, 'log')
310
+ logSpy.mockImplementation(() => {})
311
+ shouldUseCnpmMocked.mockReturnValue(true)
312
+
313
+ await runUpdate(appPath, {
314
+ args: ['project'],
315
+ options: {
316
+ npm: 'cnpm',
317
+ disableGlobalConfig: true
318
+ }
319
+ })
320
+ expect(execMocked).toBeCalledWith('cnpm install')
321
+
322
+ logSpy.mockRestore()
323
+ })
324
+ })
@@ -0,0 +1,54 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { Config, Kernel } from '@tarojs/service'
4
+
5
+ interface IRunOptions {
6
+ options?: Record<string, string | boolean>
7
+ args?: string[]
8
+ }
9
+
10
+ interface IRun {
11
+ (appPath: string, options?: IRunOptions): Promise<Kernel>
12
+ }
13
+
14
+ export function run (name: string, presets: string[] = []): IRun {
15
+ return async function (appPath, opts = {}) {
16
+ const { options = {}, args = [] } = opts
17
+
18
+ const config = new Config({
19
+ appPath: appPath,
20
+ disableGlobalConfig: !!options.disableGlobalConfig
21
+ })
22
+ await config.init({
23
+ mode: (options.mode || process.env.NODE_ENV) as string,
24
+ command: name
25
+ })
26
+
27
+ const kernel = new Kernel({
28
+ appPath: appPath,
29
+ presets: [
30
+ path.resolve(__dirname, '../__mocks__', 'presets.ts'),
31
+ ...presets.map(e => path.isAbsolute(e) ? e : path.resolve(__dirname, '../../presets', `${e}.ts`))
32
+ ],
33
+ plugins: [],
34
+ config
35
+ })
36
+ kernel.optsPlugins ||= []
37
+
38
+ const type = options.type
39
+ if (typeof type === 'string' && !presets.some(e => e.includes(type))) {
40
+ kernel.optsPlugins.push(require.resolve(`@tarojs/plugin-platform-${options.type}`))
41
+ }
42
+
43
+ await kernel.run({
44
+ name,
45
+ opts: {
46
+ _: [name, ...args],
47
+ options,
48
+ isHelp: false
49
+ }
50
+ })
51
+
52
+ return kernel
53
+ }
54
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,250 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { dotenvParse, fs, patchEnv } from '@tarojs/helper'
4
+ import { Config, Kernel } from '@tarojs/service'
5
+ import * as minimist from 'minimist'
6
+
7
+ import customCommand from './commands/customCommand'
8
+ import { getPkgVersion } from './util'
9
+
10
+ const DISABLE_GLOBAL_CONFIG_COMMANDS = ['global-config', 'doctor', 'update', 'config']
11
+ const DEFAULT_FRAMEWORK = 'react'
12
+
13
+ export default class CLI {
14
+ appPath: string
15
+ constructor(appPath?: string) {
16
+ this.appPath = appPath || process.cwd()
17
+ const majorVersion = parseInt(process.version.substring(1).split('.')[0], 10)
18
+ if (majorVersion < 20) {
19
+ console.warn('Taro 将不再支持 Node.js 小于 20 的版本。请升级 Node.js 至 20 或更高版本。')
20
+ }
21
+ }
22
+
23
+ run () {
24
+ return this.parseArgs()
25
+ }
26
+
27
+ async parseArgs () {
28
+ const args = minimist(process.argv.slice(2), {
29
+ alias: {
30
+ version: ['v'],
31
+ help: ['h'],
32
+ port: ['p'],
33
+ resetCache: ['reset-cache'], // specially for rn, Removes cached files.
34
+ publicPath: ['public-path'], // specially for rn, assets public path.
35
+ bundleOutput: ['bundle-output'], // specially for rn, File name where to store the resulting bundle.
36
+ sourcemapOutput: ['sourcemap-output'], // specially for rn, File name where to store the sourcemap file for resulting bundle.
37
+ sourceMapUrl: ['sourcemap-use-absolute-path'], // specially for rn, Report SourceMapURL using its full path.
38
+ sourcemapSourcesRoot: ['sourcemap-sources-root'], // specially for rn, Path to make sourcemaps sources entries relative to.
39
+ assetsDest: ['assets-dest'], // specially for rn, Directory name where to store assets referenced in the bundle.
40
+ envPrefix: ['env-prefix'],
41
+ },
42
+ boolean: ['version', 'help', 'disable-global-config'],
43
+ default: {
44
+ build: true,
45
+ check: true,
46
+ 'inject-global-style': true
47
+ },
48
+ })
49
+ const _ = args._
50
+ const command = _[0]
51
+ if (command) {
52
+ const appPath = this.appPath
53
+ const presetsPath = path.resolve(__dirname, 'presets')
54
+ const commandsPath = path.resolve(presetsPath, 'commands')
55
+ const platformsPath = path.resolve(presetsPath, 'platforms')
56
+ const commandPlugins = fs.readdirSync(commandsPath)
57
+ const targetPlugin = `${command}.js`
58
+
59
+ // 设置环境变量
60
+ process.env.NODE_ENV ||= args.env
61
+ if (process.env.NODE_ENV === 'undefined' && (command === 'build' || command === 'inspect')) {
62
+ process.env.NODE_ENV = (args.watch ? 'development' : 'production')
63
+ }
64
+ args.type ||= args.t
65
+ if (args.type) {
66
+ process.env.TARO_ENV = args.type
67
+ }
68
+ if (typeof args.plugin === 'string') {
69
+ process.env.TARO_ENV = 'plugin'
70
+ }
71
+ const mode = args.mode || process.env.NODE_ENV
72
+ // 这里解析 dotenv 以便于 config 解析时能获取 dotenv 配置信息
73
+ const expandEnv = dotenvParse(appPath, args.envPrefix, mode)
74
+
75
+ const disableGlobalConfig = !!(args['disable-global-config'] || DISABLE_GLOBAL_CONFIG_COMMANDS.includes(command))
76
+
77
+ const configEnv = {
78
+ mode,
79
+ command,
80
+ }
81
+ const config = new Config({
82
+ appPath: this.appPath,
83
+ disableGlobalConfig: disableGlobalConfig
84
+ })
85
+ await config.init(configEnv)
86
+
87
+ const kernel = new Kernel({
88
+ appPath,
89
+ presets: [
90
+ path.resolve(__dirname, '.', 'presets', 'index.js')
91
+ ],
92
+ config,
93
+ plugins: []
94
+ })
95
+ kernel.optsPlugins ||= []
96
+
97
+ // 将自定义的 变量 添加到 config.env 中,实现 definePlugin 字段定义
98
+ const initialConfig = kernel.config?.initialConfig
99
+ if (initialConfig) {
100
+ initialConfig.env = patchEnv(initialConfig, expandEnv)
101
+ }
102
+ if (command === 'doctor') {
103
+ kernel.optsPlugins.push('@tarojs/plugin-doctor')
104
+ } else if (commandPlugins.includes(targetPlugin)) {
105
+ // 针对不同的内置命令注册对应的命令插件
106
+ kernel.optsPlugins.push(path.resolve(commandsPath, targetPlugin))
107
+ }
108
+
109
+ // 把内置命令插件传递给 kernel,可以暴露给其他插件使用
110
+ kernel.cliCommandsPath = commandsPath
111
+ kernel.cliCommands = commandPlugins
112
+ .filter(commandFileName => /^[\w-]+(\.[\w-]+)*\.js$/.test(commandFileName))
113
+ .map(fileName => fileName.replace(/\.js$/, ''))
114
+
115
+ switch (command) {
116
+ case 'inspect':
117
+ case 'build': {
118
+ let plugin
119
+ let platform = args.type
120
+ const { publicPath, bundleOutput, sourcemapOutput, sourceMapUrl, sourcemapSourcesRoot, assetsDest } = args
121
+
122
+ // 针对不同的内置平台注册对应的端平台插件
123
+ switch (platform) {
124
+ case 'weapp':
125
+ case 'ascf':
126
+ case 'alipay':
127
+ case 'swan':
128
+ case 'tt':
129
+ case 'qq':
130
+ case 'jd':
131
+ case 'h5':
132
+ case 'harmony-hybrid':
133
+ kernel.optsPlugins.push(`@tarojs/plugin-platform-${platform}`)
134
+ break
135
+ default: {
136
+ // plugin, rn
137
+ const platformPlugins = fs.readdirSync(platformsPath)
138
+ const targetPlugin = `${platform}.js`
139
+ if (platformPlugins.includes(targetPlugin)) {
140
+ kernel.optsPlugins.push(path.resolve(platformsPath, targetPlugin))
141
+ }
142
+ break
143
+ }
144
+ }
145
+
146
+ // 根据 framework 启用插件
147
+ const framework = kernel.config?.initialConfig.framework || DEFAULT_FRAMEWORK
148
+ const frameworkMap = {
149
+ vue3: '@tarojs/plugin-framework-vue3',
150
+ react: '@tarojs/plugin-framework-react',
151
+ preact: '@tarojs/plugin-framework-react',
152
+ solid: '@tarojs/plugin-framework-solid',
153
+ }
154
+ if (frameworkMap[framework]) {
155
+ kernel.optsPlugins.push(frameworkMap[framework])
156
+ }
157
+
158
+ // 编译小程序插件
159
+ if (typeof args.plugin === 'string') {
160
+ plugin = args.plugin
161
+ platform = 'plugin'
162
+ kernel.optsPlugins.push(path.resolve(platformsPath, 'plugin.js'))
163
+ if (plugin === 'weapp' || plugin === 'alipay' || plugin === 'jd') {
164
+ kernel.optsPlugins.push(`@tarojs/plugin-platform-${plugin}`)
165
+ }
166
+ }
167
+
168
+ // 传递 inspect 参数即可
169
+ if (command === 'inspect') {
170
+ customCommand(command, kernel, args)
171
+ break
172
+ }
173
+ customCommand(command, kernel, {
174
+ args,
175
+ _,
176
+ platform,
177
+ plugin,
178
+ isWatch: Boolean(args.watch),
179
+ // Note: 是否把 Taro 组件编译为原生自定义组件
180
+ isBuildNativeComp: _[1] === 'native-components',
181
+ // Note: 新的混合编译模式,支持把组件单独编译为原生组件
182
+ newBlended: Boolean(args['new-blended']),
183
+ // Note: 是否禁用编译
184
+ withoutBuild: !args.build,
185
+ noInjectGlobalStyle: !args['inject-global-style'],
186
+ noCheck: !args.check,
187
+ port: args.port,
188
+ env: args.env,
189
+ deviceType: args.platform,
190
+ resetCache: !!args.resetCache,
191
+ publicPath,
192
+ bundleOutput,
193
+ sourcemapOutput,
194
+ sourceMapUrl,
195
+ sourcemapSourcesRoot,
196
+ assetsDest,
197
+ qr: !!args.qr,
198
+ blended: Boolean(args.blended),
199
+ h: args.h
200
+ })
201
+ break
202
+ }
203
+ case 'init': {
204
+ customCommand(command, kernel, {
205
+ _,
206
+ appPath,
207
+ projectName: _[1] || args.name,
208
+ description: args.description,
209
+ typescript: args.typescript,
210
+ buildEs5: args['build-es5'],
211
+ framework: args.framework,
212
+ compiler: args.compiler,
213
+ npm: args.npm,
214
+ templateSource: args['template-source'],
215
+ clone: !!args.clone,
216
+ template: args.template,
217
+ css: args.css,
218
+ autoInstall: args.autoInstall,
219
+ h: args.h
220
+ })
221
+ break
222
+ }
223
+ default:
224
+ customCommand(command, kernel, args)
225
+ break
226
+ }
227
+ } else {
228
+ if (args.h) {
229
+ console.log('Usage: taro <command> [options]')
230
+ console.log()
231
+ console.log('Options:')
232
+ console.log(' -v, --version output the version number')
233
+ console.log(' -h, --help output usage information')
234
+ console.log()
235
+ console.log('Commands:')
236
+ console.log(' init [projectName] Init a project with default templete')
237
+ console.log(' config <cmd> Taro config')
238
+ console.log(' create Create page for project')
239
+ console.log(' build Build a project with options')
240
+ console.log(' update Update packages of taro')
241
+ console.log(' info Diagnostics Taro env info')
242
+ console.log(' doctor Diagnose taro project')
243
+ console.log(' inspect Inspect the webpack config')
244
+ console.log(' help [cmd] display help for [cmd]')
245
+ } else if (args.v) {
246
+ console.log(getPkgVersion())
247
+ }
248
+ }
249
+ }
250
+ }
@@ -0,0 +1,26 @@
1
+ import { Kernel } from '@tarojs/service'
2
+
3
+ export default function customCommand (
4
+ command: string,
5
+ kernel: Kernel,
6
+ args: { _: string[], [key: string]: any }
7
+ ) {
8
+ if (typeof command === 'string') {
9
+ const options: any = {}
10
+ const excludeKeys = ['_', 'version', 'v', 'help', 'h', 'disable-global-config']
11
+ Object.keys(args).forEach(key => {
12
+ if (!excludeKeys.includes(key)) {
13
+ options[key] = args[key]
14
+ }
15
+ })
16
+
17
+ kernel.run({
18
+ name: command,
19
+ opts: {
20
+ _: args._,
21
+ options,
22
+ isHelp: args.h
23
+ }
24
+ })
25
+ }
26
+ }
@@ -0,0 +1,7 @@
1
+ export default {
2
+ OUTPUT_DIR: 'dist',
3
+ SOURCE_DIR: 'src',
4
+ TEMP_DIR: '.temp',
5
+ NPM_DIR: 'npm',
6
+ ENTRY: 'app'
7
+ }
@@ -0,0 +1,35 @@
1
+ {
2
+ "package": "com.application.demo",
3
+ "name": "TaroQuickApp",
4
+ "versionName": "1.0.0",
5
+ "versionCode": 1,
6
+ "minPlatformVersion": 1040,
7
+ "features": [
8
+ { "name": "system.prompt" },
9
+ { "name": "system.router" },
10
+ { "name": "system.shortcut" },
11
+ { "name": "system.fetch" },
12
+ { "name": "system.storage" },
13
+ { "name": "system.barcode" },
14
+ { "name": "system.vibrator" },
15
+ { "name": "system.clipboard" },
16
+ { "name": "system.sensor" },
17
+ { "name": "system.geolocation" },
18
+ { "name": "system.share" },
19
+ { "name": "system.notification" },
20
+ { "name": "system.device" },
21
+ { "name": "system.webview" },
22
+ { "name": "system.request" },
23
+ { "name": "system.audio" },
24
+ { "name": "system.record" },
25
+ { "name": "system.contact" },
26
+ { "name": "system.sms" }
27
+ ],
28
+ "icon": "logo.png",
29
+ "permissions": [
30
+ { "origin": "*" }
31
+ ],
32
+ "config": {
33
+ "logLevel": "debug"
34
+ }
35
+ }