@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,136 @@
1
+ import * as path from 'node:path';
2
+
3
+ import { chalk, fs } from '@spcsn/taro-helper';
4
+
5
+ import doctor from '../doctor';
6
+
7
+ const validator = doctor.validators[3];
8
+
9
+ jest.mock('@spcsn/taro-helper', () => {
10
+ const helper = jest.requireActual('@spcsn/taro-helper');
11
+ const fs = helper.fs;
12
+ return {
13
+ __esModule: true,
14
+ ...helper,
15
+ fs: {
16
+ ...fs,
17
+ readdirSync: jest.fn(),
18
+ existsSync: jest.fn(),
19
+ },
20
+ };
21
+ });
22
+
23
+ describe('recommand validator of doctor', () => {
24
+ const existsSyncMocked = fs.existsSync as jest.Mock<any>;
25
+ const readdirSyncMocked = fs.readdirSync as jest.Mock<any>;
26
+
27
+ beforeEach(() => {
28
+ jest.resetModules();
29
+ existsSyncMocked.mockReset();
30
+ readdirSyncMocked.mockReset();
31
+ existsSyncMocked.mockReturnValue(true);
32
+ });
33
+
34
+ it.skip("should exit because there isn't a Taro project", async () => {
35
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>;
36
+ const logSpy = jest.spyOn(console, 'log');
37
+
38
+ exitSpy.mockImplementation(() => {
39
+ throw new Error();
40
+ });
41
+ logSpy.mockImplementation(() => {});
42
+ existsSyncMocked.mockReturnValue(false);
43
+
44
+ const MOCK_APP_PATH = 'src/';
45
+ try {
46
+ await validator({ appPath: MOCK_APP_PATH });
47
+ } catch (error) {} // eslint-disable-line no-empty
48
+
49
+ expect(exitSpy).toBeCalledWith(1);
50
+ const PROJECT_PACKAGE_PATH = path.join(MOCK_APP_PATH, 'package.json');
51
+ expect(logSpy).toBeCalledWith(chalk.red(`找不到${PROJECT_PACKAGE_PATH},请确定当前目录是Taro项目根目录!`));
52
+
53
+ exitSpy.mockRestore();
54
+ logSpy.mockRestore();
55
+ });
56
+
57
+ it.skip('should warn when test framework not found', async () => {
58
+ jest.doMock('./fixtures/default/package.json', () => ({
59
+ devDependencies: {
60
+ eslint: 1,
61
+ },
62
+ }));
63
+ readdirSyncMocked.mockReturnValue(['readme', '.gitignore', '.editorconfig']);
64
+
65
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') });
66
+
67
+ expect(lines.length).toBe(1);
68
+ expect(lines[0].desc).toBe(
69
+ '没有检查到常见的测试依赖(jest/mocha/ava/tape/jesmine/karma), 配置测试可以帮助提升项目质量',
70
+ );
71
+ expect(lines[0].valid).toBe(true);
72
+ expect(lines[0].solution).toBe(
73
+ '可以参考 https://github.com/NervJS/taro-ui-sample 项目, 其中已经包含了完整的测试配置与范例',
74
+ );
75
+
76
+ jest.dontMock('./fixtures/default/package.json');
77
+ });
78
+
79
+ it.skip('should warn when Readme not found', async () => {
80
+ jest.doMock('./fixtures/default/package.json', () => ({
81
+ devDependencies: {
82
+ mocha: 1,
83
+ jslint: 1,
84
+ },
85
+ }));
86
+ readdirSyncMocked.mockReturnValue(['.gitignore', '.editorconfig']);
87
+
88
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') });
89
+
90
+ expect(lines.length).toBe(1);
91
+ expect(lines[0].desc).toBe(
92
+ '没有检查到 Readme (readme/readme.md/readme.markdown), 编写 Readme 可以方便其他人了解项目',
93
+ );
94
+ expect(lines[0].valid).toBe(true);
95
+
96
+ jest.dontMock('./fixtures/default/package.json');
97
+ });
98
+
99
+ it.skip('should warn when .gitignore not found', async () => {
100
+ jest.doMock('./fixtures/default/package.json', () => ({
101
+ devDependencies: {
102
+ jesmine: 1,
103
+ tslint: 1,
104
+ },
105
+ }));
106
+ readdirSyncMocked.mockReturnValue(['readme.markdown', '.editorconfig']);
107
+
108
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') });
109
+
110
+ expect(lines.length).toBe(1);
111
+ expect(lines[0].desc).toBe(
112
+ '没有检查到 .gitignore 配置, 配置 .gitignore 以避免将敏感信息或不必要的内容提交到代码仓库',
113
+ );
114
+ expect(lines[0].valid).toBe(true);
115
+
116
+ jest.dontMock('./fixtures/default/package.json');
117
+ });
118
+
119
+ it.skip('should warn when .editorconfig not found', async () => {
120
+ jest.doMock('./fixtures/default/package.json', () => ({
121
+ devDependencies: {
122
+ karma: 1,
123
+ jshint: 1,
124
+ },
125
+ }));
126
+ readdirSyncMocked.mockReturnValue(['readme', '.gitignore']);
127
+
128
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') });
129
+
130
+ expect(lines.length).toBe(1);
131
+ expect(lines[0].desc).toBe('没有检查到 .editconfig 配置, 配置 editconfig 以统一项目成员编辑器的代码风格');
132
+ expect(lines[0].valid).toBe(true);
133
+
134
+ jest.dontMock('./fixtures/default/package.json');
135
+ });
136
+ });
@@ -0,0 +1,94 @@
1
+ import { chalk } from '@spcsn/taro-helper';
2
+
3
+ import { run } from './utils';
4
+
5
+ jest.mock('../doctor', () => {
6
+ return {
7
+ __esModule: true,
8
+ default: {
9
+ validators: [
10
+ () => ({
11
+ isValid: true,
12
+ messgaes: [
13
+ {
14
+ kind: 2,
15
+ content: 'Env Success',
16
+ },
17
+ ],
18
+ }),
19
+ () =>
20
+ Promise.resolve({
21
+ isValid: false,
22
+ messgaes: [
23
+ {
24
+ kind: 1,
25
+ content: 'Config Error',
26
+ },
27
+ ],
28
+ }),
29
+ () => ({
30
+ isValid: true,
31
+ messgaes: [
32
+ {
33
+ kind: 2,
34
+ content: 'Package Success',
35
+ },
36
+ ],
37
+ }),
38
+ () => ({
39
+ isValid: true,
40
+ messgaes: [
41
+ {
42
+ kind: 2,
43
+ content: 'recommend Success',
44
+ },
45
+ ],
46
+ }),
47
+ async () => ({
48
+ isValid: true,
49
+ messgaes: [
50
+ {
51
+ kind: 2,
52
+ content: 'doctor Success',
53
+ },
54
+ ],
55
+ }),
56
+ ],
57
+ },
58
+ };
59
+ });
60
+
61
+ jest.mock('ora', () => {
62
+ const ora = jest.fn();
63
+ ora.mockReturnValue({
64
+ start() {
65
+ return {
66
+ succeed() {},
67
+ };
68
+ },
69
+ });
70
+ return ora;
71
+ });
72
+
73
+ const runDoctor = run('doctor', [require.resolve('@tarojs/plugin-doctor')]);
74
+
75
+ describe('doctor', () => {
76
+ it("should exit because there isn't a Taro project", async () => {
77
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>;
78
+ const logSpy = jest.spyOn(console, 'log');
79
+
80
+ exitSpy.mockImplementation(() => {
81
+ throw new Error();
82
+ });
83
+ logSpy.mockImplementation(() => {});
84
+ try {
85
+ await runDoctor('', { options: { disableGlobalConfig: true } });
86
+ } catch (error) {} // eslint-disable-line no-empty
87
+
88
+ expect(exitSpy).toBeCalledWith(1);
89
+ expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'));
90
+
91
+ exitSpy.mockRestore();
92
+ logSpy.mockRestore();
93
+ });
94
+ });
@@ -0,0 +1,105 @@
1
+ import * as path from 'node:path';
2
+
3
+ import { dotenvParse } from '@spcsn/taro-helper';
4
+ import { Kernel } from '@spcsn/taro-service';
5
+
6
+ import CLI from '../cli';
7
+
8
+ jest.mock('@spcsn/taro-service');
9
+ const MockedKernel = Kernel as unknown as jest.Mock<Kernel>;
10
+ const APP_PATH = path.join(__dirname, 'fixtures/default');
11
+
12
+ function setProcessArgv(cmd: string) {
13
+ // @ts-ignore
14
+ process.argv = [null, ...cmd.split(' ')];
15
+ }
16
+
17
+ describe('inspect', () => {
18
+ let cli: CLI;
19
+
20
+ beforeAll(() => {
21
+ cli = new CLI(APP_PATH);
22
+ });
23
+
24
+ beforeEach(() => {
25
+ MockedKernel.mockClear();
26
+ process.argv = [];
27
+ delete process.env.NODE_ENV;
28
+ delete process.env.TARO_ENV;
29
+ delete process.env.TARO_APP_TEST;
30
+ delete process.env.TARO_APP_ID;
31
+ delete process.env.JD_APP_TEST;
32
+ delete process.env.TARO_APP_DEFAULT;
33
+ delete process.env.TARO_APP_FOO;
34
+ });
35
+
36
+ afterEach(() => {
37
+ MockedKernel.mockClear();
38
+ process.argv = [];
39
+ delete process.env.NODE_ENV;
40
+ delete process.env.TARO_ENV;
41
+ delete process.env.TARO_APP_TEST;
42
+ delete process.env.TARO_APP_ID;
43
+ delete process.env.JD_APP_TEST;
44
+ delete process.env.TARO_APP_DEFAULT;
45
+ delete process.env.TARO_APP_FOO;
46
+ });
47
+
48
+ describe('cli mode env', () => {
49
+ it('dotenvParse .env .env.dev should success', async () => {
50
+ expect(process.env.TARO_test).toBeUndefined();
51
+ dotenvParse(path.resolve(__dirname, 'env'), 'TARO_', 'dev');
52
+ expect(process.env.TARO_test).toBe('123');
53
+ expect(process.env._TARO_test).toBeUndefined();
54
+ });
55
+
56
+ it('--watch true => 默认加载 .env.development', async () => {
57
+ setProcessArgv('taro build --watch --type weapp');
58
+ await cli.run();
59
+ expect(process.env.TARO_APP_TEST).toEqual('env-development');
60
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default');
61
+ });
62
+
63
+ it('--watch false => 默认加载 .env.production', async () => {
64
+ setProcessArgv('taro build --type weapp');
65
+ await cli.run();
66
+ expect(process.env.TARO_APP_TEST).toEqual('env-production');
67
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default');
68
+ });
69
+
70
+ it('指定加载 .env.pre', async () => {
71
+ setProcessArgv('taro build --type weapp --mode pre');
72
+ await cli.run();
73
+ expect(process.env.TARO_APP_TEST).toEqual('env-pre');
74
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default');
75
+ });
76
+
77
+ it('env.local 比 .env 优先级更高', async () => {
78
+ setProcessArgv('taro build --type weapp --mode find404');
79
+ await cli.run();
80
+ expect(process.env.TARO_APP_TEST).toEqual('env-local');
81
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default');
82
+ });
83
+
84
+ it('env.uat.local 比 .env.uat 优先级更高', async () => {
85
+ setProcessArgv('taro build --type weapp --mode uat');
86
+ await cli.run();
87
+ expect(process.env.TARO_APP_TEST).toEqual('env-uat-local');
88
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default');
89
+ });
90
+
91
+ it('自定义前缀: JD_APP_', async () => {
92
+ setProcessArgv('taro build --type weapp --mode uat --env-prefix JD_APP_');
93
+ await cli.run();
94
+ expect(process.env.JD_APP_TEST).toEqual('env-uat');
95
+ expect(process.env.TARO_APP_TEST).toEqual(undefined);
96
+ expect(process.env.TARO_APP_ID).toEqual('特殊变量appid');
97
+ });
98
+
99
+ it('环境变量可以相互引用', async () => {
100
+ setProcessArgv('taro build --type weapp --mode pre');
101
+ await cli.run();
102
+ expect(process.env.TARO_APP_FOO).toEqual('env-pre-foo');
103
+ });
104
+ });
105
+ });
@@ -0,0 +1,2 @@
1
+ TARO_test = 123
2
+ _TARO_test = 456
@@ -0,0 +1,3 @@
1
+ TARO_APP_TEST=env
2
+
3
+ TARO_APP_DEFAULT=default
@@ -0,0 +1 @@
1
+ TARO_APP_TEST=env-development
@@ -0,0 +1 @@
1
+ TARO_APP_TEST=env-local
@@ -0,0 +1,3 @@
1
+ TARO_APP_TEST=env-pre
2
+
3
+ TARO_APP_FOO=${TARO_APP_TEST}-foo
@@ -0,0 +1 @@
1
+ TARO_APP_TEST=env-production
@@ -0,0 +1,5 @@
1
+ TARO_APP_TEST=env-uat
2
+
3
+ JD_APP_TEST=env-uat
4
+
5
+ TARO_APP_ID=特殊变量appid
@@ -0,0 +1 @@
1
+ TARO_APP_TEST=env-uat-local
@@ -0,0 +1,10 @@
1
+ // babel-preset-taro 更多选项和默认值:
2
+ // https://github.com/NervJS/taro/blob/next/packages/babel-preset-taro/README.md
3
+ module.exports = {
4
+ presets: [
5
+ ['taro', {
6
+ framework: 'react',
7
+ ts: false
8
+ }]
9
+ ]
10
+ }
@@ -0,0 +1,9 @@
1
+ module.exports = {
2
+ env: {
3
+ NODE_ENV: '"development"'
4
+ },
5
+ defineConstants: {
6
+ },
7
+ mini: {},
8
+ h5: {}
9
+ }
@@ -0,0 +1,84 @@
1
+ const config = {
2
+ projectName: 't2',
3
+ date: '2020-5-21',
4
+ designWidth: 750,
5
+ deviceRatio: {
6
+ 640: 2.34 / 2,
7
+ 750: 1,
8
+ 828: 1.81 / 2
9
+ },
10
+ sourceRoot: 'src',
11
+ outputRoot: 'dist',
12
+ plugins: [],
13
+ defineConstants: {
14
+ },
15
+ copy: {
16
+ patterns: [
17
+ ],
18
+ options: {
19
+ }
20
+ },
21
+ framework: 'react',
22
+ compiler: {
23
+ type: 'webpack5',
24
+ prebundle: {
25
+ enable: false
26
+ }
27
+ },
28
+ mini: {
29
+ output: {
30
+ clean: {
31
+ keep: ['project.config.json']
32
+ }
33
+ },
34
+ postcss: {
35
+ pxtransform: {
36
+ enable: true,
37
+ config: {
38
+
39
+ }
40
+ },
41
+ url: {
42
+ enable: true,
43
+ config: {
44
+ limit: 1024 // 设定转换尺寸上限
45
+ }
46
+ },
47
+ cssModules: {
48
+ enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
49
+ config: {
50
+ namingPattern: 'module', // 转换模式,取值为 global/module
51
+ generateScopedName: '[name]__[local]___[hash:base64:5]'
52
+ }
53
+ }
54
+ }
55
+ },
56
+ h5: {
57
+ output: {
58
+ clean: false
59
+ },
60
+ publicPath: '/',
61
+ staticDirectory: 'static',
62
+ postcss: {
63
+ autoprefixer: {
64
+ enable: true,
65
+ config: {
66
+ }
67
+ },
68
+ cssModules: {
69
+ enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
70
+ config: {
71
+ namingPattern: 'module', // 转换模式,取值为 global/module
72
+ generateScopedName: '[name]__[local]___[hash:base64:5]'
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ module.exports = function (merge) {
80
+ if (process.env.NODE_ENV === 'development') {
81
+ return merge({}, config, require('./dev'))
82
+ }
83
+ return merge({}, config, require('./prod'))
84
+ }
@@ -0,0 +1,18 @@
1
+ module.exports = {
2
+ env: {
3
+ NODE_ENV: '"production"'
4
+ },
5
+ defineConstants: {
6
+ },
7
+ mini: {},
8
+ h5: {
9
+ /**
10
+ * 如果h5端编译后体积过大,可以使用webpack-bundle-analyzer插件对打包体积进行分析。
11
+ * 参考代码如下:
12
+ * webpackChain (chain) {
13
+ * chain.plugin('analyzer')
14
+ * .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, [])
15
+ * }
16
+ */
17
+ }
18
+ }
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "test",
3
+ "dependencies": {
4
+ "@babel/runtime": "^7.7.7",
5
+ "@spcsn/taro-shared": "3.0.0-beta.6",
6
+ "@spcsn/taro": "3.0.0-beta.6",
7
+ "@spcsn/taro-api": "3.0.0-beta.6",
8
+ "@spcsn/taro-cli": "3.0.0-beta.6",
9
+ "@spcsn/taro-components": "3.0.0-beta.6",
10
+ "@spcsn/taro-helper": "3.0.0-beta.6",
11
+ "@spcsn/taro-react": "3.0.0-beta.6",
12
+ "@spcsn/taro-runner-utils": "3.0.0-beta.6",
13
+ "@spcsn/taro-runtime": "3.0.0-beta.6",
14
+ "@spcsn/taro-service": "3.0.0-beta.6",
15
+ "@spcsn/taro-plugin-platform-weapp": "3.0.0-beta.6",
16
+ "@spcsn/taro-plugin-framework-react": "3.0.0-beta.6",
17
+ "@spcsn/taro-plugin-generator": "3.0.0-beta.6",
18
+ "@spcsn/taro-vite-runner": "3.0.0-beta.6",
19
+ "@spcsn/taro-binding": "0.1.0"
20
+ },
21
+ "devDependencies": {
22
+ "babel-preset-taro": "2.0.0",
23
+ "babel-plugin-transform-taroapi": "3.0.0-beta.6",
24
+ "postcss-plugin-constparse": "3.0.0-beta.6",
25
+ "postcss-pxtransform": "3.0.0-beta.6"
26
+ }
27
+ }
@@ -0,0 +1,11 @@
1
+ export default {
2
+ pages: [
3
+ 'pages/index/index'
4
+ ],
5
+ window: {
6
+ backgroundTextStyle: 'light',
7
+ navigationBarBackgroundColor: '#fff',
8
+ navigationBarTitleText: 'WeChat',
9
+ navigationBarTextStyle: 'black'
10
+ }
11
+ }
@@ -0,0 +1,16 @@
1
+ import { Component } from 'react'
2
+
3
+ class App extends Component {
4
+ componentDidMount () {}
5
+
6
+ componentDidShow () {}
7
+
8
+ componentDidHide () {}
9
+
10
+ // this.props.children 是将要会渲染的页面
11
+ render () {
12
+ return this.props.children
13
+ }
14
+ }
15
+
16
+ export default App
File without changes
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
5
+ <meta content="width=device-width,initial-scale=1,user-scalable=no" name="viewport">
6
+ <meta name="apple-mobile-web-app-capable" content="yes">
7
+ <meta name="apple-touch-fullscreen" content="yes">
8
+ <meta name="format-detection" content="telephone=no,address=no">
9
+ <meta name="apple-mobile-web-app-status-bar-style" content="white">
10
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
11
+ <title></title>
12
+ <script>
13
+ !function(x){function w(){var v,u,t,tes,s=x.document,r=s.documentElement,a=r.getBoundingClientRect().width;if(!v&&!u){var n=!!x.navigator.appVersion.match(/AppleWebKit.*Mobile.*/);v=x.devicePixelRatio;tes=x.devicePixelRatio;v=n?v:1,u=1/v}if(a>=640){r.style.fontSize="40px"}else{if(a<=320){r.style.fontSize="20px"}else{r.style.fontSize=a/320*20+"px"}}}x.addEventListener("resize",function(){w()});w()}(window);
14
+ </script>
15
+ </head>
16
+ <body>
17
+ <div id="app"></div>
18
+ </body>
19
+ </html>
@@ -0,0 +1,3 @@
1
+ export default {
2
+ navigationBarTitleText: '首页'
3
+ }
@@ -0,0 +1,22 @@
1
+ import React, { Component } from 'react'
2
+ import { View, Text } from '@spcsn/taro-components'
3
+
4
+ export default class Index extends Component {
5
+ componentWillMount () { }
6
+
7
+ componentDidMount () { }
8
+
9
+ componentWillUnmount () { }
10
+
11
+ componentDidShow () { }
12
+
13
+ componentDidHide () { }
14
+
15
+ render () {
16
+ return (
17
+ <View className='index'>
18
+ <Text>Hello world!</Text>
19
+ </View>
20
+ )
21
+ }
22
+ }
@@ -0,0 +1,72 @@
1
+ import * as path from 'node:path';
2
+
3
+ import { chalk } from '@spcsn/taro-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('@spcsn/taro-helper')).toBeTruthy()
63
+ // expect(info.npmPackages.hasOwnProperty('@spcsn/taro-service')).toBeTruthy()
64
+ // expect(info.npmPackages.hasOwnProperty('@spcsn/taro')).toBeTruthy()
65
+ // expect(info.npmPackages.hasOwnProperty('@spcsn/taroize')).toBeTruthy()
66
+ // expect(info.npmPackages.hasOwnProperty('@spcsn/taro-webpack-runner')).toBeTruthy()
67
+ // expect(info.npmPackages.hasOwnProperty('babel-plugin-transform-taroapi')).toBeTruthy()
68
+ // expect(info.npmPackages.hasOwnProperty('postcss-pxtransform')).toBeTruthy()
69
+
70
+ logSpy.mockRestore();
71
+ });
72
+ });