@ohos-ports/tarojs-cli 0.0.0-stage → 4.3.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/LICENSE +174 -0
  2. package/README.md +0 -3
  3. package/bin/taro +7 -0
  4. package/dist/cli.d.ts +6 -0
  5. package/dist/cli.js +252 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/commands/customCommand.d.ts +5 -0
  8. package/dist/commands/customCommand.js +23 -0
  9. package/dist/commands/customCommand.js.map +1 -0
  10. package/dist/config/index.d.ts +8 -0
  11. package/dist/config/index.js +10 -0
  12. package/dist/config/index.js.map +1 -0
  13. package/dist/config/packagesManagement.d.ts +19 -0
  14. package/dist/config/packagesManagement.js +22 -0
  15. package/dist/config/packagesManagement.js.map +1 -0
  16. package/dist/create/constants.d.ts +1 -0
  17. package/dist/create/constants.js +5 -0
  18. package/dist/create/constants.js.map +1 -0
  19. package/dist/create/creator.d.ts +9 -0
  20. package/dist/create/creator.js +31 -0
  21. package/dist/create/creator.js.map +1 -0
  22. package/dist/create/fetchTemplate.d.ts +8 -0
  23. package/dist/create/fetchTemplate.js +137 -0
  24. package/dist/create/fetchTemplate.js.map +1 -0
  25. package/dist/create/page.d.ts +65 -0
  26. package/dist/create/page.js +252 -0
  27. package/dist/create/page.js.map +1 -0
  28. package/dist/create/plugin.d.ts +16 -0
  29. package/dist/create/plugin.js +48 -0
  30. package/dist/create/plugin.js.map +1 -0
  31. package/dist/create/project.d.ts +80 -0
  32. package/dist/create/project.js +519 -0
  33. package/dist/create/project.js.map +1 -0
  34. package/dist/doctor/index.d.ts +4 -0
  35. package/dist/doctor/index.js +32 -0
  36. package/dist/doctor/index.js.map +1 -0
  37. package/dist/index.d.ts +16 -0
  38. package/dist/index.js +21 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/presets/commands/build.d.ts +3 -0
  41. package/dist/presets/commands/build.js +239 -0
  42. package/dist/presets/commands/build.js.map +1 -0
  43. package/dist/presets/commands/config.d.ts +3 -0
  44. package/dist/presets/commands/config.js +88 -0
  45. package/dist/presets/commands/config.js.map +1 -0
  46. package/dist/presets/commands/create.d.ts +3 -0
  47. package/dist/presets/commands/create.js +91 -0
  48. package/dist/presets/commands/create.js.map +1 -0
  49. package/dist/presets/commands/global-config.d.ts +3 -0
  50. package/dist/presets/commands/global-config.js +139 -0
  51. package/dist/presets/commands/global-config.js.map +1 -0
  52. package/dist/presets/commands/help.d.ts +3 -0
  53. package/dist/presets/commands/help.js +29 -0
  54. package/dist/presets/commands/help.js.map +1 -0
  55. package/dist/presets/commands/info.d.ts +3 -0
  56. package/dist/presets/commands/info.js +79 -0
  57. package/dist/presets/commands/info.js.map +1 -0
  58. package/dist/presets/commands/init.d.ts +3 -0
  59. package/dist/presets/commands/init.js +59 -0
  60. package/dist/presets/commands/init.js.map +1 -0
  61. package/dist/presets/commands/inspect.d.ts +3 -0
  62. package/dist/presets/commands/inspect.js +118 -0
  63. package/dist/presets/commands/inspect.js.map +1 -0
  64. package/dist/presets/commands/update.d.ts +3 -0
  65. package/dist/presets/commands/update.js +171 -0
  66. package/dist/presets/commands/update.js.map +1 -0
  67. package/dist/presets/constant/hooks.d.ts +13 -0
  68. package/dist/presets/constant/hooks.js +17 -0
  69. package/dist/presets/constant/hooks.js.map +1 -0
  70. package/dist/presets/constant/index.d.ts +1 -0
  71. package/dist/presets/constant/index.js +18 -0
  72. package/dist/presets/constant/index.js.map +1 -0
  73. package/dist/presets/files/generateFrameworkInfo.d.ts +3 -0
  74. package/dist/presets/files/generateFrameworkInfo.js +30 -0
  75. package/dist/presets/files/generateFrameworkInfo.js.map +1 -0
  76. package/dist/presets/files/generateProjectConfig.d.ts +3 -0
  77. package/dist/presets/files/generateProjectConfig.js +40 -0
  78. package/dist/presets/files/generateProjectConfig.js.map +1 -0
  79. package/dist/presets/files/writeFileToDist.d.ts +3 -0
  80. package/dist/presets/files/writeFileToDist.js +17 -0
  81. package/dist/presets/files/writeFileToDist.js.map +1 -0
  82. package/dist/presets/hooks/build.d.ts +3 -0
  83. package/dist/presets/hooks/build.js +22 -0
  84. package/dist/presets/hooks/build.js.map +1 -0
  85. package/dist/presets/hooks/create.d.ts +3 -0
  86. package/dist/presets/hooks/create.js +11 -0
  87. package/dist/presets/hooks/create.js.map +1 -0
  88. package/dist/presets/index.d.ts +4 -0
  89. package/dist/presets/index.js +17 -0
  90. package/dist/presets/index.js.map +1 -0
  91. package/dist/presets/platforms/plugin.d.ts +3 -0
  92. package/dist/presets/platforms/plugin.js +76 -0
  93. package/dist/presets/platforms/plugin.js.map +1 -0
  94. package/dist/presets/platforms/rn.d.ts +3 -0
  95. package/dist/presets/platforms/rn.js +101 -0
  96. package/dist/presets/platforms/rn.js.map +1 -0
  97. package/dist/util/appConfig.d.ts +11 -0
  98. package/dist/util/appConfig.js +25 -0
  99. package/dist/util/appConfig.js.map +1 -0
  100. package/dist/util/createPage.d.ts +9 -0
  101. package/dist/util/createPage.js +117 -0
  102. package/dist/util/createPage.js.map +1 -0
  103. package/dist/util/defineConfig.d.ts +17 -0
  104. package/dist/util/defineConfig.js +12 -0
  105. package/dist/util/defineConfig.js.map +1 -0
  106. package/dist/util/index.d.ts +23 -0
  107. package/dist/util/index.js +125 -0
  108. package/dist/util/index.js.map +1 -0
  109. package/dist/util/types.d.ts +58 -0
  110. package/dist/util/types.js +3 -0
  111. package/dist/util/types.js.map +1 -0
  112. package/global.d.ts +4 -0
  113. package/index.js +3 -0
  114. package/package.json +76 -4
  115. package/postinstall.js +14 -0
  116. package/src/__tests__/__mocks__/presets.ts +15 -0
  117. package/src/__tests__/build-config.spec.ts +99 -0
  118. package/src/__tests__/cli.spec.ts +233 -0
  119. package/src/__tests__/config.spec.ts +232 -0
  120. package/src/__tests__/doctor-config.spec.ts +805 -0
  121. package/src/__tests__/doctor-eslint.spec.ts +31 -0
  122. package/src/__tests__/doctor-recommand.spec.ts +158 -0
  123. package/src/__tests__/doctor.spec.ts +77 -0
  124. package/src/__tests__/dotenv-parse.spec.ts +105 -0
  125. package/src/__tests__/env/.env +2 -0
  126. package/src/__tests__/fixtures/default/.env +3 -0
  127. package/src/__tests__/fixtures/default/.env.development +1 -0
  128. package/src/__tests__/fixtures/default/.env.local +1 -0
  129. package/src/__tests__/fixtures/default/.env.pre +3 -0
  130. package/src/__tests__/fixtures/default/.env.production +1 -0
  131. package/src/__tests__/fixtures/default/.env.uat +5 -0
  132. package/src/__tests__/fixtures/default/.env.uat.local +1 -0
  133. package/src/__tests__/fixtures/default/babel.config.js +10 -0
  134. package/src/__tests__/fixtures/default/config/dev.js +9 -0
  135. package/src/__tests__/fixtures/default/config/index.js +84 -0
  136. package/src/__tests__/fixtures/default/config/prod.js +18 -0
  137. package/src/__tests__/fixtures/default/package.json +38 -0
  138. package/src/__tests__/fixtures/default/src/app.config.js +11 -0
  139. package/src/__tests__/fixtures/default/src/app.js +16 -0
  140. package/src/__tests__/fixtures/default/src/app.scss +0 -0
  141. package/src/__tests__/fixtures/default/src/index.html +19 -0
  142. package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
  143. package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
  144. package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
  145. package/src/__tests__/info.spec.ts +74 -0
  146. package/src/__tests__/inspect.spec.ts +163 -0
  147. package/src/__tests__/project.spec.ts +304 -0
  148. package/src/__tests__/update.spec.ts +324 -0
  149. package/src/__tests__/utils/index.ts +54 -0
  150. package/src/cli.ts +250 -0
  151. package/src/commands/customCommand.ts +26 -0
  152. package/src/config/index.ts +7 -0
  153. package/src/config/manifest.default.json +35 -0
  154. package/src/config/packagesManagement.ts +20 -0
  155. package/src/config/rn-stylelint.json +5 -0
  156. package/src/config/tsconfig.json +24 -0
  157. package/src/create/constants.ts +1 -0
  158. package/src/create/creator.ts +37 -0
  159. package/src/create/fetchTemplate.ts +152 -0
  160. package/src/create/page.ts +299 -0
  161. package/src/create/plugin.ts +55 -0
  162. package/src/create/project.ts +591 -0
  163. package/src/doctor/index.ts +27 -0
  164. package/src/index.ts +24 -0
  165. package/src/presets/commands/build.ts +227 -0
  166. package/src/presets/commands/config.ts +86 -0
  167. package/src/presets/commands/create.ts +105 -0
  168. package/src/presets/commands/global-config.ts +140 -0
  169. package/src/presets/commands/help.ts +19 -0
  170. package/src/presets/commands/info.ts +77 -0
  171. package/src/presets/commands/init.ts +66 -0
  172. package/src/presets/commands/inspect.ts +138 -0
  173. package/src/presets/commands/update.ts +179 -0
  174. package/src/presets/constant/hooks.ts +14 -0
  175. package/src/presets/constant/index.ts +1 -0
  176. package/src/presets/files/generateFrameworkInfo.ts +30 -0
  177. package/src/presets/files/generateProjectConfig.ts +41 -0
  178. package/src/presets/files/writeFileToDist.ts +17 -0
  179. package/src/presets/hooks/build.ts +22 -0
  180. package/src/presets/hooks/create.ts +11 -0
  181. package/src/presets/index.ts +15 -0
  182. package/src/presets/platforms/plugin.ts +81 -0
  183. package/src/presets/platforms/rn.ts +113 -0
  184. package/src/util/appConfig.ts +29 -0
  185. package/src/util/createPage.ts +132 -0
  186. package/src/util/defineConfig.ts +22 -0
  187. package/src/util/index.ts +127 -0
  188. package/src/util/types.ts +81 -0
  189. package/templates/default/.husky/commit-msg +4 -0
  190. package/templates/default/_editorconfig +12 -0
  191. package/templates/default/_env.development +2 -0
  192. package/templates/default/_env.production +1 -0
  193. package/templates/default/_env.test +1 -0
  194. package/templates/default/_eslintrc +11 -0
  195. package/templates/default/_gitignore +8 -0
  196. package/templates/default/babel.config.js +14 -0
  197. package/templates/default/commitlint.config.mjs +1 -0
  198. package/templates/default/config/dev.js +10 -0
  199. package/templates/default/config/index.js +106 -0
  200. package/templates/default/config/prod.js +46 -0
  201. package/templates/default/package.json.tmpl +158 -0
  202. package/templates/default/project.config.json +15 -0
  203. package/templates/default/src/app.config.js +11 -0
  204. package/templates/default/src/app.css +0 -0
  205. package/templates/default/src/app.js +44 -0
  206. package/templates/default/src/index.html +17 -0
  207. package/templates/default/src/pages/index/index.config.js +3 -0
  208. package/templates/default/src/pages/index/index.css +0 -0
  209. package/templates/default/src/pages/index/index.jsx +15 -0
  210. package/templates/default/src/pages/index/index.vue +23 -0
  211. package/templates/default/stylelint.config.mjs +4 -0
  212. package/templates/default/template_creator.js +77 -0
  213. package/templates/default/tsconfig.json +32 -0
  214. package/templates/default/types/global.d.ts +35 -0
  215. package/templates/default/types/solid.d.ts +12 -0
  216. package/templates/default/types/vue.d.ts +10 -0
  217. package/templates/global-config/index.json +4 -0
  218. package/templates/global-config/package.json +5 -0
  219. package/templates/plugin-compile/README.md +32 -0
  220. package/templates/plugin-compile/index.js +3 -0
  221. package/templates/plugin-compile/package.json.tmpl +43 -0
  222. package/templates/plugin-compile/src/index.ts +153 -0
  223. package/templates/plugin-compile/tsconfig.json +29 -0
