@spcsn/taro-cli 0.1.6 → 1.0.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 (188) hide show
  1. package/LICENSE +1 -153
  2. package/README.md +10 -7
  3. package/define-config.d.ts +1 -0
  4. package/define-config.js +1 -0
  5. package/dist/cli.js +140 -110
  6. package/dist/cli.js.map +1 -1
  7. package/dist/commands/{customCommand.d.ts → custom-command.d.ts} +1 -1
  8. package/dist/commands/custom-command.js +23 -0
  9. package/dist/commands/custom-command.js.map +1 -0
  10. package/dist/create/creator.js +2 -0
  11. package/dist/create/creator.js.map +1 -1
  12. package/dist/create/{fetchTemplate.js → fetch-template.js} +26 -33
  13. package/dist/create/fetch-template.js.map +1 -0
  14. package/dist/create/project.d.ts +2 -6
  15. package/dist/create/project.js +240 -285
  16. package/dist/create/project.js.map +1 -1
  17. package/dist/create/template-creator.d.ts +75 -0
  18. package/dist/create/template-creator.js +275 -0
  19. package/dist/create/template-creator.js.map +1 -0
  20. package/dist/doctor/validators.d.ts +7 -6
  21. package/dist/doctor/validators.js +6 -7
  22. package/dist/doctor/validators.js.map +1 -1
  23. package/dist/platform-weapp/apis-list.d.ts +1 -0
  24. package/dist/platform-weapp/apis-list.js +37 -0
  25. package/dist/platform-weapp/apis-list.js.map +1 -0
  26. package/dist/platform-weapp/apis.d.ts +16 -0
  27. package/dist/platform-weapp/apis.js +34 -0
  28. package/dist/platform-weapp/apis.js.map +1 -0
  29. package/dist/platform-weapp/components-react.d.ts +80 -0
  30. package/dist/platform-weapp/components-react.js +86 -0
  31. package/dist/platform-weapp/components-react.js.map +1 -0
  32. package/dist/platform-weapp/components.d.ts +4 -0
  33. package/dist/platform-weapp/components.js +520 -0
  34. package/dist/platform-weapp/components.js.map +1 -0
  35. package/dist/platform-weapp/index.d.ts +11 -0
  36. package/dist/platform-weapp/index.js +17 -0
  37. package/dist/platform-weapp/index.js.map +1 -0
  38. package/dist/platform-weapp/program.d.ts +35 -0
  39. package/dist/platform-weapp/program.js +64 -0
  40. package/dist/platform-weapp/program.js.map +1 -0
  41. package/dist/platform-weapp/runtime-utils.d.ts +27 -0
  42. package/dist/platform-weapp/runtime-utils.js +48 -0
  43. package/dist/platform-weapp/runtime-utils.js.map +1 -0
  44. package/dist/platform-weapp/runtime.d.ts +1 -0
  45. package/dist/platform-weapp/runtime.js +7 -0
  46. package/dist/platform-weapp/runtime.js.map +1 -0
  47. package/dist/platform-weapp/template.d.ts +39 -0
  48. package/dist/platform-weapp/template.js +119 -0
  49. package/dist/platform-weapp/template.js.map +1 -0
  50. package/dist/presets/commands/build.js +25 -45
  51. package/dist/presets/commands/build.js.map +1 -1
  52. package/dist/presets/commands/init.js +2 -4
  53. package/dist/presets/commands/init.js.map +1 -1
  54. package/dist/presets/constant/hooks.d.ts +0 -2
  55. package/dist/presets/constant/hooks.js +1 -3
  56. package/dist/presets/constant/hooks.js.map +1 -1
  57. package/dist/presets/files/generate-framework-info.js +68 -0
  58. package/dist/presets/files/generate-framework-info.js.map +1 -0
  59. package/dist/presets/files/{generateProjectConfig.js → generate-project-config.js} +1 -1
  60. package/dist/presets/files/generate-project-config.js.map +1 -0
  61. package/dist/presets/files/{writeFileToDist.js → write-file-to-dist.js} +1 -1
  62. package/dist/presets/files/write-file-to-dist.js.map +1 -0
  63. package/dist/presets/hooks/build.js +1 -3
  64. package/dist/presets/hooks/build.js.map +1 -1
  65. package/dist/presets/index.js +3 -5
  66. package/dist/presets/index.js.map +1 -1
  67. package/dist/util/{appConfig.d.ts → app-config.d.ts} +2 -2
  68. package/dist/util/{appConfig.js → app-config.js} +4 -3
  69. package/dist/util/app-config.js.map +1 -0
  70. package/dist/util/defineConfig.d.ts +5 -5
  71. package/dist/util/defineConfig.js.map +1 -1
  72. package/dist/util/index.d.ts +9 -1
  73. package/dist/util/index.js +54 -23
  74. package/dist/util/index.js.map +1 -1
  75. package/package.json +24 -27
  76. package/postinstall.js +1 -17
  77. package/src/cli.ts +185 -132
  78. package/src/commands/custom-command.ts +22 -0
  79. package/src/create/{fetchTemplate.ts → fetch-template.ts} +30 -35
  80. package/src/create/project.ts +31 -78
  81. package/src/create/template-creator.ts +325 -0
  82. package/src/doctor/validators.js +1 -0
  83. package/src/doctor/validators.ts +8 -6
  84. package/src/platform-weapp/apis-list.ts +33 -0
  85. package/src/platform-weapp/apis.ts +48 -0
  86. package/src/platform-weapp/components-react.ts +81 -0
  87. package/src/platform-weapp/components.ts +521 -0
  88. package/src/platform-weapp/index.ts +25 -0
  89. package/src/platform-weapp/program.ts +70 -0
  90. package/src/platform-weapp/runtime-utils.ts +57 -0
  91. package/src/platform-weapp/runtime.ts +6 -0
  92. package/src/platform-weapp/template.ts +150 -0
  93. package/src/presets/commands/build.ts +20 -40
  94. package/src/presets/commands/init.ts +1 -4
  95. package/src/presets/constant/hooks.js +12 -0
  96. package/src/presets/constant/hooks.ts +0 -2
  97. package/src/presets/files/{generateFrameworkInfo.ts → generate-framework-info.ts} +7 -1
  98. package/src/presets/hooks/build.ts +1 -3
  99. package/src/presets/index.ts +3 -5
  100. package/src/util/app-config.js +1 -0
  101. package/src/util/{appConfig.ts → app-config.ts} +5 -4
  102. package/src/util/defineConfig.ts +6 -6
  103. package/src/util/index.ts +67 -25
  104. package/templates/default/config/dev.js +1 -1
  105. package/templates/default/config/index.js +28 -40
  106. package/templates/default/config/prod.js +1 -1
  107. package/templates/default/package.json.tmpl +2 -17
  108. package/templates/default/types/global.d.ts +0 -4
  109. package/templates/plugin-compile/README.md +3 -1
  110. package/templates/plugin-compile/package.json.tmpl +1 -2
  111. package/templates/plugin-compile/src/index.ts +18 -28
  112. package/dist/commands/customCommand.js +0 -23
  113. package/dist/commands/customCommand.js.map +0 -1
  114. package/dist/config/packagesManagement.d.ts +0 -19
  115. package/dist/config/packagesManagement.js +0 -22
  116. package/dist/config/packagesManagement.js.map +0 -1
  117. package/dist/create/fetchTemplate.js.map +0 -1
  118. package/dist/create/page.d.ts +0 -65
  119. package/dist/create/page.js +0 -266
  120. package/dist/create/page.js.map +0 -1
  121. package/dist/create/plugin.d.ts +0 -16
  122. package/dist/create/plugin.js +0 -77
  123. package/dist/create/plugin.js.map +0 -1
  124. package/dist/presets/commands/config.js +0 -121
  125. package/dist/presets/commands/config.js.map +0 -1
  126. package/dist/presets/commands/create.js +0 -112
  127. package/dist/presets/commands/create.js.map +0 -1
  128. package/dist/presets/commands/doctor.js +0 -15
  129. package/dist/presets/commands/doctor.js.map +0 -1
  130. package/dist/presets/commands/global-config.d.ts +0 -3
  131. package/dist/presets/commands/global-config.js +0 -175
  132. package/dist/presets/commands/global-config.js.map +0 -1
  133. package/dist/presets/commands/help.d.ts +0 -3
  134. package/dist/presets/commands/help.js +0 -16
  135. package/dist/presets/commands/help.js.map +0 -1
  136. package/dist/presets/commands/info.d.ts +0 -3
  137. package/dist/presets/commands/info.js +0 -96
  138. package/dist/presets/commands/info.js.map +0 -1
  139. package/dist/presets/commands/inspect.d.ts +0 -3
  140. package/dist/presets/commands/inspect.js +0 -155
  141. package/dist/presets/commands/inspect.js.map +0 -1
  142. package/dist/presets/commands/update.d.ts +0 -3
  143. package/dist/presets/commands/update.js +0 -187
  144. package/dist/presets/commands/update.js.map +0 -1
  145. package/dist/presets/files/generateFrameworkInfo.d.ts +0 -3
  146. package/dist/presets/files/generateFrameworkInfo.js +0 -30
  147. package/dist/presets/files/generateFrameworkInfo.js.map +0 -1
  148. package/dist/presets/files/generateProjectConfig.d.ts +0 -3
  149. package/dist/presets/files/generateProjectConfig.js.map +0 -1
  150. package/dist/presets/files/writeFileToDist.d.ts +0 -3
  151. package/dist/presets/files/writeFileToDist.js.map +0 -1
  152. package/dist/presets/hooks/create.d.ts +0 -3
  153. package/dist/presets/hooks/create.js +0 -42
  154. package/dist/presets/hooks/create.js.map +0 -1
  155. package/dist/presets/platforms/plugin.d.ts +0 -3
  156. package/dist/presets/platforms/plugin.js +0 -106
  157. package/dist/presets/platforms/plugin.js.map +0 -1
  158. package/dist/presets/platforms/rn.d.ts +0 -3
  159. package/dist/presets/platforms/rn.js +0 -130
  160. package/dist/presets/platforms/rn.js.map +0 -1
  161. package/dist/util/appConfig.js.map +0 -1
  162. package/dist/util/createPage.d.ts +0 -9
  163. package/dist/util/createPage.js +0 -114
  164. package/dist/util/createPage.js.map +0 -1
  165. package/src/commands/customCommand.ts +0 -22
  166. package/src/config/packagesManagement.ts +0 -20
  167. package/src/config/rn-stylelint.json +0 -4
  168. package/src/create/page.ts +0 -325
  169. package/src/create/plugin.ts +0 -55
  170. package/src/presets/commands/config.ts +0 -86
  171. package/src/presets/commands/create.ts +0 -104
  172. package/src/presets/commands/doctor.ts +0 -21
  173. package/src/presets/commands/global-config.ts +0 -140
  174. package/src/presets/commands/help.ts +0 -17
  175. package/src/presets/commands/info.ts +0 -81
  176. package/src/presets/commands/inspect.ts +0 -137
  177. package/src/presets/commands/update.ts +0 -175
  178. package/src/presets/hooks/create.ts +0 -9
  179. package/src/presets/platforms/plugin.ts +0 -75
  180. package/src/presets/platforms/rn.ts +0 -125
  181. package/src/util/createPage.ts +0 -139
  182. package/templates/default/babel.config.js +0 -14
  183. /package/dist/create/{fetchTemplate.d.ts → fetch-template.d.ts} +0 -0
  184. /package/dist/presets/{commands/config.d.ts → files/generate-framework-info.d.ts} +0 -0
  185. /package/dist/presets/{commands/create.d.ts → files/generate-project-config.d.ts} +0 -0
  186. /package/dist/presets/{commands/doctor.d.ts → files/write-file-to-dist.d.ts} +0 -0
  187. /package/src/presets/files/{generateProjectConfig.ts → generate-project-config.ts} +0 -0
  188. /package/src/presets/files/{writeFileToDist.ts → write-file-to-dist.ts} +0 -0
