@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,215 @@
1
+ import { MessageKind, validateConfig } from '@tarojs/plugin-doctor';
2
+
3
+ import { extractCompileEntry } from '../../util/appConfig';
4
+ import * as hooks from '../constant';
5
+
6
+ import type { IPluginContext } from '@spcsn/taro-service';
7
+
8
+ export default (ctx: IPluginContext) => {
9
+ ctx.registerCommand({
10
+ name: 'build',
11
+ optionsMap: {
12
+ '--type [typeName]': 'Build type, weapp(默认 weapp)',
13
+ '--watch': 'Watch mode',
14
+ '--env [env]': 'Value for process.env.NODE_ENV',
15
+ '--pages': 'Specify the pages to be compiled, separate multiple by comma',
16
+ '--components': 'Specify the components to be compiled, separate multiple by comma',
17
+ '--mode [mode]': 'Value of dotenv extname',
18
+ '-p, --port [port]': 'Specified port',
19
+ '--no-build': 'Do not build project',
20
+ '--blended': 'Blended Taro project in an original MiniApp project',
21
+ '--new-blended':
22
+ 'Blended Taro project in an original MiniApp project while supporting building components independently',
23
+ '--plugin [typeName]': 'Build Taro plugin project, weapp',
24
+ '--env-prefix [envPrefix]': "Provide the dotEnv varables's prefix",
25
+ '--no-check': 'Do not check config is valid or not',
26
+ },
27
+ synopsisList: [
28
+ 'taro build --type weapp',
29
+ 'taro build --type weapp --watch',
30
+ 'taro build --type weapp --watch --pages pages/index/index',
31
+ 'taro build --type weapp --env production',
32
+ 'taro build --type weapp --blended',
33
+ 'taro build --type weapp --no-build',
34
+ 'taro build native-components --type weapp',
35
+ 'taro build --type weapp --new-blended',
36
+ 'taro build --plugin weapp --watch',
37
+ 'taro build --plugin weapp',
38
+ 'taro build --type weapp --mode prepare --env-prefix TARO_APP_',
39
+ ],
40
+ async fn(opts) {
41
+ const { options, config, _ } = opts;
42
+ const { platform, isWatch, blended, newBlended, withoutBuild, noInjectGlobalStyle, noCheck } = options;
43
+ const { fs, chalk, PROJECT_CONFIG } = ctx.helper;
44
+ const { outputPath, configPath } = ctx.paths;
45
+ const { args } = options;
46
+
47
+ if (!configPath || !fs.existsSync(configPath)) {
48
+ console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`));
49
+ process.exit(1);
50
+ }
51
+
52
+ if (typeof platform !== 'string') {
53
+ console.log(chalk.red('请传入正确的编译类型!'));
54
+ process.exit(0);
55
+ }
56
+
57
+ // 校验 Taro 项目配置
58
+ if (!noCheck) {
59
+ const checkResult = await checkConfig({
60
+ projectConfig: ctx.initialConfig,
61
+ helper: ctx.helper,
62
+ });
63
+ if (!checkResult.isValid) {
64
+ const ERROR = chalk.red('[✗] ');
65
+ const WARNING = chalk.yellow('[!] ');
66
+ const SUCCESS = chalk.green('[✓] ');
67
+
68
+ const lineChalk = chalk.hex('#fff');
69
+ const errorChalk = chalk.hex('#f00');
70
+ console.log(errorChalk(`Taro 配置有误,请检查! (${configPath})`));
71
+ checkResult.messages.forEach((message) => {
72
+ switch (message.kind) {
73
+ case MessageKind.Error:
74
+ console.log(' ' + ERROR + lineChalk(message.content));
75
+ break;
76
+ case MessageKind.Success:
77
+ console.log(' ' + SUCCESS + lineChalk(message.content));
78
+ break;
79
+ case MessageKind.Warning:
80
+ console.log(' ' + WARNING + lineChalk(message.content));
81
+ break;
82
+ case MessageKind.Manual:
83
+ console.log(' ' + lineChalk(message.content));
84
+ break;
85
+ default:
86
+ break;
87
+ }
88
+ });
89
+ console.log('');
90
+ process.exit(0);
91
+ }
92
+ }
93
+
94
+ const isProduction = process.env.NODE_ENV === 'production' || !isWatch;
95
+
96
+ // dist folder
97
+ fs.ensureDirSync(outputPath);
98
+
99
+ // is build native components mode?
100
+ const isBuildNativeComp = _[1] === 'native-components';
101
+
102
+ await ctx.applyPlugins(hooks.ON_BUILD_START);
103
+ await ctx.applyPlugins({
104
+ name: platform,
105
+ opts: {
106
+ config: {
107
+ ...config,
108
+ isWatch,
109
+ mode: isProduction ? 'production' : 'development',
110
+ blended,
111
+ isBuildNativeComp,
112
+ withoutBuild,
113
+ newBlended,
114
+ noInjectGlobalStyle,
115
+ async modifyAppConfig(appConfig) {
116
+ extractCompileEntry(appConfig, args, ctx);
117
+
118
+ await ctx.applyPlugins({
119
+ name: hooks.MODIFY_APP_CONFIG,
120
+ opts: {
121
+ appConfig,
122
+ },
123
+ });
124
+ },
125
+ async modifyWebpackChain(chain, webpack, data) {
126
+ await ctx.applyPlugins({
127
+ name: hooks.MODIFY_WEBPACK_CHAIN,
128
+ initialVal: chain,
129
+ opts: {
130
+ chain,
131
+ webpack,
132
+ data,
133
+ },
134
+ });
135
+ },
136
+ async modifyViteConfig(viteConfig, data, viteCompilerContext) {
137
+ await ctx.applyPlugins({
138
+ name: hooks.MODIFY_VITE_CONFIG,
139
+ initialVal: viteConfig,
140
+ opts: {
141
+ viteConfig,
142
+ data,
143
+ viteCompilerContext,
144
+ },
145
+ });
146
+ },
147
+ async modifyBuildAssets(assets, miniPlugin) {
148
+ await ctx.applyPlugins({
149
+ name: hooks.MODIFY_BUILD_ASSETS,
150
+ initialVal: assets,
151
+ opts: {
152
+ assets,
153
+ miniPlugin,
154
+ },
155
+ });
156
+ },
157
+ async modifyMiniConfigs(configMap) {
158
+ await ctx.applyPlugins({
159
+ name: hooks.MODIFY_MINI_CONFIGS,
160
+ initialVal: configMap,
161
+ opts: {
162
+ configMap,
163
+ },
164
+ });
165
+ },
166
+ async modifyComponentConfig(componentConfig, config) {
167
+ await ctx.applyPlugins({
168
+ name: hooks.MODIFY_COMPONENT_CONFIG,
169
+ opts: {
170
+ componentConfig,
171
+ config,
172
+ },
173
+ });
174
+ },
175
+ async onCompilerMake(compilation, compiler, plugin) {
176
+ await ctx.applyPlugins({
177
+ name: hooks.ON_COMPILER_MAKE,
178
+ opts: {
179
+ compilation,
180
+ compiler,
181
+ plugin,
182
+ },
183
+ });
184
+ },
185
+ async onParseCreateElement(nodeName, componentConfig) {
186
+ await ctx.applyPlugins({
187
+ name: hooks.ON_PARSE_CREATE_ELEMENT,
188
+ opts: {
189
+ nodeName,
190
+ componentConfig,
191
+ },
192
+ });
193
+ },
194
+ async onBuildFinish({ error, stats, isWatch }) {
195
+ await ctx.applyPlugins({
196
+ name: hooks.ON_BUILD_FINISH,
197
+ opts: {
198
+ error,
199
+ stats,
200
+ isWatch,
201
+ },
202
+ });
203
+ },
204
+ },
205
+ },
206
+ });
207
+ await ctx.applyPlugins(hooks.ON_BUILD_COMPLETE);
208
+ },
209
+ });
210
+ };
211
+
212
+ async function checkConfig({ projectConfig, helper }) {
213
+ const result = await validateConfig(projectConfig, helper);
214
+ return result;
215
+ }
@@ -0,0 +1,86 @@
1
+ import * as path from 'node:path';
2
+
3
+ import type { IPluginContext } from '@spcsn/taro-service';
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ ctx.registerCommand({
7
+ name: 'config',
8
+ optionsMap: {
9
+ '--json': '以 JSON 形式输出',
10
+ },
11
+ synopsisList: [
12
+ 'taro config set <key> <value>',
13
+ 'taro config get <key>',
14
+ 'taro config delete <key>',
15
+ 'taro config list [--json]',
16
+ ],
17
+ fn({ _, options }) {
18
+ const [, cmd, key, value] = _;
19
+ const json = !!options.json;
20
+ const { fs, getUserHomeDir, TARO_CONFIG_FOLDER, TARO_BASE_CONFIG } = ctx.helper;
21
+ const homedir = getUserHomeDir();
22
+ const configPath = path.join(homedir, `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`);
23
+ if (!homedir) return console.log('找不到用户根目录');
24
+
25
+ function displayConfigPath(configPath) {
26
+ console.log(`Config path: ${configPath}`);
27
+ console.log();
28
+ }
29
+
30
+ switch (cmd) {
31
+ case 'get':
32
+ if (!key) return console.log('Usage: taro config get <key>');
33
+ if (fs.existsSync(configPath)) {
34
+ displayConfigPath(configPath);
35
+ const config = fs.readJSONSync(configPath);
36
+ console.log(`key: ${key}, value: ${config[key]}`);
37
+ }
38
+ break;
39
+ case 'set':
40
+ if (!key || !value) return console.log('Usage: taro config set <key> <value>');
41
+
42
+ if (fs.existsSync(configPath)) {
43
+ displayConfigPath(configPath);
44
+ const config = fs.readJSONSync(configPath);
45
+ config[key] = value;
46
+ fs.writeJSONSync(configPath, config);
47
+ } else {
48
+ fs.ensureFileSync(configPath);
49
+ fs.writeJSONSync(configPath, {
50
+ [key]: value,
51
+ });
52
+ }
53
+ console.log(`set key: ${key}, value: ${value}`);
54
+ break;
55
+ case 'delete':
56
+ if (!key) return console.log('Usage: taro config delete <key>');
57
+
58
+ if (fs.existsSync(configPath)) {
59
+ displayConfigPath(configPath);
60
+ const config = fs.readJSONSync(configPath);
61
+ delete config[key];
62
+ fs.writeJSONSync(configPath, config);
63
+ }
64
+ console.log(`deleted: ${key}`);
65
+ break;
66
+ case 'list':
67
+ case 'ls':
68
+ if (fs.existsSync(configPath)) {
69
+ displayConfigPath(configPath);
70
+ console.log('Config info:');
71
+ const config = fs.readJSONSync(configPath);
72
+ if (json) {
73
+ console.log(JSON.stringify(config, null, 2));
74
+ } else {
75
+ for (const key in config) {
76
+ console.log(`${key}=${config[key]}`);
77
+ }
78
+ }
79
+ }
80
+ break;
81
+ default:
82
+ break;
83
+ }
84
+ },
85
+ });
86
+ };
@@ -0,0 +1,104 @@
1
+ import * as hooks from '../constant';
2
+
3
+ import type { IPluginContext } from '@spcsn/taro-service';
4
+ import type { TSetCustomTemplateConfig } from '../../create/page';
5
+
6
+ declare const enum createTemplateTypeEnum {
7
+ /**
8
+ * taro页面,taro使用者使用
9
+ */
10
+ PAGE = 'page',
11
+ /**
12
+ * taro插件,用于扩展命令行
13
+ */
14
+ PLUGIN_COMMAND = 'plugin-command',
15
+ /**
16
+ * taro插件,用于扩展编译过程
17
+ */
18
+ PLUGIN_BUILD = 'plugin-build',
19
+ /**
20
+ * taro插件,用于扩展 taro create 自定义模版
21
+ */
22
+ PLUGIN_TEMPLATE = 'plugin-template',
23
+ }
24
+
25
+ export default (ctx: IPluginContext) => {
26
+ ctx.registerCommand({
27
+ name: 'create',
28
+ optionsMap: {
29
+ '--name [name]': '名称',
30
+ '--dir [dir]': '路径',
31
+ '--subpkg [subpkg]': '分包路径',
32
+ '--description [description]': '介绍',
33
+ '--type [type]': '模版类型(page(默认)|plugin-command|plugin-build|plugin-template)',
34
+ },
35
+ synopsisList: [
36
+ 'taro create page',
37
+ 'taro create --name=page --description=desc',
38
+ 'taro create my-plugin --type=plugin-command',
39
+ ],
40
+ fn({ _, options }) {
41
+ const type = options.type || createTemplateTypeEnum.PAGE;
42
+ const name = _[1] || options.name;
43
+ const description = options.description || '';
44
+ const afterCreate = options.afterCreate;
45
+ const templateSource = options.templateSource;
46
+ const framework = options.framework;
47
+ const css = options.css;
48
+ const typescript = options.typescript;
49
+ const clone = options.clone;
50
+ const { chalk } = ctx.helper;
51
+ const { appPath } = ctx.paths;
52
+
53
+ switch (type) {
54
+ case createTemplateTypeEnum.PAGE: {
55
+ if (typeof name !== 'string') {
56
+ return console.log(chalk.red('请输入需要创建的页面名称'));
57
+ }
58
+
59
+ const Page = require('../../create/page').default;
60
+ const page = new Page({
61
+ clone,
62
+ subPkg: options.subpkg,
63
+ framework,
64
+ css,
65
+ typescript,
66
+ pageDir: options.dir,
67
+ pageName: name,
68
+ projectDir: appPath,
69
+ description,
70
+ templateSource,
71
+ afterCreate,
72
+ async modifyCustomTemplateConfig(cb: TSetCustomTemplateConfig) {
73
+ await ctx.applyPlugins({ name: hooks.MODIFY_CREATE_TEMPLATE, opts: cb });
74
+ },
75
+ });
76
+
77
+ page.create();
78
+ break;
79
+ }
80
+ case createTemplateTypeEnum.PLUGIN_COMMAND:
81
+ case createTemplateTypeEnum.PLUGIN_BUILD:
82
+ case createTemplateTypeEnum.PLUGIN_TEMPLATE: {
83
+ if (typeof name !== 'string') {
84
+ return console.log(chalk.red('请输入需要创建的插件名称'));
85
+ }
86
+
87
+ const Plugin = require('../../create/plugin').default;
88
+ const plugin = new Plugin({
89
+ pluginName: name,
90
+ projectDir: appPath,
91
+ type,
92
+ description,
93
+ template: 'plugin-compile',
94
+ });
95
+
96
+ plugin.create();
97
+ break;
98
+ }
99
+ default:
100
+ break;
101
+ }
102
+ },
103
+ });
104
+ };
@@ -0,0 +1,140 @@
1
+ import * as path from 'node:path';
2
+
3
+ import ora from 'ora';
4
+ import * as validatePkgName from 'validate-npm-package-name';
5
+
6
+ import { execCommand, getPkgNameByFilterVersion, getRootPath } from '../../util';
7
+
8
+ import type { IPluginContext } from '@spcsn/taro-service';
9
+
10
+ type TPresetOrPluginAction = 'install' | 'uninstall';
11
+ type TPluginType = 'plugin' | 'preset';
12
+
13
+ const PRESET_OR_PLUGIN_COMMAND_CHINESE_MAP_MAP = {
14
+ install: '添加',
15
+ uninstall: '删除',
16
+ };
17
+ const PRESET_OR_PLUGIN_CHINESE_NAME_MAP = {
18
+ plugin: '插件',
19
+ preset: '插件集',
20
+ };
21
+
22
+ const PLUGIN_TYPE_TO_CONFIG_KEY = {
23
+ plugin: 'plugins',
24
+ preset: 'presets',
25
+ };
26
+
27
+ export default (ctx: IPluginContext) => {
28
+ ctx.registerCommand({
29
+ name: 'global-config',
30
+ synopsisList: [
31
+ 'taro global-config add-plugin [pluginName]',
32
+ 'taro global-config remove-plugin [pluginName]',
33
+ 'taro global-config add-preset [presetName]',
34
+ 'taro global-config remove-preset [presetName]',
35
+ 'taro global-config reset',
36
+ ],
37
+ optionsMap: {
38
+ '-r --registry [url]': '指定 npm registry',
39
+ '-h, --help': 'output usage information',
40
+ },
41
+ fn({ _, options }) {
42
+ const [, action, pluginName] = _;
43
+ const { getUserHomeDir, TARO_GLOBAL_CONFIG_DIR, fs, TARO_GLOBAL_CONFIG_FILE } = ctx.helper;
44
+ const homedir = getUserHomeDir();
45
+ const globalPluginConfigDir = path.join(homedir, TARO_GLOBAL_CONFIG_DIR);
46
+ if (!homedir) return console.log('找不到用户根目录');
47
+ const rootPath = getRootPath();
48
+ const templatePath = path.join(rootPath, 'templates', 'global-config');
49
+ const registry = options.registry || options.r;
50
+ function makeSureConfigExists() {
51
+ if (!fs.existsSync(globalPluginConfigDir)) {
52
+ const spinner = ora(`目录不存在,全局配置初始化`).start();
53
+ try {
54
+ fs.copySync(templatePath, globalPluginConfigDir);
55
+ spinner.succeed(`全局配置初始化成功,${globalPluginConfigDir}`);
56
+ } catch (e) {
57
+ spinner.fail(`全局配置初始化失败,${e}`);
58
+ process.exit(1);
59
+ }
60
+ }
61
+ }
62
+ function addOrRemovePresetOrPlugin(actionType: TPresetOrPluginAction, pluginType: TPluginType) {
63
+ makeSureConfigExists();
64
+ const presetOrPluginChineseName = PRESET_OR_PLUGIN_CHINESE_NAME_MAP[pluginType];
65
+ const chineseCommand = PRESET_OR_PLUGIN_COMMAND_CHINESE_MAP_MAP[actionType];
66
+ if (!pluginName) {
67
+ console.error(`缺少要${chineseCommand}的${presetOrPluginChineseName}`);
68
+ process.exit(1);
69
+ }
70
+
71
+ const spinner = ora(`开始${chineseCommand}${presetOrPluginChineseName} ${pluginName}`).start();
72
+ const pluginWithoutVersionName = getPkgNameByFilterVersion(pluginName);
73
+ if (!validatePkgName(pluginWithoutVersionName).validForNewPackages) {
74
+ spinner.fail('安装的插件名不合规!');
75
+ process.exit(1);
76
+ }
77
+ let command = `cd ${globalPluginConfigDir} && npm ${actionType} ${pluginName}`;
78
+ if (registry) {
79
+ command += ` --registry=${registry}`;
80
+ }
81
+ execCommand({
82
+ command,
83
+ successCallback(data) {
84
+ console.log(data.replace(/\n$/, ''));
85
+ spinner.start(`开始修改${presetOrPluginChineseName}配置`);
86
+ const configFilePath = path.join(globalPluginConfigDir, TARO_GLOBAL_CONFIG_FILE);
87
+ let globalConfig;
88
+ try {
89
+ globalConfig = fs.readJSONSync(configFilePath);
90
+ } catch (e) {
91
+ spinner.fail('获取配置文件失败');
92
+ }
93
+ const configKey = PLUGIN_TYPE_TO_CONFIG_KEY[pluginType];
94
+ const configItem = globalConfig[configKey] || [];
95
+ const pluginIndex = configItem.findIndex((item) => {
96
+ if (typeof item === 'string') return item === pluginWithoutVersionName;
97
+ if (item instanceof Array) return item?.[0] === pluginWithoutVersionName;
98
+ });
99
+ const shouldChangeFile = !(Number(pluginIndex !== -1) ^ Number(actionType === 'uninstall'));
100
+ if (shouldChangeFile) {
101
+ actionType === 'install' ? configItem.push(pluginWithoutVersionName) : configItem.splice(pluginIndex, 1);
102
+ try {
103
+ fs.writeJSONSync(configFilePath, {
104
+ [configKey]: configItem,
105
+ });
106
+ } catch (e) {
107
+ spinner.fail(`修改配置文件失败:${e}`);
108
+ }
109
+ }
110
+ spinner.succeed('修改配置文件成功');
111
+ },
112
+ failCallback(data) {
113
+ spinner.stop();
114
+ spinner.warn(data.replace(/\n$/, ''));
115
+ },
116
+ });
117
+ }
118
+ switch (action) {
119
+ case 'add-plugin':
120
+ addOrRemovePresetOrPlugin('install', 'plugin');
121
+ break;
122
+ case 'remove-plugin':
123
+ addOrRemovePresetOrPlugin('uninstall', 'plugin');
124
+ break;
125
+ case 'add-preset':
126
+ addOrRemovePresetOrPlugin('install', 'preset');
127
+ break;
128
+ case 'remove-preset':
129
+ addOrRemovePresetOrPlugin('uninstall', 'preset');
130
+ break;
131
+ case 'reset':
132
+ if (fs.existsSync(globalPluginConfigDir)) fs.removeSync(globalPluginConfigDir);
133
+ fs.copySync(templatePath, globalPluginConfigDir);
134
+ break;
135
+ default:
136
+ console.error('请输出正确的参数');
137
+ }
138
+ },
139
+ });
140
+ };
@@ -0,0 +1,17 @@
1
+ import { chalk } from '@spcsn/taro-helper';
2
+
3
+ import type { IPluginContext } from '@spcsn/taro-service';
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ ctx.registerCommand({
7
+ name: 'help',
8
+ synopsisList: ['taro help [cmd]'],
9
+ async fn({ _ }) {
10
+ const cmd = _[1];
11
+
12
+ if (!cmd) return console.log(chalk.yellow('用法:taro help [cmd],cmd 不存在!请输入需要查看帮助的命令名称。'));
13
+
14
+ ctx.ctx.runHelp(cmd);
15
+ },
16
+ });
17
+ };
@@ -0,0 +1,81 @@
1
+ import * as path from 'node:path';
2
+
3
+ import * as envinfo from 'envinfo';
4
+
5
+ import { getPkgVersion } from '../../util';
6
+
7
+ import type { IPluginContext } from '@spcsn/taro-service';
8
+
9
+ export default (ctx: IPluginContext) => {
10
+ ctx.registerCommand({
11
+ name: 'info',
12
+ synopsisList: ['taro info', 'taro info rn'],
13
+ async fn({ _ }) {
14
+ const rn = _[1] === 'rn';
15
+ const { fs, chalk, PROJECT_CONFIG } = ctx.helper;
16
+ const { appPath, configPath } = ctx.paths;
17
+
18
+ if (!configPath || !fs.existsSync(configPath)) {
19
+ console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`));
20
+ process.exit(1);
21
+ }
22
+
23
+ if (rn) {
24
+ const tempPath = path.join(appPath, '.rn_temp');
25
+ if (fs.lstatSync(tempPath).isDirectory()) {
26
+ process.chdir('.rn_temp');
27
+ }
28
+ }
29
+
30
+ await info({}, ctx);
31
+ },
32
+ });
33
+ };
34
+
35
+ // 单独function获取@jdtaro相关的包
36
+ function getJdtaroPackages(ctx) {
37
+ try {
38
+ const { appPath } = ctx.paths;
39
+ const fs = ctx.helper.fs;
40
+ const packageJsonPath = path.join(appPath, 'package.json');
41
+
42
+ if (fs.existsSync(packageJsonPath)) {
43
+ const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
44
+ const packageJson = JSON.parse(packageJsonContent);
45
+ const dependencies = Object.assign({}, packageJson.dependencies || {}, packageJson.devDependencies || {});
46
+
47
+ // 筛选出@jdtaro相关的包
48
+ return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@jdtaro/'));
49
+ }
50
+ } catch (error) {
51
+ // 记录错误但不中断程序执行(添加trycatch)
52
+ console.error('读取或解析package.json时发生错误:', error instanceof Error ? error.message : String(error));
53
+ }
54
+
55
+ return [];
56
+ }
57
+
58
+ async function info(options, ctx) {
59
+ let npmPackages = ctx.helper.UPDATE_PACKAGE_LIST.concat(['react', 'react-native', 'expo', 'taro-ui']);
60
+
61
+ // 调用新函数获取@jdtaro相关包
62
+ const jdtaroPackages = getJdtaroPackages(ctx);
63
+ npmPackages = npmPackages.concat(jdtaroPackages);
64
+
65
+ const info = await envinfo.run(
66
+ Object.assign(
67
+ {},
68
+ {
69
+ System: ['OS', 'Shell'],
70
+ Binaries: ['Node', 'Yarn', 'npm'],
71
+ npmPackages,
72
+ npmGlobalPackages: ['typescript'],
73
+ },
74
+ options,
75
+ ),
76
+ {
77
+ title: `Taro CLI ${getPkgVersion()} environment info`,
78
+ },
79
+ );
80
+ console.log(info);
81
+ }