@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,74 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { chalk } from '@tarojs/helper'
4
+
5
+ import { getPkgVersion } from '../util'
6
+ import { run } from './utils'
7
+
8
+ jest.mock('envinfo', () => {
9
+ const envinfo = jest.requireActual('envinfo')
10
+ return {
11
+ __esModule: true,
12
+ async run(data, options) {
13
+ const res = await envinfo.run(data, { ...options, json: true })
14
+ return JSON.parse(res)
15
+ },
16
+ }
17
+ })
18
+
19
+ const runInfo = run('info', ['commands/info'])
20
+
21
+ describe('info', () => {
22
+ it("should exit because there isn't a Taro project", async () => {
23
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
24
+ const logSpy = jest.spyOn(console, 'log')
25
+
26
+ exitSpy.mockImplementation(() => {
27
+ throw new Error()
28
+ })
29
+ logSpy.mockImplementation(() => {})
30
+
31
+ try {
32
+ await runInfo('')
33
+ } catch (error) {} // eslint-disable-line no-empty
34
+
35
+ expect(exitSpy).toBeCalledWith(1)
36
+ expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'))
37
+
38
+ exitSpy.mockRestore()
39
+ logSpy.mockRestore()
40
+ })
41
+
42
+ it('should log information', async () => {
43
+ const logSpy = jest.spyOn(console, 'log')
44
+ logSpy.mockImplementation(() => {})
45
+
46
+ const appPath = path.resolve(__dirname, 'fixtures/default')
47
+ await runInfo(appPath)
48
+
49
+ expect(logSpy).toBeCalledTimes(1)
50
+ const res = logSpy.mock.calls[0][0]
51
+ const title = `Taro CLI ${getPkgVersion()} environment info`
52
+ expect(res.hasOwnProperty(title)).toBeTruthy()
53
+ const info = res[title]
54
+ expect('System' in info).toBeTruthy()
55
+ expect('Binaries' in info).toBeTruthy()
56
+ // envinfo 还不支持 yarn workspace
57
+ // expect('npmPackages' in info).toBeTruthy()
58
+ // Note: windows 操作系统可能不存在 System.Shell
59
+ expect(Object.keys(info.System)).toEqual(expect.arrayContaining(['OS']))
60
+ // Note: 环境内可能不包括 Yarn
61
+ expect(Object.keys(info.Binaries)).toEqual(expect.arrayContaining(['Node', 'npm']))
62
+ // expect(info.npmPackages.hasOwnProperty('@tarojs/helper')).toBeTruthy()
63
+ // expect(info.npmPackages.hasOwnProperty('@tarojs/service')).toBeTruthy()
64
+ // expect(info.npmPackages.hasOwnProperty('@tarojs/taro')).toBeTruthy()
65
+ // expect(info.npmPackages.hasOwnProperty('@tarojs/taroize')).toBeTruthy()
66
+ // expect(info.npmPackages.hasOwnProperty('@tarojs/webpack-runner')).toBeTruthy()
67
+ // expect(info.npmPackages.hasOwnProperty('babel-plugin-transform-taroapi')).toBeTruthy()
68
+ // expect(info.npmPackages.hasOwnProperty('eslint-config-taro')).toBeTruthy()
69
+ // expect(info.npmPackages.hasOwnProperty('eslint-plugin-taro')).toBeTruthy()
70
+ // expect(info.npmPackages.hasOwnProperty('postcss-pxtransform')).toBeTruthy()
71
+
72
+ logSpy.mockRestore()
73
+ })
74
+ })
@@ -0,0 +1,163 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { chalk, fs } from '@tarojs/helper'
4
+
5
+ import { run } from './utils'
6
+
7
+ jest.mock('cli-highlight', () => {
8
+ return {
9
+ __esModule: true,
10
+ default (str) {
11
+ return str
12
+ }
13
+ }
14
+ })
15
+
16
+ jest.mock('@tarojs/helper', () => {
17
+ const helper = jest.requireActual('@tarojs/helper')
18
+ const fs = helper.fs
19
+ return {
20
+ __esModule: true,
21
+ ...helper,
22
+ fs: {
23
+ ...fs,
24
+ writeFileSync: jest.fn()
25
+ }
26
+ }
27
+ })
28
+
29
+ const runInspect = run('inspect', [
30
+ 'commands/build',
31
+ 'commands/inspect',
32
+ require.resolve('@tarojs/plugin-platform-weapp')
33
+ ])
34
+
35
+ describe('inspect', () => {
36
+ beforeEach(() => {
37
+ jest.resetModules()
38
+ })
39
+
40
+ it('should exit because there isn\'t a Taro project', async () => {
41
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
42
+ const logSpy = jest.spyOn(console, 'log')
43
+
44
+ exitSpy.mockImplementation(() => {
45
+ throw new Error()
46
+ })
47
+ logSpy.mockImplementation(() => {})
48
+
49
+ try {
50
+ await runInspect('')
51
+ } catch (error) {} // eslint-disable-line no-empty
52
+
53
+ expect(exitSpy).toBeCalledWith(1)
54
+ expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'))
55
+
56
+ exitSpy.mockRestore()
57
+ logSpy.mockRestore()
58
+ })
59
+
60
+ it('should exit when user haven\'t pass correct type', async () => {
61
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
62
+ const logSpy = jest.spyOn(console, 'log')
63
+
64
+ exitSpy.mockImplementation(() => {
65
+ throw new Error()
66
+ })
67
+ logSpy.mockImplementation(() => {})
68
+
69
+ try {
70
+ await runInspect(path.resolve(__dirname, 'fixtures/default'))
71
+ } catch (error) {} // eslint-disable-line no-empty
72
+
73
+ expect(exitSpy).toBeCalledWith(0)
74
+ expect(logSpy).toBeCalledWith(chalk.red('请传入正确的编译类型!'))
75
+
76
+ exitSpy.mockRestore()
77
+ logSpy.mockRestore()
78
+ })
79
+
80
+ it('should log config', async () => {
81
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
82
+ const logSpy = jest.spyOn(console, 'info')
83
+
84
+ exitSpy.mockImplementation(() => {
85
+ throw new Error()
86
+ })
87
+ logSpy.mockImplementation(() => {})
88
+
89
+ try {
90
+ const appPath = path.resolve(__dirname, 'fixtures/default')
91
+ await runInspect(appPath, {
92
+ options: {
93
+ type: 'weapp'
94
+ }
95
+ })
96
+ } catch (error) {} // eslint-disable-line no-empty
97
+
98
+ expect(exitSpy).toBeCalledWith(0)
99
+ expect(logSpy).toBeCalledTimes(1)
100
+
101
+ exitSpy.mockRestore()
102
+ logSpy.mockRestore()
103
+ })
104
+
105
+ it('should log specific config', async () => {
106
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
107
+ const logSpy = jest.spyOn(console, 'info')
108
+ const errorSpy = jest.spyOn(console, 'error')
109
+
110
+ exitSpy.mockImplementation(() => {
111
+ throw new Error()
112
+ })
113
+ logSpy.mockImplementation(() => {})
114
+ errorSpy.mockImplementation(() => {})
115
+
116
+ try {
117
+ const appPath = path.resolve(__dirname, 'fixtures/default')
118
+ await runInspect(appPath, {
119
+ options: {
120
+ type: 'h5'
121
+ },
122
+ args: ['resolve.mainFields.0']
123
+ })
124
+ } catch (error) {} // eslint-disable-line no-empty
125
+
126
+ expect(exitSpy).toBeCalledWith(0)
127
+ expect(logSpy).toBeCalledTimes(1)
128
+ expect(logSpy).toBeCalledWith('\'main:h5\'')
129
+
130
+ exitSpy.mockRestore()
131
+ logSpy.mockRestore()
132
+ errorSpy.mockRestore()
133
+ })
134
+
135
+ it('should output config', async () => {
136
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
137
+ const writeFileSync = fs.writeFileSync as jest.Mock<any>
138
+ const outputPath = 'project-config.js'
139
+
140
+ exitSpy.mockImplementation(() => {
141
+ throw new Error()
142
+ })
143
+
144
+ try {
145
+ const appPath = path.resolve(__dirname, 'fixtures/default')
146
+ await runInspect(appPath, {
147
+ options: {
148
+ type: 'weapp',
149
+ output: outputPath
150
+ },
151
+ args: ['resolve.mainFields.0']
152
+ })
153
+ } catch (error) {} // eslint-disable-line no-empty
154
+
155
+ expect(exitSpy).toBeCalledWith(0)
156
+ expect(writeFileSync).toBeCalledWith(
157
+ outputPath,
158
+ expect.stringMatching(/^'(browser|main:mini)'$/)
159
+ )
160
+
161
+ exitSpy.mockRestore()
162
+ })
163
+ })
@@ -0,0 +1,304 @@
1
+ import Project from '../create/project'
2
+
3
+ jest.mock('@tarojs/helper', () => ({
4
+ chalk: {
5
+ blueBright: jest.fn((text) => text),
6
+ green: jest.fn((text) => text),
7
+ red: jest.fn((text) => text)
8
+ },
9
+ DEFAULT_TEMPLATE_SRC: 'https://github.com/NervJS/taro-project-templates',
10
+ DEFAULT_TEMPLATE_SRC_GITEE: 'https://gitee.com/o2team/taro-project-templates',
11
+ fs: {
12
+ ensureDirSync: jest.fn(),
13
+ existsSync: jest.fn(() => true)
14
+ },
15
+ getUserHomeDir: jest.fn(() => '/tmp'),
16
+ SOURCE_DIR: 'src',
17
+ TARO_BASE_CONFIG: 'index.json',
18
+ TARO_CONFIG_FOLDER: '.taro'
19
+ }))
20
+
21
+ jest.mock('@tarojs/shared', () => ({
22
+ isArray: Array.isArray
23
+ }), { virtual: true })
24
+
25
+ jest.mock('@tarojs/binding', () => ({
26
+ CompilerType: {
27
+ Solid: 'solid',
28
+ Vite: 'vite',
29
+ Webpack5: 'webpack5'
30
+ },
31
+ createProject: jest.fn(() => Promise.resolve()),
32
+ CSSType: {
33
+ None: 'none'
34
+ },
35
+ FrameworkType: {
36
+ Preact: 'preact',
37
+ React: 'react',
38
+ Solid: 'solid',
39
+ Vue3: 'vue3'
40
+ },
41
+ NpmType: {
42
+ Npm: 'npm'
43
+ },
44
+ PeriodType: {
45
+ CreateAPP: 'createApp'
46
+ }
47
+ }))
48
+
49
+ describe('create project', () => {
50
+ let logSpy: jest.SpyInstance
51
+
52
+ beforeEach(() => {
53
+ logSpy = jest.spyOn(console, 'log').mockImplementation()
54
+ })
55
+
56
+ afterEach(() => {
57
+ logSpy.mockRestore()
58
+ })
59
+
60
+ it('should resolve dot project name to current directory', () => {
61
+ const project = new Project({
62
+ projectDir: '/a/b/current-project',
63
+ projectName: '.',
64
+ sourceRoot: __dirname,
65
+ template: 'default',
66
+ templateSource: 'default-template',
67
+ npm: 'npm',
68
+ css: 'none',
69
+ framework: 'react'
70
+ } as any)
71
+
72
+ expect(project.conf.projectDir).toBe('/a/b')
73
+ expect(project.conf.projectName).toBe('current-project')
74
+ })
75
+
76
+ it('should resolve current directory path project name to current directory', () => {
77
+ const project = new Project({
78
+ projectDir: '/a/b/current-project',
79
+ projectName: './',
80
+ sourceRoot: __dirname,
81
+ template: 'default',
82
+ templateSource: 'default-template',
83
+ npm: 'npm',
84
+ css: 'none',
85
+ framework: 'react'
86
+ } as any)
87
+
88
+ expect(project.conf.projectDir).toBe('/a/b')
89
+ expect(project.conf.projectName).toBe('current-project')
90
+ })
91
+
92
+ it('should resolve windows-style ".\\" project name to current directory', () => {
93
+ const project = new Project({
94
+ projectDir: '/a/b/current-project',
95
+ projectName: '.\\',
96
+ sourceRoot: __dirname,
97
+ template: 'default',
98
+ templateSource: 'default-template',
99
+ npm: 'npm',
100
+ css: 'none',
101
+ framework: 'react'
102
+ } as any)
103
+
104
+ expect(project.conf.projectDir).toBe('/a/b')
105
+ expect(project.conf.projectName).toBe('current-project')
106
+ expect(project.conf.initInCurrentDir).toBe(true)
107
+ })
108
+
109
+ it('should fall back to process.cwd() when projectDir is empty', () => {
110
+ const cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue('/x/y/z')
111
+ try {
112
+ const project = new Project({
113
+ projectName: '.',
114
+ sourceRoot: __dirname,
115
+ template: 'default',
116
+ templateSource: 'default-template',
117
+ npm: 'npm',
118
+ css: 'none',
119
+ framework: 'react'
120
+ } as any)
121
+
122
+ expect(project.conf.projectDir).toBe('/x/y')
123
+ expect(project.conf.projectName).toBe('z')
124
+ expect(project.conf.initInCurrentDir).toBe(true)
125
+ } finally {
126
+ cwdSpy.mockRestore()
127
+ }
128
+ })
129
+
130
+ it.each(['..', '../', '.foo', 'foo', 'my-app'])(
131
+ 'should keep project name %p unchanged',
132
+ (projectName) => {
133
+ const project = new Project({
134
+ projectDir: '/a/b',
135
+ projectName,
136
+ sourceRoot: __dirname,
137
+ template: 'default',
138
+ templateSource: 'default-template',
139
+ npm: 'npm',
140
+ css: 'none',
141
+ framework: 'react'
142
+ } as any)
143
+
144
+ expect(project.conf.projectDir).toBe('/a/b')
145
+ expect(project.conf.projectName).toBe(projectName)
146
+ expect(project.conf.initInCurrentDir).toBeUndefined()
147
+ }
148
+ )
149
+
150
+ it('should throw when initializing at the filesystem root', () => {
151
+ expect(() => new Project({
152
+ projectDir: '/',
153
+ projectName: '.',
154
+ sourceRoot: __dirname,
155
+ template: 'default',
156
+ templateSource: 'default-template',
157
+ npm: 'npm',
158
+ css: 'none',
159
+ framework: 'react'
160
+ } as any)).toThrow('无法在文件系统根目录下初始化项目')
161
+ })
162
+
163
+ it('should skip existing-name prompt when initializing in current directory', () => {
164
+ // fs.existsSync 被 mock 为恒 true,模拟目标目录(当前目录)已存在。
165
+ // `.` 场景下不应因此推入“同名目录已存在”的询问。
166
+ const project = new Project({
167
+ projectDir: '/a/b/current-project',
168
+ projectName: '.',
169
+ sourceRoot: __dirname,
170
+ template: 'default',
171
+ templateSource: 'default-template',
172
+ npm: 'npm',
173
+ css: 'none',
174
+ framework: 'react'
175
+ } as any)
176
+
177
+ expect(project.conf.initInCurrentDir).toBe(true)
178
+
179
+ const prompts: Record<string, unknown>[] = []
180
+ project.askProjectName(project.conf, prompts)
181
+
182
+ expect(prompts).toHaveLength(0)
183
+ })
184
+
185
+ it('should prompt for a new name when a same-name directory exists (normal init)', () => {
186
+ // 普通命名 + 目标位置已存在同名目录时,仍应提示换名。
187
+ const project = new Project({
188
+ projectDir: '/a/b',
189
+ projectName: 'existing-project',
190
+ sourceRoot: __dirname,
191
+ template: 'default',
192
+ templateSource: 'default-template',
193
+ npm: 'npm',
194
+ css: 'none',
195
+ framework: 'react'
196
+ } as any)
197
+
198
+ expect(project.conf.initInCurrentDir).toBeUndefined()
199
+
200
+ const prompts: Record<string, unknown>[] = []
201
+ project.askProjectName(project.conf, prompts)
202
+
203
+ expect(prompts).toHaveLength(1)
204
+ expect(prompts[0]).toMatchObject({ name: 'projectName' })
205
+ })
206
+
207
+ it('should prompt for platforms when conf.platforms is undefined', () => {
208
+ const project = new Project({
209
+ projectDir: '/a/b',
210
+ projectName: 'my-project',
211
+ sourceRoot: __dirname,
212
+ template: 'default',
213
+ templateSource: 'default-template',
214
+ npm: 'npm',
215
+ css: 'none',
216
+ framework: 'react'
217
+ } as any)
218
+
219
+ const prompts: Record<string, unknown>[] = []
220
+ project.askPlatforms(project.conf, prompts)
221
+
222
+ expect(prompts).toHaveLength(1)
223
+ expect(prompts[0]).toMatchObject({ type: 'checkbox', name: 'platforms' })
224
+ })
225
+
226
+ it('should not prompt for platforms when conf.platforms is already an array', () => {
227
+ const project = new Project({
228
+ projectDir: '/a/b',
229
+ projectName: 'my-project',
230
+ sourceRoot: __dirname,
231
+ template: 'default',
232
+ templateSource: 'default-template',
233
+ npm: 'npm',
234
+ css: 'none',
235
+ framework: 'react',
236
+ platforms: ['weapp', 'h5']
237
+ } as any)
238
+
239
+ const prompts: Record<string, unknown>[] = []
240
+ project.askPlatforms(project.conf, prompts)
241
+
242
+ expect(prompts).toHaveLength(0)
243
+ expect(project.conf.platforms).toEqual(['weapp', 'h5'])
244
+ })
245
+
246
+ it('should normalize a comma-separated platforms string into an array', () => {
247
+ const project = new Project({
248
+ projectDir: '/a/b',
249
+ projectName: 'my-project',
250
+ sourceRoot: __dirname,
251
+ template: 'default',
252
+ templateSource: 'default-template',
253
+ npm: 'npm',
254
+ css: 'none',
255
+ framework: 'react',
256
+ platforms: 'weapp, h5'
257
+ } as any)
258
+
259
+ const prompts: Record<string, unknown>[] = []
260
+ project.askPlatforms(project.conf, prompts)
261
+
262
+ expect(project.conf.platforms).toEqual(['weapp', 'h5'])
263
+ expect(prompts).toHaveLength(0)
264
+ })
265
+
266
+ it.each([
267
+ [[], '未选择平台'],
268
+ [['unknown'], 'unknown']
269
+ ])('should reject invalid platforms %p', (platforms, message) => {
270
+ const project = new Project({
271
+ projectDir: '/a/b',
272
+ projectName: 'my-project',
273
+ sourceRoot: __dirname,
274
+ template: 'default',
275
+ templateSource: 'default-template',
276
+ npm: 'npm',
277
+ css: 'none',
278
+ framework: 'react',
279
+ platforms
280
+ } as any)
281
+
282
+ expect(() => project.askPlatforms(project.conf, [])).toThrow(message)
283
+ })
284
+
285
+ it('should require at least one platform when prompting', () => {
286
+ const project = new Project({
287
+ projectDir: '/a/b',
288
+ projectName: 'my-project',
289
+ sourceRoot: __dirname,
290
+ template: 'default',
291
+ templateSource: 'default-template',
292
+ npm: 'npm',
293
+ css: 'none',
294
+ framework: 'react'
295
+ } as any)
296
+
297
+ const prompts: Record<string, unknown>[] = []
298
+ project.askPlatforms(project.conf, prompts)
299
+ const validate = prompts[0].validate as (input: string[]) => true | string
300
+
301
+ expect(validate([])).toBe('请至少选择一个平台!')
302
+ expect(validate(['weapp'])).toBe(true)
303
+ })
304
+ })