package/src/cli.ts CHANGED
@@ -1,14 +1,117 @@
1
1
  import * as path from 'node:path';
2
2
 
3
- import { dotenvParse, fs, patchEnv } from '@spcsn/taro-helper';
3
+ import { dotenvParse, patchEnv } from '@spcsn/taro-helper';
4
4
  import { Config, Kernel } from '@spcsn/taro-service';
5
- import minimist from 'minimist';
6
5
 
7
- import customCommand from './commands/customCommand';
8
- import { getPkgVersion } from './util';
6
+ import customCommand from './commands/custom-command';
7
+ import { cliProfiler, getPkgVersion } from './util/index.js';
9
8
 
10
- const DISABLE_GLOBAL_CONFIG_COMMANDS = ['global-config', 'doctor', 'update', 'config'];
11
9
  const DEFAULT_FRAMEWORK = 'react';
10
+ const SUPPORTED_COMMANDS = new Set(['build', 'init']);
11
+
12
+ interface CliArgs {
13
+ _: string[];
14
+ [key: string]: boolean | number | string | string[] | undefined;
15
+ }
16
+
17
+ const ARG_ALIASES: Record<string, string[]> = {
18
+ envPrefix: ['env-prefix'],
19
+ help: ['h'],
20
+ port: ['p'],
21
+ version: ['v'],
22
+ };
23
+
24
+ const BOOLEAN_ARGS = new Set(['disable-global-config', 'h', 'help', 'v', 'version']);
25
+
26
+ function parseArgValue(value: string): number | string {
27
+ if (value.trim() !== '' && /^-?\d+(\.\d+)?$/.test(value)) {
28
+ return Number(value);
29
+ }
30
+
31
+ return value;
32
+ }
33
+
34
+ function setArgValue(args: CliArgs, key: string, value: boolean | number | string) {
35
+ args[key] = value;
36
+
37
+ for (const [canonicalKey, aliases] of Object.entries(ARG_ALIASES)) {
38
+ if (key === canonicalKey || aliases.includes(key)) {
39
+ args[canonicalKey] = value;
40
+ aliases.forEach((alias) => {
41
+ args[alias] = value;
42
+ });
43
+ return;
44
+ }
45
+ }
46
+ }
47
+
48
+ function getStringArg(args: CliArgs, key: string): string | undefined {
49
+ const value = args[key];
50
+ return typeof value === 'string' ? value : undefined;
51
+ }
52
+
53
+ function getNumberArg(args: CliArgs, key: string): number | undefined {
54
+ const value = args[key];
55
+ return typeof value === 'number' ? value : undefined;
56
+ }
57
+
58
+ function parseCliArgs(argv: string[]): CliArgs {
59
+ const args: CliArgs = {
60
+ _: [],
61
+ build: true,
62
+ check: true,
63
+ h: false,
64
+ help: false,
65
+ 'inject-global-style': true,
66
+ v: false,
67
+ version: false,
68
+ };
69
+
70
+ for (let index = 0; index < argv.length; index++) {
71
+ const item = argv[index];
72
+ if (!item.startsWith('-') || item === '-') {
73
+ args._.push(item);
74
+ continue;
75
+ }
76
+
77
+ if (item === '--') {
78
+ args._.push(...argv.slice(index + 1));
79
+ break;
80
+ }
81
+
82
+ const normalizedItem = item.replace(/^--?/, '');
83
+ const equalIndex = normalizedItem.indexOf('=');
84
+ const rawKey = equalIndex >= 0 ? normalizedItem.slice(0, equalIndex) : normalizedItem;
85
+ const inlineValue = equalIndex >= 0 ? normalizedItem.slice(equalIndex + 1) : undefined;
86
+ const isNegativeBoolean = rawKey.startsWith('no-');
87
+ const key = isNegativeBoolean ? rawKey.slice(3) : rawKey;
88
+
89
+ if (isNegativeBoolean) {
90
+ setArgValue(args, key, false);
91
+ continue;
92
+ }
93
+
94
+ if (inlineValue !== undefined) {
95
+ setArgValue(args, key, parseArgValue(inlineValue));
96
+ continue;
97
+ }
98
+
99
+ if (BOOLEAN_ARGS.has(key)) {
100
+ setArgValue(args, key, true);
101
+ continue;
102
+ }
103
+
104
+ const nextItem = argv[index + 1];
105
+ if (nextItem && !nextItem.startsWith('-')) {
106
+ setArgValue(args, key, parseArgValue(nextItem));
107
+ index++;
108
+ } else {
109
+ setArgValue(args, key, true);
110
+ }
111
+ }
112
+
113
+ return args;
114
+ }
12
115
 
