@spcsn/taro-cli 0.1.0

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 (213) hide show
  1. package/LICENSE +174 -0
  2. package/README.md +0 -0
  3. package/bin/taro +7 -0
  4. package/dist/cli.d.ts +6 -0
  5. package/dist/cli.js +266 -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 +64 -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 +169 -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 +266 -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 +77 -0
  30. package/dist/create/plugin.js.map +1 -0
  31. package/dist/create/project.d.ts +70 -0
  32. package/dist/create/project.js +481 -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 +23 -0
  36. package/dist/doctor/index.js.map +1 -0
  37. package/dist/index.d.ts +16 -0
  38. package/dist/index.js +24 -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 +236 -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 +121 -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 +112 -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 +175 -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 +16 -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 +96 -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 +46 -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 +155 -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 +187 -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 +72 -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 +50 -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 +55 -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 +42 -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 +50 -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 +106 -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 +130 -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 +27 -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 +114 -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 +11 -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 +149 -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 +74 -0
  115. package/postinstall.js +17 -0
  116. package/src/__tests__/__mocks__/presets.ts +15 -0
  117. package/src/__tests__/build-config.spec.ts +66 -0
  118. package/src/__tests__/cli.spec.ts +226 -0
  119. package/src/__tests__/config.spec.ts +224 -0
  120. package/src/__tests__/doctor-config.spec.ts +943 -0
  121. package/src/__tests__/doctor-recommand.spec.ts +136 -0
  122. package/src/__tests__/doctor.spec.ts +94 -0
  123. package/src/__tests__/dotenv-parse.spec.ts +105 -0
  124. package/src/__tests__/env/.env +2 -0
  125. package/src/__tests__/fixtures/default/.env +3 -0
  126. package/src/__tests__/fixtures/default/.env.development +1 -0
  127. package/src/__tests__/fixtures/default/.env.local +1 -0
  128. package/src/__tests__/fixtures/default/.env.pre +3 -0
  129. package/src/__tests__/fixtures/default/.env.production +1 -0
  130. package/src/__tests__/fixtures/default/.env.uat +5 -0
  131. package/src/__tests__/fixtures/default/.env.uat.local +1 -0
  132. package/src/__tests__/fixtures/default/babel.config.js +10 -0
  133. package/src/__tests__/fixtures/default/config/dev.js +9 -0
  134. package/src/__tests__/fixtures/default/config/index.js +84 -0
  135. package/src/__tests__/fixtures/default/config/prod.js +18 -0
  136. package/src/__tests__/fixtures/default/package.json +27 -0
  137. package/src/__tests__/fixtures/default/src/app.config.js +11 -0
  138. package/src/__tests__/fixtures/default/src/app.js +16 -0
  139. package/src/__tests__/fixtures/default/src/app.scss +0 -0
  140. package/src/__tests__/fixtures/default/src/index.html +19 -0
  141. package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
  142. package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
  143. package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
  144. package/src/__tests__/info.spec.ts +72 -0
  145. package/src/__tests__/inspect.spec.ts +160 -0
  146. package/src/__tests__/update.spec.ts +310 -0
  147. package/src/__tests__/utils/index.ts +54 -0
  148. package/src/cli.ts +240 -0
  149. package/src/commands/customCommand.ts +22 -0
  150. package/src/config/index.ts +7 -0
  151. package/src/config/manifest.default.json +33 -0
  152. package/src/config/packagesManagement.ts +20 -0
  153. package/src/config/rn-stylelint.json +4 -0
  154. package/src/config/tsconfig.json +22 -0
  155. package/src/create/constants.ts +1 -0
  156. package/src/create/creator.ts +37 -0
  157. package/src/create/fetchTemplate.ts +161 -0
  158. package/src/create/page.ts +325 -0
  159. package/src/create/plugin.ts +55 -0
  160. package/src/create/project.ts +539 -0
  161. package/src/doctor/index.ts +21 -0
  162. package/src/index.ts +24 -0
  163. package/src/presets/commands/build.ts +215 -0
  164. package/src/presets/commands/config.ts +86 -0
  165. package/src/presets/commands/create.ts +104 -0
  166. package/src/presets/commands/global-config.ts +140 -0
  167. package/src/presets/commands/help.ts +17 -0
  168. package/src/presets/commands/info.ts +81 -0
  169. package/src/presets/commands/init.ts +63 -0
  170. package/src/presets/commands/inspect.ts +137 -0
  171. package/src/presets/commands/update.ts +175 -0
  172. package/src/presets/constant/hooks.ts +14 -0
  173. package/src/presets/constant/index.ts +1 -0
  174. package/src/presets/files/generateFrameworkInfo.ts +30 -0
  175. package/src/presets/files/generateProjectConfig.ts +41 -0
  176. package/src/presets/files/writeFileToDist.ts +17 -0
  177. package/src/presets/hooks/build.ts +22 -0
  178. package/src/presets/hooks/create.ts +9 -0
  179. package/src/presets/index.ts +15 -0
  180. package/src/presets/platforms/plugin.ts +75 -0
  181. package/src/presets/platforms/rn.ts +125 -0
  182. package/src/util/appConfig.ts +32 -0
  183. package/src/util/createPage.ts +139 -0
  184. package/src/util/defineConfig.ts +28 -0
  185. package/src/util/index.ts +126 -0
  186. package/src/util/types.ts +81 -0
  187. package/templates/default/_editorconfig +12 -0
  188. package/templates/default/_env.development +2 -0
  189. package/templates/default/_env.production +1 -0
  190. package/templates/default/_env.test +1 -0
  191. package/templates/default/_gitignore +8 -0
  192. package/templates/default/babel.config.js +14 -0
  193. package/templates/default/config/dev.js +5 -0
  194. package/templates/default/config/index.js +61 -0
  195. package/templates/default/config/prod.js +5 -0
  196. package/templates/default/package.json.tmpl +52 -0
  197. package/templates/default/project.config.json +15 -0
  198. package/templates/default/src/app.config.js +11 -0
  199. package/templates/default/src/app.css +0 -0
  200. package/templates/default/src/app.js +15 -0
  201. package/templates/default/src/pages/index/index.config.js +3 -0
  202. package/templates/default/src/pages/index/index.css +0 -0
  203. package/templates/default/src/pages/index/index.jsx +15 -0
  204. package/templates/default/template_creator.js +61 -0
  205. package/templates/default/tsconfig.json +30 -0
  206. package/templates/default/types/global.d.ts +27 -0
  207. package/templates/global-config/index.json +4 -0
  208. package/templates/global-config/package.json +5 -0
  209. package/templates/plugin-compile/README.md +32 -0
  210. package/templates/plugin-compile/index.js +3 -0
  211. package/templates/plugin-compile/package.json.tmpl +43 -0
  212. package/templates/plugin-compile/src/index.ts +153 -0
  213. package/templates/plugin-compile/tsconfig.json +28 -0