@@ -0,0 +1,66 @@
1
+ import type { IPluginContext } from '@tarojs/service'
2
+
3
+ export default (ctx: IPluginContext) => {
4
+ ctx.registerCommand({
5
+ name: 'init',
6
+ optionsMap: {
7
+ '--name [name]': '项目名称',
8
+ '--description [description]': '项目介绍',
9
+ '--typescript': '使用TypeScript',
10
+ '--build-es5': '是否需要编译为es5',
11
+ '--npm [npm]': '包管理工具',
12
+ '--template-source [templateSource]': '项目模板源',
13
+ '--clone [clone]': '拉取远程模板时使用git clone',
14
+ '--template [template]': '项目模板',
15
+ '--css [css]': 'CSS预处理器(sass/less/stylus/none)',
16
+ '--autoInstall': '自动安装依赖',
17
+ '--platforms [platforms]': '需要支持的平台,多个用逗号分隔(如 weapp,h5)',
18
+ '-h, --help': 'output usage information'
19
+ },
20
+ async fn (opts) {
21
+ // init project
22
+ const { appPath } = ctx.paths
23
+ const {
24
+ projectName,
25
+ templateSource,
26
+ clone,
27
+ template,
28
+ description,
29
+ typescript,
30
+ buildEs5,
31
+ css,
32
+ npm,
33
+ framework,
34
+ compiler,
35
+ hideDefaultTemplate,
36
+ sourceRoot,
37
+ autoInstall,
38
+ platforms,
39
+ ask
40
+ } = opts.options
41
+
42
+ const Project = require('../../create/project').default
43
+ const project = new Project({
44
+ sourceRoot,
45
+ projectName,
46
+ projectDir: appPath,
47
+ npm,
48
+ templateSource,
49
+ clone,
50
+ template,
51
+ description,
52
+ typescript,
53
+ buildEs5,
54
+ framework,
55
+ compiler,
56
+ hideDefaultTemplate,
57
+ autoInstall,
58
+ css,
59
+ platforms,
60
+ ask
61
+ })
62
+
63
+ project.create()
64
+ }
65
+ })
66
+ }
@@ -0,0 +1,138 @@
1
+ import * as path from 'node:path'
2
+
3
+ import {
4
+ ENTRY,
5
+ OUTPUT_DIR,
6
+ resolveScriptPath,
7
+ SOURCE_DIR
8
+ } from '@tarojs/helper'
9
+ import { getPlatformType } from '@tarojs/shared'
10
+
11
+ import * as hooks from '../constant'
12
+
13
+ import type { IPluginContext } from '@tarojs/service'
14
+
15
+ export default (ctx: IPluginContext) => {
16
+ ctx.registerCommand({
17
+ name: 'inspect',
18
+ optionsMap: {
19
+ '-t, --type [typeName]': 'Build type, weapp/swan/alipay/tt/h5/quickapp/rn/qq/jd',
20
+ '-o, --output [outputPath]': 'output config to outputPath'
21
+ },
22
+ synopsisList: [
23
+ 'taro inspect --type weapp',
24
+ 'taro inspect --type weapp --output inspect.config.js',
25
+ 'taro inspect --type weapp plugins',
26
+ 'taro inspect --type weapp module.rules.0'
27
+ ],
28
+ async fn ({ _, options }) {
29
+ const { fs, chalk } = ctx.helper
30
+ const platform = options.type || options.t
31
+
32
+ verifyIsTaroProject(ctx)
33
+ verifyPlatform(platform, chalk)
34
+
35
+ const configName = ctx.platforms.get(platform)?.useConfigName || ''
36
+ process.env.TARO_ENV = platform
37
+ process.env.TARO_PLATFORM = getPlatformType(platform, configName)
38
+
39
+ let config = getConfig(ctx, platform)
40
+ config = {
41
+ ...config,
42
+ ...config[configName]
43
+ }
44
+ delete config.mini
45
+ delete config.h5
46
+
47
+ const isProduction = process.env.NODE_ENV === 'production'
48
+ const outputPath = options.output || options.o
49
+ const mode = outputPath ? 'output' : 'console'
50
+ const extractPath = _[1]
51
+
52
+ await ctx.applyPlugins({
53
+ name: platform,
54
+ opts: {
55
+ config: {
56
+ ...config,
57
+ isWatch: !isProduction,
58
+ mode: isProduction ? 'production' : 'development',
59
+ async modifyWebpackChain (chain, webpack, data) {
60
+ await ctx.applyPlugins({
61
+ name: hooks.MODIFY_WEBPACK_CHAIN,
62
+ initialVal: chain,
63
+ opts: {
64
+ chain,
65
+ webpack,
66
+ data
67
+ }
68
+ })
69
+ },
70
+ onWebpackChainReady (chain) {
71
+ const webpackConfig = chain.toConfig()
72
+ const { toString } = chain.constructor
73
+ const config = extractConfig(webpackConfig, extractPath)
74
+ const res = toString(config)
75
+
76
+ if (mode === 'console') {
77
+ const highlight = require('cli-highlight').default
78
+ console.info(highlight(res, { language: 'js' }))
79
+ } else if (mode === 'output' && outputPath) {
80
+ fs.writeFileSync(outputPath, res)
81
+ }
82
+
83
+ process.exit(0)
84
+ }
85
+ }
86
+ }
87
+ })
88
+ }
89
+ })
90
+ }
91
+
92
+ /** 是否 Taro 项目根路径 */
93
+ function verifyIsTaroProject (ctx: IPluginContext) {
94
+ const { fs, chalk, PROJECT_CONFIG } = ctx.helper
95
+ const { configPath } = ctx.paths
96
+
97
+ if (!configPath || !fs.existsSync(configPath)) {
98
+ console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`))
99
+ process.exit(1)
100
+ }
101
+ }
102
+
103
+ /** 检查平台类型 */
104
+ function verifyPlatform (platform, chalk) {
105
+ if (typeof platform !== 'string') {
106
+ console.log(chalk.red('请传入正确的编译类型!'))
107
+ process.exit(0)
108
+ }
109
+ }
110
+
111
+ /** 整理 config */
112
+ function getConfig (ctx: IPluginContext, platform: string) {
113
+ const { initialConfig } = ctx
114
+ const sourceDirName = initialConfig.sourceRoot || SOURCE_DIR
115
+ const outputDirName = initialConfig.outputRoot || OUTPUT_DIR
116
+ const sourceDir = path.join(ctx.appPath, sourceDirName)
117
+ const entryFilePath = resolveScriptPath(path.join(sourceDir, ENTRY))
118
+
119
+ const entry = {
120
+ [ENTRY]: [entryFilePath]
121
+ }
122
+
123
+ return {
124
+ ...initialConfig,
125
+ entry,
126
+ sourceRoot: sourceDirName,
127
+ outputRoot: outputDirName,
128
+ platform
129
+ }
130
+ }
131
+
132
+ /** 按路径取出 webpackConfig 内的对应值 */
133
+ function extractConfig (webpackConfig, extractPath: string | undefined) {
134
+ if (!extractPath) return webpackConfig
135
+
136
+ const list = extractPath.split('.')
137
+ return list.reduce((config, current) => config[current], webpackConfig)
138
+ }
@@ -0,0 +1,179 @@
1
+ import * as path from 'node:path'
2
+
3
+ import * as inquirer from 'inquirer'
4
+ import * as getLatestVersion from 'latest-version'
5
+ import * as ora from 'ora'
6
+ import * as semver from 'semver'
7
+
8
+ import packagesManagement from '../../config/packagesManagement'
9
+ import { execCommand, getPkgItemByKey } from '../../util'
10
+
11
+ import type { IPluginContext } from '@tarojs/service'
12
+
13
+ export default (ctx: IPluginContext) => {
14
+ ctx.registerCommand({
15
+ name: 'update',
16
+ synopsisList: [
17
+ 'taro update self [version]',
18
+ 'taro update project [version]'
19
+ ],
20
+ optionsMap: {
21
+ '--npm [npm]': '包管理工具',
22
+ '-h, --help': 'output usage information'
23
+ },
24
+ async fn ({ _, options }) {
25
+ const { npm } = options
26
+ const [, updateType, version] = _ as [string, ('self' | 'project')?, string?]
27
+ const { appPath, configPath } = ctx.paths
28
+ const {
29
+ chalk,
30
+ fs,
31
+ PROJECT_CONFIG,
32
+ UPDATE_PACKAGE_LIST
33
+ } = ctx.helper
34
+
35
+ const pkgPath = path.join(appPath, 'package.json')
36
+ const pkgName = getPkgItemByKey('name')
37
+ const conf = {
38
+ npm: null
39
+ }
40
+ const prompts: Record<string, unknown>[] = []
41
+
42
+ async function getTargetVersion () {
43
+ let targetTaroVersion
44
+
45
+ if (version) {
46
+ targetTaroVersion = semver.clean(version)
47
+ } else {
48
+ try {
49
+ targetTaroVersion = await getLatestVersion(pkgName, {
50
+ version: 'latest'
51
+ })
52
+ } catch (e) {
53
+ targetTaroVersion = await getLatestVersion(pkgName)
54
+ }
55
+ }
56
+ if (!semver.valid(targetTaroVersion)) {
57
+ console.log(chalk.red('命令错误:无效的 version ~'))
58
+ throw Error('无效的 version!')
59
+ }
60
+ return targetTaroVersion
61
+ }
62
+
63
+ function execUpdate (command: string, version: string, isSelf = false) {
64
+ const updateTarget = isSelf ? ' CLI ' : ' Taro 项目依赖'
65
+ const spinString = `正在更新${updateTarget}到 v${version} ...`
66
+ const spinner = ora(spinString).start()
67
+ execCommand({
68
+ command,
69
+ successCallback (data) {
70
+ spinner.stop()
71
+ console.log(data.replace(/\n$/, ''))
72
+ },
73
+ failCallback (data) {
74
+ spinner.stop()
75
+ spinner.warn(data.replace(/\n$/, ''))
76
+ }
77
+ })
78
+ }
79
+
80
+ /** 更新全局的 Taro CLI */
81
+ async function updateSelf () {
82
+ const spinner = ora('正在获取最新版本信息...').start()
83
+ const targetTaroVersion = await getTargetVersion()
84
+ spinner.stop()
85
+ console.log(chalk.green(`Taro 最新版本:${targetTaroVersion}\n`))
86
+
87
+ askNpm(conf, prompts)
88
+ const answers = npm ? { npm } : await inquirer.prompt(prompts)
89
+
90
+ const command = `${packagesManagement[answers.npm].globalCommand}@${targetTaroVersion}`
91
+ execUpdate(command, targetTaroVersion, true)
92
+ }
93
+
94
+ /** 更新当前项目中的 Taro 相关依赖 */
95
+ async function updateProject () {
96
+ if (!configPath || !fs.existsSync(configPath)) {
97
+ console.log(chalk.red(`找不到项目配置文件 ${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`))
98
+ process.exit(1)
99
+ }
100
+ const packageMap = require(pkgPath)
101
+
102
+ const spinner = ora('正在获取最新版本信息...').start()
103
+
104
+ const version = await getTargetVersion()
105
+
106
+ spinner.stop()
107
+
108
+ const oldVersion = packageMap.dependencies['@tarojs/taro']
109
+ // 更新 @tarojs/* 版本和 NervJS 版本
110
+ Object.keys(packageMap.dependencies || {}).forEach((key) => {
111
+ if (UPDATE_PACKAGE_LIST.indexOf(key) !== -1) {
112
+ packageMap.dependencies[key] = version
113
+ }
114
+ })
115
+ Object.keys(packageMap.devDependencies || {}).forEach((key) => {
116
+ if (UPDATE_PACKAGE_LIST.indexOf(key) !== -1) {
117
+ packageMap.devDependencies[key] = version
118
+ }
119
+ })
120
+
121
+ // 写入package.json
122
+ try {
123
+ await fs.writeJson(pkgPath, packageMap, { spaces: '\t' })
124
+ console.log(chalk.green(`项目当前 Taro 版本:${oldVersion},Taro 最新版本:${version},更新项目 package.json 成功!`))
125
+ console.log()
126
+ } catch (err) {
127
+ console.error(err)
128
+ }
129
+
130
+ askNpm(conf, prompts)
131
+ const answers = npm ? { npm } : await inquirer.prompt(prompts)
132
+
133
+ const command = packagesManagement[answers.npm].command
134
+
135
+ execUpdate(command, version)
136
+ }
137
+
138
+ function askNpm (conf, prompts) {
139
+ const packages = [
140
+ {
141
+ name: 'yarn',
142
+ value: 'yarn'
143
+ },
144
+ {
145
+ name: 'pnpm',
146
+ value: 'pnpm'
147
+ },
148
+ {
149
+ name: 'npm',
150
+ value: 'npm'
151
+ },
152
+ {
153
+ name: 'cnpm',
154
+ value: 'cnpm'
155
+ }
156
+ ]
157
+
158
+ if ((typeof conf.npm as string | undefined) !== 'string') {
159
+ prompts.push({
160
+ type: 'list',
161
+ name: 'npm',
162
+ message: '请选择包管理工具',
163
+ choices: packages
164
+ })
165
+ }
166
+ }
167
+
168
+ if (updateType === 'self') return updateSelf()
169
+
170
+ if (updateType === 'project') return updateProject()
171
+
172
+ console.log(chalk.red('命令错误:'))
173
+ console.log(`${chalk.green(
174
+ 'taro update self [version]')} 更新 Taro 开发工具 taro-cli 到指定版本或 Taro3 的最新版本`)
175
+ console.log(`${chalk.green(
176
+ 'taro update project [version]')} 更新项目所有 Taro 相关依赖到指定版本或 Taro3 的最新版本`)
177
+ }
178
+ })
179
+ }
@@ -0,0 +1,14 @@
1
+ export const MODIFY_APP_CONFIG = 'modifyAppConfig'
2
+ export const MODIFY_WEBPACK_CHAIN = 'modifyWebpackChain'
3
+ export const MODIFY_VITE_CONFIG = 'modifyViteConfig'
4
+ export const MODIFY_BUILD_ASSETS = 'modifyBuildAssets'
5
+ export const MODIFY_MINI_CONFIGS = 'modifyMiniConfigs'
6
+ export const MODIFY_COMPONENT_CONFIG = 'modifyComponentConfig'
7
+ export const MODIFY_RUNNER_OPTS = 'modifyRunnerOpts'
8
+ export const ON_COMPILER_MAKE = 'onCompilerMake'
9
+ export const ON_PARSE_CREATE_ELEMENT = 'onParseCreateElement'
10
+ export const ON_BUILD_START = 'onBuildStart'
11
+ export const ON_BUILD_FINISH = 'onBuildFinish'
12
+ export const ON_BUILD_COMPLETE = 'onBuildComplete'
13
+
14
+ export const MODIFY_CREATE_TEMPLATE = 'modifyCreateTemplate'
@@ -0,0 +1 @@
1
+ export * from './hooks'
@@ -0,0 +1,30 @@
1
+ import { getPkgVersion } from '../../util'
2
+
3
+ import type { IPluginContext } from '@tarojs/service'
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ ctx.registerMethod('generateFrameworkInfo', () => {
7
+ const { getInstalledNpmPkgVersion, processTypeEnum, printLog, chalk } = ctx.helper
8
+ const { nodeModulesPath } = ctx.paths
9
+ const { date, outputRoot } = ctx.initialConfig
10
+ const frameworkInfoFileName = '.frameworkinfo'
11
+ const frameworkName = '@tarojs/runtime'
12
+ const frameworkVersion = getInstalledNpmPkgVersion(frameworkName, nodeModulesPath)
13
+
14
+ if (frameworkVersion) {
15
+ const frameworkinfo = {
16
+ toolName: 'Taro',
17
+ toolCliVersion: getPkgVersion(),
18
+ toolFrameworkVersion: frameworkVersion,
19
+ createTime: date ? new Date(date).getTime() : Date.now()
20
+ }
21
+ ctx.writeFileToDist({
22
+ filePath: frameworkInfoFileName,
23
+ content: JSON.stringify(frameworkinfo, null, 2)
24
+ })
25
+ printLog(processTypeEnum.GENERATE, '框架信息', `${outputRoot}/${frameworkInfoFileName}`)
26
+ } else {
27
+ printLog(processTypeEnum.WARNING, '依赖安装', chalk.red(`项目依赖 ${frameworkName} 未安装,或安装有误!`))
28
+ }
29
+ })
30
+ }
@@ -0,0 +1,41 @@
1
+ import * as path from 'node:path'
2
+
3
+ import type { IPluginContext } from '@tarojs/service'
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ ctx.registerMethod('generateProjectConfig', ({ srcConfigName, distConfigName }) => {
7
+ // 混合模式不需要生成项目配置
8
+ const { blended, newBlended } = ctx.runOpts
9
+ if (blended || newBlended) return
10
+
11
+ const { appPath, sourcePath, outputPath } = ctx.paths
12
+ const { printLog, processTypeEnum, fs } = ctx.helper
13
+ // 生成 project.config.json
14
+ const projectConfigFileName = srcConfigName
15
+ let projectConfigPath = path.join(appPath, projectConfigFileName)
16
+ if (!fs.existsSync(projectConfigPath)) {
17
+ // 若项目根目录不存在对应平台的 projectConfig 文件,则尝试从源代码目录查找
18
+ projectConfigPath = path.join(sourcePath, projectConfigFileName)
19
+ if (!fs.existsSync(projectConfigPath)) return
20
+ }
21
+
22
+ const origProjectConfig = fs.readJSONSync(projectConfigPath)
23
+ // 优先从环境变量中获取 appid, 以应对多环境appid不同的情况
24
+ origProjectConfig.appid = process.env.TARO_APP_ID || origProjectConfig.appid
25
+
26
+ // compileType 是 plugin 时不修改 miniprogramRoot 字段
27
+ let distProjectConfig = origProjectConfig
28
+ if (origProjectConfig.compileType !== 'plugin') {
29
+ distProjectConfig = Object.assign({}, origProjectConfig, { miniprogramRoot: './' })
30
+ }
31
+ ctx.writeFileToDist({
32
+ filePath: distConfigName,
33
+ content: JSON.stringify(distProjectConfig, null, 2)
34
+ })
35
+
36
+ if (ctx.initialConfig.logger?.quiet === false) {
37
+ printLog(processTypeEnum.REMIND, 'appid', `${origProjectConfig.appid}`)
38
+ printLog(processTypeEnum.GENERATE, '工具配置', `${outputPath}/${distConfigName}`)
39
+ }
40
+ })
41
+ }
@@ -0,0 +1,17 @@
1
+ import * as path from 'node:path'
2
+
3
+ import type { IPluginContext } from '@tarojs/service'
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ ctx.registerMethod('writeFileToDist', ({ filePath, content }) => {
7
+ const { outputPath } = ctx.paths
8
+ const { printLog, processTypeEnum, fs } = ctx.helper
9
+ if (path.isAbsolute(filePath)) {
10
+ printLog(processTypeEnum.ERROR, 'ctx.writeFileToDist 不能接受绝对路径')
11
+ return
12
+ }
13
+ const absFilePath = path.join(outputPath, filePath)
14
+ fs.ensureDirSync(path.dirname(absFilePath))
15
+ fs.writeFileSync(absFilePath, content)
16
+ })
17
+ }
@@ -0,0 +1,22 @@
1
+ import * as hooks from '../constant'
2
+
3
+ import type { IPluginContext } from '@tarojs/service'
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ [
7
+ hooks.MODIFY_APP_CONFIG,
8
+ hooks.MODIFY_WEBPACK_CHAIN,
9
+ hooks.MODIFY_VITE_CONFIG,
10
+ hooks.MODIFY_BUILD_ASSETS,
11
+ hooks.MODIFY_MINI_CONFIGS,
12
+ hooks.MODIFY_COMPONENT_CONFIG,
13
+ hooks.ON_COMPILER_MAKE,
14
+ hooks.ON_PARSE_CREATE_ELEMENT,
15
+ hooks.ON_BUILD_START,
16
+ hooks.ON_BUILD_FINISH,
17
+ hooks.ON_BUILD_COMPLETE,
18
+ hooks.MODIFY_RUNNER_OPTS
19
+ ].forEach(methodName => {
20
+ ctx.registerMethod(methodName)
21
+ })
22
+ }
@@ -0,0 +1,11 @@
1
+ import * as hooks from '../constant'
2
+
3
+ import type { IPluginContext } from '@tarojs/service'
4
+
5
+ export default (ctx: IPluginContext) => {
6
+ [
7
+ hooks.MODIFY_CREATE_TEMPLATE,
8
+ ].forEach(methodName => {
9
+ ctx.registerMethod(methodName)
10
+ })
11
+ }
@@ -0,0 +1,15 @@
1
+ import * as path from 'node:path'
2
+
3
+ export default () => {
4
+ return {
5
+ plugins: [
6
+ // hooks
7
+ path.resolve(__dirname, 'hooks', 'build.js'),
8
+ path.resolve(__dirname, 'hooks', 'create.js'),
9
+ // 兼容其他平台小程序插件
10
+ path.resolve(__dirname, 'files', 'writeFileToDist.js'),
11
+ path.resolve(__dirname, 'files', 'generateProjectConfig.js'),
12
+ path.resolve(__dirname, 'files', 'generateFrameworkInfo.js')
13
+ ]
14
+ }
15
+ }
@@ -0,0 +1,81 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { fs } from '@tarojs/helper'
4
+ import { getPlatformType } from '@tarojs/shared'
5
+
6
+ import type { IPluginContext } from '@tarojs/service'
7
+
8
+ const configName = 'mini'
9
+ export default (ctx: IPluginContext) => {
10
+ ctx.registerPlatform({
11
+ name: 'plugin',
12
+ useConfigName: configName,
13
+ async fn ({ config }) {
14
+ const {
15
+ options,
16
+ _
17
+ } = ctx.runOpts
18
+ const { chalk, PLATFORMS } = ctx.helper
19
+ const { ASCF, WEAPP, ALIPAY, JD } = PLATFORMS
20
+ const typeMap = {
21
+ [ASCF]: 'ASCF',
22
+ [JD]: '京东',
23
+ [WEAPP]: '微信',
24
+ [ALIPAY]: '支付宝'
25
+ }
26
+ const { plugin, isWatch } = options
27
+ if (plugin !== ASCF && plugin !== WEAPP && plugin !== ALIPAY && plugin !== JD) {
28
+ console.log(chalk.red('目前插件编译仅支持 ASCF/微信/支付宝/京东 小程序!'))
29
+ return
30
+ }
31
+ console.log(chalk.green(`开始编译${typeMap[plugin]}小程序插件`))
32
+ async function buildPlugin (platform) {
33
+ process.env.TARO_ENV = platform
34
+ process.env.TARO_PLATFORM = getPlatformType(platform, configName)
35
+ await ctx.applyPlugins({
36
+ name: 'build',
37
+ opts: {
38
+ config: {
39
+ ...config,
40
+ isBuildPlugin: true,
41
+ isWatch,
42
+ outputRoot: `${config.outputRoot}/plugin`,
43
+ platform
44
+ },
45
+ options: Object.assign({}, options, {
46
+ platform
47
+ }),
48
+ _
49
+ }
50
+ })
51
+ await ctx.applyPlugins({
52
+ name: 'build',
53
+ opts: {
54
+ config: {
55
+ ...config,
56
+ isBuildPlugin: false,
57
+ isWatch,
58
+ outputRoot: `${config.outputRoot}/miniprogram`,
59
+ platform,
60
+ output: { ...(config.output || {}), clean: false }
61
+ },
62
+ options: Object.assign({}, options, {
63
+ platform
64
+ }),
65
+ _
66
+ }
67
+ })
68
+ }
69
+
70
+ await buildPlugin(plugin)
71
+
72
+ try {
73
+ const docSrcPath = path.join(process.cwd(), 'src/plugin/doc')
74
+ const docDestPath = path.join(process.cwd(), 'miniprogram/doc')
75
+ fs.copy(docSrcPath, docDestPath)
76
+ } catch (err) {
77
+ console.error('[@tarojs/cli] build plugin doc failed: ', err)
78
+ }
79
+ }
80
+ })
81
+ }