13
116
  export default class CLI {
14
117
  appPath: string;
@@ -25,58 +128,45 @@ export default class CLI {
25
128
  }
26
129
 
27
130
  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
- });
131
+ const totalStartMs = cliProfiler.start();
132
+ const parseArgsStartMs = cliProfiler.start();
133
+ const args = parseCliArgs(process.argv.slice(2));
134
+ cliProfiler.end('parse args', parseArgsStartMs);
49
135
  const _ = args._;
50
136
  const command = _[0];
51
137
  if (command) {
52
138
  const appPath = this.appPath;
53
139
  const presetsPath = path.resolve(__dirname, 'presets');
54
140
  const commandsPath = path.resolve(presetsPath, 'commands');
55
- const platformsPath = path.resolve(presetsPath, 'platforms');
56
- const commandPlugins = fs.readdirSync(commandsPath);
57
141
  const targetPlugin = `${command}.js`;
58
142
 
143
+ if (!SUPPORTED_COMMANDS.has(command)) {
144
+ console.log('当前 CLI 仅支持 build 和 init 命令。');
145
+ return;
146
+ }
147
+
59
148
  // 设置环境变量
60
- process.env.NODE_ENV ||= args.env;
61
- if (process.env.NODE_ENV === 'undefined' && (command === 'build' || command === 'inspect')) {
149
+ const env = getStringArg(args, 'env');
150
+ const envPrefix = getStringArg(args, 'envPrefix');
151
+ process.env.NODE_ENV ||= env;
152
+ if (process.env.NODE_ENV === 'undefined' && command === 'build') {
62
153
  process.env.NODE_ENV = args.watch ? 'development' : 'production';
63
154
  }
64
- args.type ||= args.t;
65
- // React-only / weapp-first fork:build/inspect 命令未显式指定 --type 时默认 weapp
66
- if (!args.type && (command === 'build' || command === 'inspect') && typeof args.plugin !== 'string') {
155
+ args.type ||= getStringArg(args, 't');
156
+ if (!args.type && command === 'build') {
67
157
  args.type = 'weapp';
68
158
  }
69
- if (args.type) {
70
- process.env.TARO_ENV = args.type;
159
+ const type = getStringArg(args, 'type');
160
+ if (type) {
161
+ process.env.TARO_ENV = type;
71
162
  }
72
- if (typeof args.plugin === 'string') {
73
- process.env.TARO_ENV = 'plugin';
74
- }
75
- const mode = args.mode || process.env.NODE_ENV;
163
+ const mode = getStringArg(args, 'mode') || process.env.NODE_ENV || 'production';
76
164
  // 这里解析 dotenv 以便于 config 解析时能获取 dotenv 配置信息
77
- const expandEnv = dotenvParse(appPath, args.envPrefix, mode);
165
+ const dotenvStartMs = cliProfiler.start();
166
+ const expandEnv = dotenvParse(appPath, envPrefix, mode);
167
+ cliProfiler.end('dotenv parse', dotenvStartMs);
78
168
 
79
- const disableGlobalConfig = !!(args['disable-global-config'] || DISABLE_GLOBAL_CONFIG_COMMANDS.includes(command));
169
+ const disableGlobalConfig = !!args['disable-global-config'];
80
170
 
81
171
  const configEnv = {
82
172
  mode,
@@ -86,14 +176,16 @@ export default class CLI {
86
176
  appPath: this.appPath,
87
177
  disableGlobalConfig: disableGlobalConfig,
88
178
  });
89
- await config.init(configEnv);
179
+ await cliProfiler.measure('config init', () => config.init(configEnv));
90
180
 
181
+ const kernelStartMs = cliProfiler.start();
91
182
  const kernel = new Kernel({
92
183
  appPath,
93
184
  presets: [path.resolve(__dirname, '.', 'presets', 'index.js')],
94
185
  config,
95
186
  plugins: [],
96
187
  });
188
+ cliProfiler.end('kernel init', kernelStartMs);
97
189
  kernel.optsPlugins ||= [];
98
190
 
99
191
  // 将自定义的 变量 添加到 config.env 中,实现 definePlugin 字段定义
@@ -101,28 +193,18 @@ export default class CLI {
101
193
  if (initialConfig) {
102
194
  initialConfig.env = patchEnv(initialConfig, expandEnv);
103
195
  }
104
- if (commandPlugins.includes(targetPlugin)) {
105
- // 针对不同的内置命令注册对应的命令插件
106
- kernel.optsPlugins.push(path.resolve(commandsPath, targetPlugin));
107
- }
108
-
109
- // 把内置命令插件传递给 kernel,可以暴露给其他插件使用
196
+ kernel.optsPlugins.push(path.resolve(commandsPath, targetPlugin));
110
197
  kernel.cliCommandsPath = commandsPath;
111
- kernel.cliCommands = commandPlugins
112
- .filter((commandFileName) => /^[\w-]+(\.[\w-]+)*\.js$/.test(commandFileName))
113
- .map((fileName) => fileName.replace(/\.js$/, ''));
198
+ kernel.cliCommands = Array.from(SUPPORTED_COMMANDS);
114
199
 
115
200
  switch (command) {
116
- case 'inspect':
117
201
  case 'build': {
118
- let plugin;
119
- let platform = args.type;
120
- const { publicPath, bundleOutput, sourcemapOutput, sourceMapUrl, sourcemapSourcesRoot, assetsDest } = args;
202
+ let platform = getStringArg(args, 'type');
121
203
 
122
204
  // 针对不同的内置平台注册对应的端平台插件
123
205
  switch (platform) {
124
206
  case 'weapp':
125
- kernel.optsPlugins.push(`@spcsn/taro-plugin-platform-${platform}`);
207
+ kernel.optsPlugins.push(path.resolve(__dirname, 'platform-weapp'));
126
208
  break;
127
209
  default: {
128
210
  if (platform) {
@@ -139,78 +221,56 @@ export default class CLI {
139
221
  console.log('当前 Fork 仅支持 React 框架。');
140
222
  return;
141
223
  }
142
- kernel.optsPlugins.push('@spcsn/taro-plugin-framework-react');
143
-
144
- // 编译小程序插件
145
- if (typeof args.plugin === 'string') {
146
- plugin = args.plugin;
147
- if (plugin !== 'weapp') {
148
- console.log('当前 Fork 仅支持 weapp 小程序插件编译。');
149
- return;
150
- }
151
- platform = 'plugin';
152
- kernel.optsPlugins.push(path.resolve(platformsPath, 'plugin.js'));
153
- kernel.optsPlugins.push(`@spcsn/taro-plugin-platform-${plugin}`);
154
- }
155
-
156
- // 传递 inspect 参数即可
157
- if (command === 'inspect') {
158
- customCommand(command, kernel, args);
159
- break;
160
- }
161
- customCommand(command, kernel, {
162
- args,
163
- _,
164
- platform,
165
- plugin,
166
- isWatch: Boolean(args.watch),
167
- // Note: 是否把 Taro 组件编译为原生自定义组件
168
- isBuildNativeComp: _[1] === 'native-components',
169
- // Note: 新的混合编译模式,支持把组件单独编译为原生组件
170
- newBlended: Boolean(args['new-blended']),
171
- // Note: 是否禁用编译
172
- withoutBuild: !args.build,
173
- noInjectGlobalStyle: !args['inject-global-style'],
174
- noCheck: !args.check,
175
- port: args.port,
176
- env: args.env,
177
- deviceType: args.platform,
178
- resetCache: !!args.resetCache,
179
- publicPath,
180
- bundleOutput,
181
- sourcemapOutput,
182
- sourceMapUrl,
183
- sourcemapSourcesRoot,
184
- assetsDest,
185
- qr: !!args.qr,
186
- blended: Boolean(args.blended),
187
- h: args.h,
188
- });
224
+ kernel.optsPlugins.push(require.resolve('@spcsn/taro-mini-runner/framework-react'));
225
+ await cliProfiler.measure('build command', () =>
226
+ customCommand(command, kernel, {
227
+ args,
228
+ _,
229
+ platform,
230
+ isWatch: Boolean(args.watch),
231
+ // Note: 是否把 Taro 组件编译为原生自定义组件
232
+ isBuildNativeComp: _[1] === 'native-components',
233
+ // Note: 新的混合编译模式,支持把组件单独编译为原生组件
234
+ newBlended: Boolean(args['new-blended']),
235
+ // Note: 是否禁用编译
236
+ withoutBuild: !args.build,
237
+ noInjectGlobalStyle: !args['inject-global-style'],
238
+ noCheck: !args.check,
239
+ port: getNumberArg(args, 'port'),
240
+ env,
241
+ deviceType: getStringArg(args, 'platform'),
242
+ qr: !!args.qr,
243
+ blended: Boolean(args.blended),
244
+ h: args.h,
245
+ }),
246
+ );
247
+ cliProfiler.end('total', totalStartMs);
248
+ cliProfiler.print();
189
249
  break;
190
250
  }
191
251
  case 'init': {
192
- customCommand(command, kernel, {
193
- _,
194
- appPath,
195
- projectName: _[1] || args.name,
196
- description: args.description,
197
- typescript: args.typescript,
198
- buildEs5: args['build-es5'],
199
- framework: args.framework,
200
- compiler: args.compiler,
201
- npm: args.npm,
202
- templateSource: args['template-source'],
203
- clone: !!args.clone,
204
- template: args.template,
205
- css: args.css,
206
- autoInstall: args.autoInstall,
207
- h: args.h,
208
- });
252
+ await cliProfiler.measure('init command', () =>
253
+ customCommand(command, kernel, {
254
+ _,
255
+ appPath,
256
+ projectName: _[1] || getStringArg(args, 'name'),
257
+ description: getStringArg(args, 'description'),
258
+ typescript: args.typescript,
259
+ framework: getStringArg(args, 'framework'),
260
+ compiler: getStringArg(args, 'compiler'),
261
+ npm: getStringArg(args, 'npm'),
262
+ templateSource: getStringArg(args, 'template-source'),
263
+ clone: !!args.clone,
264
+ template: getStringArg(args, 'template'),
265
+ css: getStringArg(args, 'css'),
266
+ autoInstall: args.autoInstall,
267
+ h: args.h,
268
+ }),
269
+ );
270
+ cliProfiler.end('total', totalStartMs);
271
+ cliProfiler.print();
209
272
  break;
210
273
  }
211
- default:
212
- customCommand(command, kernel, args);
213
- break;
214
274
  }
215
275
  } else {
216
276
  if (args.h) {
@@ -221,15 +281,8 @@ export default class CLI {
221
281
  console.log(' -h, --help output usage information');
222
282
  console.log();
223
283
  console.log('Commands:');
224
- console.log(' init [projectName] Init a project with default templete');
225
- console.log(' config <cmd> Taro config');
226
- console.log(' create Create page for project');
227
- console.log(' build Build a project with options');
228
- console.log(' update Update packages of taro');
229
- console.log(' info Diagnostics Taro env info');
230
- console.log(' doctor Diagnose taro project');
231
- console.log(' inspect Inspect the webpack config');
232
- console.log(' help [cmd] display help for [cmd]');
284
+ console.log(' init [projectName] Init a project with default template');
285
+ console.log(' build Build a WeApp project');
233
286
  } else if (args.v) {
234
287
  console.log(getPkgVersion());
235
288
  }
@@ -0,0 +1,22 @@
1
+ import { Kernel } from '@spcsn/taro-service';
2
+
3
+ export default function customCommand(command: string, kernel: Kernel, args: { _: string[]; [key: string]: any }) {
4
+ if (typeof command !== 'string') return Promise.resolve();
5
+
6
+ const options: any = {};
7
+ const excludeKeys = ['_', 'version', 'v', 'help', 'h', 'disable-global-config'];
8
+ Object.keys(args).forEach((key) => {
9
+ if (!excludeKeys.includes(key)) {
10
+ options[key] = args[key];
11
+ }
12
+ });
13
+
14
+ return kernel.run({
15
+ name: command,
16
+ opts: {
17
+ _: args._,
18
+ options,
19
+ isHelp: args.h,
20
+ },
21
+ });
22
+ }
@@ -2,7 +2,6 @@ import * as path from 'node:path';
2
2
 
3
3
  import { chalk, fs } from '@spcsn/taro-helper';
4
4
  import * as AdmZip from 'adm-zip';
5
- import axios from 'axios';
6
5
  import * as download from 'download-git-repo';
7
6
  import ora from 'ora';
8
7
 
@@ -19,7 +18,7 @@ export interface ITemplates {
19
18
 
20
19
  const TEMP_DOWNLOAD_FOLDER = 'taro-temp';
21
20
 
22
- export default function fetchTemplate(
21
+ export default async function fetchTemplate(
23
22
  templateSource: string,
24
23
  templateRootPath: string,
25
24
  clone?: boolean,
@@ -27,13 +26,13 @@ export default function fetchTemplate(
27
26
  const type = getTemplateSourceType(templateSource);
28
27
  const tempPath = path.join(templateRootPath, TEMP_DOWNLOAD_FOLDER);
29
28
  let name: string;
30
- // eslint-disable-next-line no-async-promise-executor
31
- return new Promise<void>(async (resolve) => {
32
- // 下载文件的缓存目录
33
- if (fs.existsSync(tempPath)) await fs.remove(tempPath);
34
- await fs.mkdirp(templateRootPath);
35
- await fs.mkdir(tempPath);
36
29
 
30
+ // 下载文件的缓存目录
31
+ if (fs.existsSync(tempPath)) await fs.remove(tempPath);
32
+ await fs.mkdirp(templateRootPath);
33
+ await fs.mkdir(tempPath);
34
+
35
+ return new Promise<void>((resolve) => {
37
36
  const spinner = ora(`正在从 ${templateSource} 拉取远程模板...`).start();
38
37
 
39
38
  if (type === 'git') {
@@ -55,33 +54,29 @@ export default function fetchTemplate(
55
54
  name = 'from-remote-url';
56
55
  const zipPath = path.join(tempPath, name + '.zip');
57
56
  const unZipPath = path.join(tempPath, name);
58
- axios
59
- .get<fs.ReadStream>(templateSource, { responseType: 'stream' })
60
- .then((response) => {
61
- const ws = fs.createWriteStream(zipPath);
62
- response.data.pipe(ws);
63
- ws.on('finish', () => {
64
- // unzip
65
- const zip = new AdmZip(zipPath);
66
- zip.extractAllTo(unZipPath, true);
67
- const files = readDirWithFileTypes(unZipPath).filter(
68
- (file) => !file.name.startsWith('.') && file.isDirectory && file.name !== '__MACOSX',
69
- );
70
-
71
- if (files.length !== 1) {
72
- spinner.color = 'red';
73
- spinner.fail(chalk.red(`拉取远程模板仓库失败!\n${new Error('远程模板源组织格式错误')}`));
74
- return resolve();
75
- }
76
- name = path.join(name, files[0].name);
77
-
78
- spinner.color = 'green';
79
- spinner.succeed(`${chalk.grey('拉取远程模板仓库成功!')}`);
80
- resolve();
81
- });
82
- ws.on('error', (error) => {
83
- throw error;
84
- });
57
+ fetch(templateSource)
58
+ .then(async (response) => {
59
+ if (!response.ok) {
60
+ throw new Error(`HTTP ${response.status} ${response.statusText}`);
61
+ }
62
+
63
+ await fs.writeFile(zipPath, Buffer.from(await response.arrayBuffer()));
64
+ const zip = new AdmZip(zipPath);
65
+ zip.extractAllTo(unZipPath, true);
66
+ const files = readDirWithFileTypes(unZipPath).filter(
67
+ (file) => !file.name.startsWith('.') && file.isDirectory && file.name !== '__MACOSX',
68
+ );
69
+
70
+ if (files.length !== 1) {
71
+ spinner.color = 'red';
72
+ spinner.fail(chalk.red(`拉取远程模板仓库失败!\n${new Error('远程模板源组织格式错误')}`));
73
+ return resolve();
74
+ }
75
+ name = path.join(name, files[0].name);
76
+
77
+ spinner.color = 'green';
78
+ spinner.succeed(`${chalk.grey('拉取远程模板仓库成功!')}`);
79
+ resolve();
85
80
  })
86
81
  .catch(async (error) => {
87
82
  spinner.color = 'red';