@@ -0,0 +1,160 @@
1
+ import * as path from 'node:path';
2
+
3
+ import { chalk, fs } from '@spcsn/taro-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('@spcsn/taro-helper', () => {
17
+ const helper = jest.requireActual('@spcsn/taro-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('@spcsn/taro-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: 'weapp',
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("'browser'");
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(outputPath, "'browser'");
157
+
158
+ exitSpy.mockRestore();
159
+ });
160
+ });
@@ -0,0 +1,310 @@
1
+ import * as path from 'node:path';
2
+
3
+ import { chalk, fs, PROJECT_CONFIG, shouldUseCnpm, shouldUseYarn } from '@spcsn/taro-helper';
4
+ import { exec } from 'child_process';
5
+
6
+ import { getPkgVersion } from '../util';
7
+ import { run } from './utils';
8
+
9
+ const runUpdate = run('update', ['commands/update']);
10
+ const lastestVersion = getPkgVersion();
11
+
12
+ jest.mock('child_process', () => {
13
+ const exec = jest.fn();
14
+ exec.mockReturnValue({
15
+ stdout: {
16
+ on() {},
17
+ },
18
+ stderr: {
19
+ on() {},
20
+ },
21
+ });
22
+ return {
23
+ __esModule: true,
24
+ exec,
25
+ };
26
+ });
27
+
28
+ jest.mock('ora', () => {
29
+ const ora = jest.fn();
30
+ ora.mockReturnValue({
31
+ start() {
32
+ return {
33
+ stop() {},
34
+ warn() {},
35
+ succeed() {},
36
+ };
37
+ },
38
+ });
39
+ return ora;
40
+ });
41
+
42
+ jest.mock('@spcsn/taro-helper', () => {
43
+ const helper = jest.requireActual('@spcsn/taro-helper');
44
+ const fs = helper.fs;
45
+ return {
46
+ __esModule: true,
47
+ ...helper,
48
+ shouldUseCnpm: jest.fn(),
49
+ shouldUseYarn: jest.fn(),
50
+ chalk: {
51
+ red: jest.fn(),
52
+ green() {},
53
+ },
54
+ fs: {
55
+ ...fs,
56
+ writeJson: jest.fn(),
57
+ },
58
+ };
59
+ });
60
+
61
+ jest.mock('latest-version', () => () => lastestVersion);
62
+
63
+ function updatePkg(pkgPath: string, version: string) {
64
+ let packageMap = require(pkgPath);
65
+ packageMap = {
66
+ ...packageMap,
67
+ dependencies: {
68
+ ...packageMap.dependencies,
69
+ '@spcsn/taro-shared': version,
70
+ '@spcsn/taro': version,
71
+ '@spcsn/taro-cli': version,
72
+ '@spcsn/taro-components': version,
73
+ '@spcsn/taro-api': version,
74
+ '@spcsn/taro-helper': version,
75
+ '@spcsn/taro-react': version,
76
+ '@spcsn/taro-runner-utils': version,
77
+ '@spcsn/taro-runtime': version,
78
+ '@spcsn/taro-service': version,
79
+ '@spcsn/taro-plugin-platform-weapp': version,
80
+ '@spcsn/taro-plugin-framework-react': version,
81
+ '@spcsn/taro-plugin-generator': version,
82
+ '@spcsn/taro-vite-runner': version,
83
+ '@spcsn/taro-binding': version,
84
+ },
85
+ devDependencies: {
86
+ ...packageMap.devDependencies,
87
+ 'babel-preset-taro': version,
88
+ 'babel-plugin-transform-taroapi': version,
89
+ 'postcss-plugin-constparse': version,
90
+ 'postcss-pxtransform': version,
91
+ },
92
+ };
93
+ return packageMap;
94
+ }
95
+
96
+ describe('update', () => {
97
+ const execMocked = exec as unknown as jest.Mock<any>;
98
+ const shouldUseCnpmMocked = shouldUseCnpm as jest.Mock<any>;
99
+ const shouldUseYarnMocked = shouldUseYarn as jest.Mock<any>;
100
+ const writeJson = fs.writeJson as jest.Mock<any>;
101
+
102
+ beforeEach(() => {
103
+ shouldUseCnpmMocked.mockReturnValue(false);
104
+ shouldUseYarnMocked.mockReturnValue(false);
105
+ });
106
+
107
+ afterEach(() => {
108
+ execMocked.mockClear();
109
+ shouldUseCnpmMocked.mockReset();
110
+ shouldUseYarnMocked.mockReset();
111
+ writeJson.mockClear();
112
+ });
113
+
114
+ it('should log errors', async () => {
115
+ const spy = jest.spyOn(console, 'log');
116
+ spy.mockImplementation(() => {});
117
+ await runUpdate('', {
118
+ options: {
119
+ npm: 'npm',
120
+ disableGlobalConfig: true,
121
+ },
122
+ });
123
+ expect(spy).toBeCalledTimes(3);
124
+ spy.mockRestore();
125
+ });
126
+
127
+ it('should update self', async () => {
128
+ await runUpdate('', {
129
+ args: ['self'],
130
+ options: {
131
+ npm: 'npm',
132
+ disableGlobalConfig: true,
133
+ },
134
+ });
135
+ expect(execMocked).toBeCalledWith(`npm i -g @spcsn/taro-cli@${lastestVersion}`);
136
+ });
137
+
138
+ it('should update self using yarn', async () => {
139
+ shouldUseCnpmMocked.mockReturnValue(true);
140
+ await runUpdate('', {
141
+ args: ['self'],
142
+ options: {
143
+ npm: 'yarn',
144
+ disableGlobalConfig: true,
145
+ },
146
+ });
147
+ expect(execMocked).toBeCalledWith(`yarn global add @spcsn/taro-cli@${lastestVersion}`);
148
+ });
149
+
150
+ it('should update self using pnpm', async () => {
151
+ shouldUseCnpmMocked.mockReturnValue(true);
152
+ await runUpdate('', {
153
+ args: ['self'],
154
+ options: {
155
+ npm: 'pnpm',
156
+ disableGlobalConfig: true,
157
+ },
158
+ });
159
+ expect(execMocked).toBeCalledWith(`pnpm add -g @spcsn/taro-cli@${lastestVersion}`);
160
+ });
161
+
162
+ it('should update self using cnpm', async () => {
163
+ shouldUseCnpmMocked.mockReturnValue(true);
164
+ await runUpdate('', {
165
+ args: ['self'],
166
+ options: {
167
+ npm: 'cnpm',
168
+ disableGlobalConfig: true,
169
+ },
170
+ });
171
+ expect(execMocked).toBeCalledWith(`cnpm i -g @spcsn/taro-cli@${lastestVersion}`);
172
+ });
173
+
174
+ it('should update self to specific version', async () => {
175
+ const version = '3.0.0-beta.0';
176
+ await runUpdate('', {
177
+ args: ['self', version],
178
+ options: {
179
+ npm: 'npm',
180
+ disableGlobalConfig: true,
181
+ },
182
+ });
183
+ expect(execMocked).toBeCalledWith(`npm i -g @spcsn/taro-cli@${version}`);
184
+ });
185
+
186
+ it("should throw when there isn't a Taro project", async () => {
187
+ const chalkMocked = chalk.red as unknown as jest.Mock<any>;
188
+ const exitSpy = jest.spyOn(process, 'exit');
189
+ const logSpy = jest.spyOn(console, 'log');
190
+ exitSpy.mockImplementation(() => {
191
+ throw new Error();
192
+ });
193
+ logSpy.mockImplementation(() => {});
194
+ try {
195
+ await runUpdate('', {
196
+ args: ['project'],
197
+ options: {
198
+ npm: 'npm',
199
+ disableGlobalConfig: true,
200
+ },
201
+ });
202
+ } catch (error) {} // eslint-disable-line no-empty
203
+ expect(exitSpy).toBeCalledWith(1);
204
+ expect(chalkMocked).toBeCalledWith(`找不到项目配置文件 ${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`);
205
+ exitSpy.mockRestore();
206
+ logSpy.mockRestore();
207
+ });
208
+
209
+ it('should update project', async () => {
210
+ const appPath = path.resolve(__dirname, 'fixtures/default');
211
+ const pkgPath = path.join(appPath, 'package.json');
212
+ const packageMap = updatePkg(pkgPath, lastestVersion);
213
+
214
+ const logSpy = jest.spyOn(console, 'log');
215
+ logSpy.mockImplementation(() => {});
216
+
217
+ await runUpdate(appPath, {
218
+ args: ['project'],
219
+ options: {
220
+ npm: 'npm',
221
+ disableGlobalConfig: true,
222
+ },
223
+ });
224
+ expect(writeJson.mock.calls[0][0]).toEqual(pkgPath);
225
+ expect(writeJson.mock.calls[0][1]).toEqual(packageMap);
226
+ expect(execMocked).toBeCalledWith('npm install');
227
+
228
+ logSpy.mockRestore();
229
+ });
230
+
231
+ it('should update project to specific version', async () => {
232
+ const version = '3.0.0-beta.4';
233
+ const appPath = path.resolve(__dirname, 'fixtures/default');
234
+ const pkgPath = path.join(appPath, 'package.json');
235
+ const packageMap = updatePkg(pkgPath, version);
236
+
237
+ const logSpy = jest.spyOn(console, 'log');
238
+ logSpy.mockImplementation(() => {});
239
+
240
+ await runUpdate(appPath, {
241
+ args: ['project', version],
242
+ options: {
243
+ npm: 'npm',
244
+ disableGlobalConfig: true,
245
+ },
246
+ });
247
+ expect(writeJson.mock.calls[0][0]).toEqual(pkgPath);
248
+ expect(writeJson.mock.calls[0][1]).toEqual(packageMap);
249
+ expect(execMocked).toBeCalledWith('npm install');
250
+
251
+ logSpy.mockRestore();
252
+ });
253
+
254
+ it('should update project with yarn', async () => {
255
+ const appPath = path.resolve(__dirname, 'fixtures/default');
256
+
257
+ const logSpy = jest.spyOn(console, 'log');
258
+ logSpy.mockImplementation(() => {});
259
+ shouldUseYarnMocked.mockReturnValue(true);
260
+
261
+ await runUpdate(appPath, {
262
+ args: ['project'],
263
+ options: {
264
+ npm: 'yarn',
265
+ disableGlobalConfig: true,
266
+ },
267
+ });
268
+ expect(execMocked).toBeCalledWith('yarn install');
269
+
270
+ logSpy.mockRestore();
271
+ });
272
+
273
+ it('should update project with pnpm', async () => {
274
+ const appPath = path.resolve(__dirname, 'fixtures/default');
275
+
276
+ const logSpy = jest.spyOn(console, 'log');
277
+ logSpy.mockImplementation(() => {});
278
+ shouldUseCnpmMocked.mockReturnValue(true);
279
+
280
+ await runUpdate(appPath, {
281
+ args: ['project'],
282
+ options: {
283
+ npm: 'pnpm',
284
+ disableGlobalConfig: true,
285
+ },
286
+ });
287
+ expect(execMocked).toBeCalledWith('pnpm install');
288
+
289
+ logSpy.mockRestore();
290
+ });
291
+
292
+ it('should update project with cnpm', async () => {
293
+ const appPath = path.resolve(__dirname, 'fixtures/default');
294
+
295
+ const logSpy = jest.spyOn(console, 'log');
296
+ logSpy.mockImplementation(() => {});
297
+ shouldUseCnpmMocked.mockReturnValue(true);
298
+
299
+ await runUpdate(appPath, {
300
+ args: ['project'],
301
+ options: {
302
+ npm: 'cnpm',
303
+ disableGlobalConfig: true,
304
+ },
305
+ });
306
+ expect(execMocked).toBeCalledWith('cnpm install');
307
+
308
+ logSpy.mockRestore();
309
+ });
310
+ });
@@ -0,0 +1,54 @@
1
+ import * as path from 'node:path';
2
+
3
+ import { Config, Kernel } from '@spcsn/taro-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(`@spcsn/taro-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
+ }