@cloudbase/cli 2.0.12 → 2.0.13

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 (227) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/lib/run/service/deployPackage.js +2 -9
  3. package/lib/utils/debug-logger.js +30 -0
  4. package/lib/utils/net/cloud-api-request.js +7 -1
  5. package/lib/utils/net/http-request.js +16 -5
  6. package/package.json +1 -1
  7. package/types/utils/debug-logger.d.ts +1 -0
  8. package/types/utils/net/http-request.d.ts +6 -2
  9. package/src/auth/index.ts +0 -2
  10. package/src/auth/login.ts +0 -91
  11. package/src/auth/logout.ts +0 -7
  12. package/src/commands/account/index.ts +0 -2
  13. package/src/commands/account/login.ts +0 -192
  14. package/src/commands/account/logout.ts +0 -24
  15. package/src/commands/common.ts +0 -246
  16. package/src/commands/env/base.ts +0 -90
  17. package/src/commands/env/create.ts +0 -92
  18. package/src/commands/env/domain.ts +0 -186
  19. package/src/commands/env/index.ts +0 -4
  20. package/src/commands/env/login.ts +0 -235
  21. package/src/commands/framework/index.ts +0 -124
  22. package/src/commands/functions/alias/getRoute.ts +0 -76
  23. package/src/commands/functions/alias/index.ts +0 -2
  24. package/src/commands/functions/alias/setRoute.ts +0 -82
  25. package/src/commands/functions/code-download.ts +0 -100
  26. package/src/commands/functions/code-update.ts +0 -62
  27. package/src/commands/functions/concurrency/delete.ts +0 -45
  28. package/src/commands/functions/concurrency/index.ts +0 -3
  29. package/src/commands/functions/concurrency/list.ts +0 -58
  30. package/src/commands/functions/concurrency/set.ts +0 -47
  31. package/src/commands/functions/config-update.ts +0 -76
  32. package/src/commands/functions/copy.ts +0 -62
  33. package/src/commands/functions/delete.ts +0 -79
  34. package/src/commands/functions/deploy.ts +0 -293
  35. package/src/commands/functions/detail.ts +0 -138
  36. package/src/commands/functions/index.ts +0 -16
  37. package/src/commands/functions/invoke.ts +0 -121
  38. package/src/commands/functions/layer/bind.ts +0 -182
  39. package/src/commands/functions/layer/common.ts +0 -8
  40. package/src/commands/functions/layer/create.ts +0 -49
  41. package/src/commands/functions/layer/delete.ts +0 -73
  42. package/src/commands/functions/layer/download.ts +0 -92
  43. package/src/commands/functions/layer/index.ts +0 -7
  44. package/src/commands/functions/layer/list.ts +0 -94
  45. package/src/commands/functions/layer/sort.ts +0 -76
  46. package/src/commands/functions/list.ts +0 -68
  47. package/src/commands/functions/log.ts +0 -148
  48. package/src/commands/functions/run.ts +0 -249
  49. package/src/commands/functions/trigger-create.ts +0 -79
  50. package/src/commands/functions/trigger-delete.ts +0 -105
  51. package/src/commands/functions/version/index.ts +0 -2
  52. package/src/commands/functions/version/list.ts +0 -73
  53. package/src/commands/functions/version/publish.ts +0 -43
  54. package/src/commands/gateway/create.ts +0 -109
  55. package/src/commands/gateway/delete.ts +0 -81
  56. package/src/commands/gateway/domain.ts +0 -159
  57. package/src/commands/gateway/index.ts +0 -5
  58. package/src/commands/gateway/list.ts +0 -76
  59. package/src/commands/gateway/switch.ts +0 -107
  60. package/src/commands/helpers/index.ts +0 -3
  61. package/src/commands/helpers/init.ts +0 -431
  62. package/src/commands/helpers/new.ts +0 -117
  63. package/src/commands/helpers/open.ts +0 -67
  64. package/src/commands/hosting/hosting.ts +0 -360
  65. package/src/commands/hosting/index.ts +0 -1
  66. package/src/commands/index.ts +0 -13
  67. package/src/commands/lowcode/app.ts +0 -34
  68. package/src/commands/lowcode/comps.ts +0 -325
  69. package/src/commands/lowcode/index.ts +0 -2
  70. package/src/commands/lowcode/utils.ts +0 -25
  71. package/src/commands/run/create.ts +0 -153
  72. package/src/commands/run/delete.ts +0 -77
  73. package/src/commands/run/image/common.ts +0 -8
  74. package/src/commands/run/image/delete.ts +0 -68
  75. package/src/commands/run/image/download.ts +0 -68
  76. package/src/commands/run/image/index.ts +0 -5
  77. package/src/commands/run/image/list.ts +0 -84
  78. package/src/commands/run/image/upload.ts +0 -76
  79. package/src/commands/run/index.ts +0 -7
  80. package/src/commands/run/list.ts +0 -83
  81. package/src/commands/run/service/config.ts +0 -81
  82. package/src/commands/run/service/create.ts +0 -122
  83. package/src/commands/run/service/deploy.ts +0 -125
  84. package/src/commands/run/service/index.ts +0 -5
  85. package/src/commands/run/service/list.ts +0 -94
  86. package/src/commands/run/service/update.ts +0 -108
  87. package/src/commands/run/standalonegateway/common.ts +0 -8
  88. package/src/commands/run/standalonegateway/create.ts +0 -85
  89. package/src/commands/run/standalonegateway/destroy.ts +0 -59
  90. package/src/commands/run/standalonegateway/index.ts +0 -5
  91. package/src/commands/run/standalonegateway/list.ts +0 -53
  92. package/src/commands/run/standalonegateway/package.ts +0 -62
  93. package/src/commands/run/standalonegateway/turn.ts +0 -63
  94. package/src/commands/run/version/common.ts +0 -8
  95. package/src/commands/run/version/create.ts +0 -369
  96. package/src/commands/run/version/delete.ts +0 -64
  97. package/src/commands/run/version/index.ts +0 -5
  98. package/src/commands/run/version/list.ts +0 -91
  99. package/src/commands/run/version/modify.ts +0 -137
  100. package/src/commands/run/version/update.ts +0 -398
  101. package/src/commands/smart.ts +0 -132
  102. package/src/commands/storage/index.ts +0 -1
  103. package/src/commands/storage/storage.ts +0 -464
  104. package/src/commands/third/index.ts +0 -1
  105. package/src/commands/third/thirdAttach.ts +0 -49
  106. package/src/completion/index.ts +0 -13
  107. package/src/constant.ts +0 -154
  108. package/src/decorators/captureError.ts +0 -25
  109. package/src/decorators/constants.ts +0 -12
  110. package/src/decorators/deprecate.ts +0 -25
  111. package/src/decorators/guard.ts +0 -42
  112. package/src/decorators/index.ts +0 -7
  113. package/src/decorators/injectParams.ts +0 -54
  114. package/src/decorators/params/common.ts +0 -28
  115. package/src/decorators/params/index.ts +0 -35
  116. package/src/env/domain.ts +0 -33
  117. package/src/env/index.ts +0 -63
  118. package/src/env/login.ts +0 -80
  119. package/src/error.ts +0 -36
  120. package/src/function/alias.ts +0 -43
  121. package/src/function/base.ts +0 -253
  122. package/src/function/code.ts +0 -55
  123. package/src/function/concurrency.ts +0 -57
  124. package/src/function/create.ts +0 -78
  125. package/src/function/delete.ts +0 -42
  126. package/src/function/index.ts +0 -11
  127. package/src/function/layer/attach.ts +0 -68
  128. package/src/function/layer/create.ts +0 -63
  129. package/src/function/layer/delete.ts +0 -21
  130. package/src/function/layer/download.ts +0 -54
  131. package/src/function/layer/index.ts +0 -7
  132. package/src/function/layer/list.ts +0 -32
  133. package/src/function/layer/sort.ts +0 -24
  134. package/src/function/trigger.ts +0 -97
  135. package/src/function/update.ts +0 -35
  136. package/src/function/version.ts +0 -39
  137. package/src/function/vpc.ts +0 -22
  138. package/src/gateway/index.ts +0 -137
  139. package/src/help.ts +0 -50
  140. package/src/hosting.ts +0 -212
  141. package/src/index.ts +0 -13
  142. package/src/logger.ts +0 -17
  143. package/src/run/create.ts +0 -23
  144. package/src/run/delete.ts +0 -15
  145. package/src/run/image/build.ts +0 -36
  146. package/src/run/image/delete.ts +0 -14
  147. package/src/run/image/index.ts +0 -4
  148. package/src/run/image/info.ts +0 -27
  149. package/src/run/image/list.ts +0 -23
  150. package/src/run/index.ts +0 -8
  151. package/src/run/list.ts +0 -30
  152. package/src/run/repo.ts +0 -25
  153. package/src/run/service/common.ts +0 -311
  154. package/src/run/service/config.ts +0 -77
  155. package/src/run/service/create.ts +0 -52
  156. package/src/run/service/deployPackage.ts +0 -65
  157. package/src/run/service/index.ts +0 -7
  158. package/src/run/service/list.ts +0 -29
  159. package/src/run/service/showLogs.ts +0 -98
  160. package/src/run/service/update.ts +0 -82
  161. package/src/run/standalonegateway/create.ts +0 -24
  162. package/src/run/standalonegateway/destroy.ts +0 -19
  163. package/src/run/standalonegateway/index.ts +0 -5
  164. package/src/run/standalonegateway/list.ts +0 -74
  165. package/src/run/standalonegateway/package/index.ts +0 -1
  166. package/src/run/standalonegateway/package/list.ts +0 -24
  167. package/src/run/standalonegateway/turn/index.ts +0 -2
  168. package/src/run/standalonegateway/turn/off.ts +0 -19
  169. package/src/run/standalonegateway/turn/on.ts +0 -19
  170. package/src/run/version/create.ts +0 -68
  171. package/src/run/version/delete.ts +0 -16
  172. package/src/run/version/index.ts +0 -6
  173. package/src/run/version/list.ts +0 -17
  174. package/src/run/version/modify.ts +0 -17
  175. package/src/run/version/repo.ts +0 -28
  176. package/src/run/version/update.ts +0 -59
  177. package/src/storage.ts +0 -114
  178. package/src/third/index.ts +0 -13
  179. package/src/types.ts +0 -672
  180. package/src/utils/auth.ts +0 -15
  181. package/src/utils/checkTcbrEnv.ts +0 -69
  182. package/src/utils/cli-table.ts +0 -23
  183. package/src/utils/commonParamsCheck.ts +0 -65
  184. package/src/utils/config.ts +0 -39
  185. package/src/utils/env.ts +0 -244
  186. package/src/utils/fs/del.ts +0 -5
  187. package/src/utils/fs/index.ts +0 -71
  188. package/src/utils/function-packer.ts +0 -97
  189. package/src/utils/index.ts +0 -25
  190. package/src/utils/log.ts +0 -82
  191. package/src/utils/net/cloud-api-request.ts +0 -62
  192. package/src/utils/net/credential.ts +0 -53
  193. package/src/utils/net/http-request.ts +0 -84
  194. package/src/utils/net/index.ts +0 -5
  195. package/src/utils/net/manager-service.ts +0 -36
  196. package/src/utils/net/proxy.ts +0 -6
  197. package/src/utils/notice.ts +0 -28
  198. package/src/utils/output/highlight.ts +0 -5
  199. package/src/utils/output/index.ts +0 -3
  200. package/src/utils/output/link.ts +0 -10
  201. package/src/utils/output/loading.ts +0 -82
  202. package/src/utils/parallel.ts +0 -82
  203. package/src/utils/platform/index.ts +0 -3
  204. package/src/utils/platform/mac.ts +0 -21
  205. package/src/utils/platform/os.ts +0 -64
  206. package/src/utils/platform/port.ts +0 -10
  207. package/src/utils/progress-bar.ts +0 -38
  208. package/src/utils/prompt/index.ts +0 -1
  209. package/src/utils/prompt/select.ts +0 -60
  210. package/src/utils/reporter/agree.ts +0 -20
  211. package/src/utils/reporter/download.ts +0 -26
  212. package/src/utils/reporter/index.ts +0 -3
  213. package/src/utils/reporter/usage.ts +0 -20
  214. package/src/utils/store/auth.ts +0 -49
  215. package/src/utils/store/common.ts +0 -8
  216. package/src/utils/store/db.ts +0 -68
  217. package/src/utils/store/index.ts +0 -5
  218. package/src/utils/store/usage.ts +0 -12
  219. package/src/utils/tcbrApi/callTcbrApi.ts +0 -28
  220. package/src/utils/tcbrApi/index.ts +0 -1
  221. package/src/utils/template.ts +0 -170
  222. package/src/utils/tools/encoding.ts +0 -8
  223. package/src/utils/tools/index.ts +0 -4
  224. package/src/utils/tools/object.ts +0 -33
  225. package/src/utils/tools/time.ts +0 -38
  226. package/src/utils/tools/uid.ts +0 -19
  227. package/src/utils/validator.ts +0 -65
@@ -1,69 +0,0 @@
1
- import chalk from 'chalk'
2
- import { CloudBaseError } from '../error'
3
- import { CloudApiService } from './net'
4
- import { EnumEnvCheck } from '../constant'
5
- import { callTcbrApi } from './tcbrApi'
6
-
7
- const oldCmdSet =
8
- `
9
- 服务列表:tcb run:deprecated list --envId <envId>
10
- 创建服务:tcb run:deprecated create --envId <envId> --name <name>
11
- 删除服务:tcb run:deprecated delete --envId <envId> --serviceName <serviceName>
12
-
13
- 版本列表:tcb run:deprecated version list --envId <envId> --serviceName <serviceName>
14
- 创建版本:tcb run:deprecated version create --envId <envId> --serviceName <serviceName>
15
- 分配流量:tcb run:deprecated version modify --envId <envId> --serviceName <serviceName>
16
- 滚动更新:tcb run:deprecated version update --envId <envId> --serviceName <serviceName> --versionName <versionName>
17
- 删除版本:tcb run:deprecated version delete --envId <envId> --serviceName <serviceName> --versionName <versionName>
18
-
19
- 查看镜像:tcb run:deprecated image list --envId <envId> --serviceName <serviceName>
20
- 上传镜像:tcb run:deprecated image upload --envId <envId> --serviceName <serviceName> --imageId <imageId> --imageTag <imageTag>
21
- 下载镜像:tcb run:deprecated image download --envId <envId> --serviceName <serviceName> --imageTag <imageTag>
22
- 删除镜像:tcb run:deprecated image delete --envId <envId> --serviceName <serviceName> --imageTag <imageTag>
23
- `
24
- const newCmdSet =
25
- `
26
- 查看环境下服务:tcb run service:list --envId <envId>
27
- 创建云托管服务:tcb run service:create --envId <envId> --serviceName <serviceName> --containerPort <containerPort>
28
- 更新云托管服务:tcb run service:update --envId <envId> --serviceName <serviceName> --containerPort <containerPort>
29
- 部署云托管服务:tcb run deploy --envId <envId> --serviceName <serviceName> --containerPort <containerPort>
30
- 更新服务基础配置:tcb run service:config --envId <envId> --serviceName <serviceName>
31
- `
32
- /**
33
- *
34
- * @param envId 环境 Id
35
- * @param isTcbr 使用的命令是否是 tcbr 新操作集
36
- * @returns
37
- */
38
- export async function checkTcbrEnv(envId: string | undefined, isTcbr: boolean): Promise<EnumEnvCheck> | never {
39
- if (envId === undefined) {
40
- throw new CloudBaseError('请使用 -e 或 --envId 指定环境 ID')
41
- }
42
- const { data: res } = await callTcbrApi('DescribeCloudRunEnvs', {
43
- EnvId: envId
44
- })
45
- const { EnvList } = res
46
- const envInfo = EnvList?.find((item: { EnvId: string }) => item?.EnvId === envId)
47
-
48
- if (envInfo === undefined) {
49
- throw new CloudBaseError(`无法读取到有效的环境信息,请检查环境 ID 是否正确\nrequestId: ${res?.RequestId}`)
50
- }
51
-
52
- if ((envInfo.EnvType === 'tcbr' && isTcbr) || (envInfo.EnvType !== 'tcbr' && !isTcbr)) {
53
- return EnumEnvCheck.EnvFit
54
- } else if (envInfo.EnvType === 'tcbr' && !isTcbr) {
55
- return EnumEnvCheck.EnvNewCmdOld
56
- } else if (envInfo.EnvType !== 'tcbr' && isTcbr) {
57
- return EnumEnvCheck.EnvOldCmdNew
58
- }
59
- }
60
-
61
- export function logEnvCheck(envId: string, warningType: EnumEnvCheck) {
62
- if (warningType === EnumEnvCheck.EnvNewCmdOld) {
63
- // 当前环境是 tcbr 环境且使用的不是 tcbr 新操作集
64
- throw new CloudBaseError(`当前能力不支持 ${envId} 环境,请使用如下操作集:${chalk.grey(newCmdSet)}`)
65
- } else if (warningType === EnumEnvCheck.EnvOldCmdNew) {
66
- // 当前环境不是 tcbr 环境但使用 tcbr 操作集
67
- throw new CloudBaseError(`当前能力不支持 ${envId} 环境,请使用如下操作集:${chalk.grey(oldCmdSet)}`)
68
- }
69
- }
@@ -1,23 +0,0 @@
1
- import Table, { TableOptions, HorizontalTable } from 'cli-table3'
2
-
3
- // 打印水平方向的表格
4
- export function printHorizontalTable(
5
- head: string[],
6
- data: (string | number)[][] = [],
7
- options?: TableOptions
8
- ) {
9
- if (!data?.length) {
10
- console.log('列表数据为空')
11
- }
12
- const table: HorizontalTable = new Table({
13
- head,
14
- style: { head: ['yellow'] },
15
- colAligns: new Array(head.length).fill('center'),
16
- ...options
17
- }) as HorizontalTable
18
-
19
- data.forEach((item: Table.Cell[]) => {
20
- table.push(item)
21
- })
22
- console.log(table.toString())
23
- }
@@ -1,65 +0,0 @@
1
- import { CloudBaseError } from '../error'
2
- import { convertNumber } from '../run'
3
- import { ITcbrServiceOptionalOptions, ITcbrServiceConvertedOptionalOptions } from '../types'
4
- import { validateCpuMem } from './validator'
5
-
6
- export function parseOptionalParams(options: ITcbrServiceOptionalOptions): ITcbrServiceConvertedOptionalOptions {
7
- let cpuConverted
8
- let memConverted
9
- if (options.cpu || options.mem) {
10
- let data = validateCpuMem(options.cpu, options.mem)
11
- ;[cpuConverted, memConverted] = [data.cpuOutput, data.memOutput]
12
- }
13
-
14
- let maxNumConverted
15
- if (options.maxNum) {
16
- maxNumConverted = convertNumber(options.maxNum)
17
- if (maxNumConverted < 0 || maxNumConverted > 50) {
18
- throw new CloudBaseError('最大副本数必须大于等于0且小于等于50')
19
- }
20
- }
21
-
22
- let minNumConverted
23
- if (options.minNum) {
24
- minNumConverted = convertNumber(options.minNum)
25
- if (minNumConverted < 0 || minNumConverted > 50) {
26
- throw new CloudBaseError('最小副本数必须大于等于0且小于等于50')
27
- }
28
- }
29
-
30
- if (minNumConverted > maxNumConverted) {
31
- throw new CloudBaseError('最小副本数不能大于最大副本数')
32
- }
33
-
34
- return {
35
- cpuConverted,
36
- memConverted,
37
- maxNumConverted,
38
- minNumConverted
39
- }
40
- }
41
-
42
- /**
43
- *
44
- * @description 通用两层三元运算符的参数处理,例如
45
- * MaxNum: maxNumConverted
46
- ? convertNumber(maxNum)
47
- : _override
48
- ? (previousServerConfig?.MaxNum)
49
- : 50
50
- * @param originalParam 原始参数,如 maxNumConverted
51
- * @param override 是否覆盖,如 _override
52
- * @param handler 处理参数的函数,如 convertNumber
53
- * @param overrideVal 如果覆盖,提供的默认覆盖值
54
- * @param defaultVal 默认值
55
- * @param args 传入 handler 中使用的额外参数
56
- */
57
- export function parseInputParam(originalParam, override: boolean, handler: Function | null, overrideVal, defaultVal, ...args) {
58
- return originalParam
59
- ? (typeof handler === 'function')
60
- ? handler(originalParam, ...args)
61
- : originalParam
62
- : override
63
- ? overrideVal
64
- : defaultVal
65
- }
@@ -1,39 +0,0 @@
1
- import _ from 'lodash'
2
- import path from 'path'
3
- import yargs, { Arguments } from 'yargs'
4
- import { ConfigParser, ICloudBaseConfig } from '@cloudbase/toolbox'
5
-
6
- export interface IArgs {
7
- envId: string
8
- region: string
9
- verbose: boolean
10
- configPath: string
11
- [x: string]: unknown
12
- }
13
-
14
- export const getArgs = (): Arguments<IArgs> => {
15
- // console.log(yargs.argv)
16
- return yargs.alias('e', 'envId').alias('r', 'region').argv as any
17
- }
18
-
19
- // 获取 cloudbase 配置
20
- export const getCloudBaseConfig = async (configPath?: string): Promise<ICloudBaseConfig> => {
21
- const args = getArgs()
22
-
23
- let specificConfigPath = configPath || args.configPath
24
- specificConfigPath = specificConfigPath ? path.resolve(specificConfigPath) : undefined
25
-
26
- const parser = new ConfigParser({
27
- configPath: specificConfigPath
28
- })
29
- const config: ICloudBaseConfig = await parser.get()
30
-
31
- // 合并默认配置
32
- if (config?.functionDefaultConfig && config?.functions?.length) {
33
- config.functions = config.functions.map((rawConfig) =>
34
- _.merge({}, config.functionDefaultConfig, rawConfig)
35
- )
36
- }
37
-
38
- return config
39
- }
package/src/utils/env.ts DELETED
@@ -1,244 +0,0 @@
1
- import _ from 'lodash'
2
- import open from 'open'
3
- import { prompt } from 'enquirer'
4
- import { getDataFromWeb, isCamRefused } from '@cloudbase/toolbox'
5
- import { CloudBaseError } from '../error'
6
- import { listEnvs, getEnvInfo } from '../env'
7
- import { ENV_STATUS, STATUS_TEXT } from '../constant'
8
- import { CloudApiService, getMangerService } from './net'
9
- import { execWithLoading } from './output'
10
- import { logger } from './log'
11
-
12
- const tcbService = CloudApiService.getInstance('tcb')
13
-
14
- const ENV_INIT_TIP = '环境初始化中,预计需要三分钟'
15
- const CREATE_ENV = 'CREATE'
16
- const consoleUrl = 'https://console.cloud.tencent.com/tcb/env/index?action=CreateEnv&from=cli'
17
-
18
- /**
19
- * 展示环境列表选择器,获取用户选择的环境
20
- */
21
- export async function getSelectedEnv(inputEnvId?: string) {
22
- // 检查是否开通 TCB 服务
23
- const isInitNow = await checkTcbService()
24
-
25
- let envData = []
26
-
27
- // 刚初始化服务,新创建的环境还未就绪
28
- if (isInitNow) {
29
- envData = await execWithLoading(
30
- () => {
31
- // 等待用户完成创建环境的流程
32
- return new Promise((resolve) => {
33
- const timer = setInterval(async () => {
34
- const envs = await listEnvs()
35
- if (envs.length) {
36
- clearInterval(timer)
37
- resolve(envs)
38
- }
39
- }, 2000)
40
- })
41
- },
42
- {
43
- startTip: '获取环境列表中'
44
- }
45
- )
46
- } else {
47
- // 选择环境
48
- envData = await execWithLoading(() => listEnvs(), {
49
- startTip: '获取环境列表中'
50
- })
51
- }
52
-
53
- envData = envData || []
54
-
55
- // 检查输入的环境 Id 是否属于用户
56
- if (envData?.length && inputEnvId) {
57
- const inputEnvIdExist = envData.find((_) => _.EnvId === inputEnvId)
58
- if (!inputEnvIdExist) {
59
- throw new CloudBaseError(`你指定的环境 Id ${inputEnvId} 不存在,请指定正确的环境 Id!`)
60
- }
61
- return inputEnvId
62
- }
63
-
64
- // 按名称排序,构造展示数据
65
- const envs: { name: string; value: string }[] = envData
66
- .map((item) => {
67
- let name = `${item.Alias} - [${item.EnvId}:${item.PackageName || '按量计费'}]`
68
- if (item.Status !== ENV_STATUS.NORMAL) {
69
- name += `(${STATUS_TEXT[item.Status]})`
70
- }
71
-
72
- return {
73
- name,
74
- value: item.EnvId
75
- }
76
- })
77
- .sort((prev, next) => prev.value.charCodeAt(0) - next.value.charCodeAt(0))
78
-
79
- const choices = [
80
- ...envs,
81
- {
82
- name: envData.length ? '创建新环境' : '无可用环境,创建新环境',
83
- value: CREATE_ENV
84
- }
85
- ]
86
-
87
- let { env } = await prompt<any>({
88
- choices,
89
- type: 'select',
90
- name: 'env',
91
- message: '请选择关联环境',
92
- result(choice) {
93
- return this.map(choice)[choice]
94
- }
95
- })
96
-
97
- // 创建新环境
98
- if (env === CREATE_ENV) {
99
- logger.success('已打开控制台,请前往控制台创建环境')
100
- // 从控制台获取创建环境生成的 envId
101
- const { envId } = await getDataFromWeb((port) => `${consoleUrl}&port=${port}`, 'getData')
102
- if (!envId) {
103
- throw new CloudBaseError('接收环境 Id 信息失败,请重新运行 init 命令!')
104
- }
105
- logger.success(`创建环境成功,环境 Id: ${envId}`)
106
- env = envId
107
- }
108
-
109
- // 检查环境状态
110
- await checkEnvStatus(env)
111
-
112
- return env
113
- }
114
-
115
- // 选择地域
116
- export async function getSelectRegion() {
117
- const { region } = await prompt<any>({
118
- choices: [
119
- {
120
- name: '上海',
121
- value: 'ap-shanghai'
122
- },
123
- {
124
- name: '广州',
125
- value: 'ap-guangzhou'
126
- }
127
- ],
128
- type: 'select',
129
- name: 'region',
130
- message: '请选择环境所在地域',
131
- result(choice) {
132
- return this.map(choice)[choice]
133
- }
134
- })
135
-
136
- tcbService.region = region
137
-
138
- return region
139
- }
140
-
141
- // 检查 TCB 服务是否开通
142
- export async function checkTcbService(): Promise<boolean> {
143
- const app = await getMangerService()
144
- let Initialized
145
- try {
146
- Initialized = (await app.env.checkTcbService()).Initialized
147
- } catch (e) {
148
- // 忽略 CAM 权限问题
149
- if (!isCamRefused(e)) {
150
- throw e
151
- }
152
- }
153
-
154
- if (!Initialized) {
155
- const { jump } = await prompt<any>({
156
- type: 'confirm',
157
- name: 'jump',
158
- message:
159
- '你还没有开通云开发服务,是否跳转到控制台开通云开发服务?(取消将无法继续操作)',
160
- initial: true
161
- })
162
-
163
- if (!jump) {
164
- throw new CloudBaseError('init 操作终止,请开通云开发服务后再进行操作!')
165
- }
166
-
167
- // 打开控制台
168
- open(consoleUrl)
169
- logger.success('已打开云开发控制台,请登录并在云开发控制台中开通服务!')
170
-
171
- await execWithLoading(() => waitForServiceEnable(), {
172
- startTip: '等待云开发服务开通中',
173
- successTip: '云开发服务开通成功!'
174
- })
175
-
176
- // 返回一个是否刚初始化服务的标志
177
- return true
178
- }
179
-
180
- return false
181
- }
182
-
183
- // 检查环境的状态,是否可以正常使用
184
- export async function checkEnvStatus(envId: string) {
185
- const env = await getEnvInfo(envId)
186
- if (env.Status === ENV_STATUS.UNAVAILABLE) {
187
- await checkEnvAvaliable(envId)
188
- } else if (env.Status !== ENV_STATUS.NORMAL) {
189
- throw new CloudBaseError('所有环境状态异常')
190
- }
191
- }
192
-
193
- // 检测环境是否可用
194
- export async function checkEnvAvaliable(envId: string) {
195
- let count = 0
196
-
197
- await execWithLoading(
198
- (flush) => {
199
- const increase = setInterval(() => {
200
- flush(`${ENV_INIT_TIP} ${++count}S`)
201
- }, 1000)
202
-
203
- return new Promise<void>((resolve) => {
204
- const timer = setInterval(async () => {
205
- const env = await getEnvInfo(envId)
206
- // 环境初始化中
207
- if (env.Status === ENV_STATUS.NORMAL) {
208
- clearInterval(timer)
209
- clearInterval(increase)
210
- resolve()
211
- }
212
- }, 3000)
213
- })
214
- },
215
- {
216
- startTip: ENV_INIT_TIP,
217
- successTip: `环境 ${envId} 初始化成功`
218
- }
219
- )
220
- }
221
-
222
- // 等待服务开通
223
- async function waitForServiceEnable() {
224
- return new Promise<void>((resolve) => {
225
- const timer = setInterval(async () => {
226
- const app = await getMangerService()
227
- try {
228
- const { Initialized } = await app.env.checkTcbService()
229
- if (Initialized) {
230
- clearInterval(timer)
231
- setTimeout(() => {
232
- // 服务初始化完成到环境创建完成有一定的延迟时间,延迟 5S 返回
233
- resolve()
234
- }, 5000)
235
- }
236
- } catch (e) {
237
- // 忽略 CAM 权限问题
238
- if (!isCamRefused(e)) {
239
- throw e
240
- }
241
- }
242
- }, 3000)
243
- })
244
- }
@@ -1,5 +0,0 @@
1
- import del from 'del'
2
-
3
- export function delSync(patterns: string | readonly string[]) {
4
- del.sync(patterns, { force: true })
5
- }
@@ -1,71 +0,0 @@
1
- import fs from 'fs'
2
- import { CloudBaseError } from '../../error'
3
-
4
- export * from './del'
5
-
6
- export type SizeUnit = 'KB' | 'MB' | 'GB'
7
-
8
- // 检查路径是否可以访问(读、写)
9
- export function checkFullAccess(dest: string, throwError = false): boolean {
10
- try {
11
- // 可见、可写
12
- fs.accessSync(dest, fs.constants.F_OK)
13
- fs.accessSync(dest, fs.constants.W_OK)
14
- fs.accessSync(dest, fs.constants.R_OK)
15
- return true
16
- } catch (e) {
17
- if (throwError) {
18
- throw new CloudBaseError(`路径不存在或没有权限访问:${dest}`)
19
- } else {
20
- return false
21
- }
22
- }
23
- }
24
-
25
- // 检查路径是否可以读
26
- export function checkWritable(dest: string, throwError = false): boolean {
27
- try {
28
- // 可见、可写
29
- fs.accessSync(dest, fs.constants.F_OK)
30
- fs.accessSync(dest, fs.constants.W_OK)
31
- return true
32
- } catch (e) {
33
- if (throwError) {
34
- throw new CloudBaseError(`路径不存在或没有权限访问:${dest}`)
35
- } else {
36
- return false
37
- }
38
- }
39
- }
40
-
41
- // 检查路径是否可以写
42
- export function checkReadable(dest: string, throwError = false): boolean {
43
- try {
44
- // 可见、可读
45
- fs.accessSync(dest, fs.constants.F_OK)
46
- fs.accessSync(dest, fs.constants.R_OK)
47
- return true
48
- } catch (e) {
49
- if (throwError) {
50
- throw new CloudBaseError(`路径不存在或没有权限访问:${dest}`)
51
- } else {
52
- return false
53
- }
54
- }
55
- }
56
-
57
- export function isDirectory(dest: string) {
58
- checkFullAccess(dest, true)
59
- return fs.statSync(dest).isDirectory()
60
- }
61
-
62
- export function formateFileSize(size: number | string, unit: SizeUnit) {
63
- const numSize = Number(size)
64
- const unitMap = {
65
- KB: 1024,
66
- MB: Math.pow(1024, 2),
67
- GB: Math.pow(1024, 3)
68
- }
69
-
70
- return Number(numSize / unitMap[unit]).toFixed(2)
71
- }
@@ -1,97 +0,0 @@
1
- import fs from 'fs'
2
- import del from 'del'
3
- import path from 'path'
4
- import makeDir from 'make-dir'
5
- import { random } from './tools'
6
- import { CloudBaseError, zipDir, checkFullAccess } from '@cloudbase/toolbox'
7
-
8
- export enum CodeType {
9
- File,
10
- JavaFile
11
- }
12
-
13
- /**
14
- * 将函数代码转换成 Base64 编码
15
- * 普通文件:Node,PHP
16
- * Java 文件:Jar,ZIP
17
- */
18
- export class FunctionPacker {
19
- // 项目根目录
20
- root: string
21
- // 函数名
22
- name: string
23
- // 代码文件类型
24
- type: CodeType
25
- funcPath: string
26
- funcDistPath: string
27
- // 临时目录
28
- tmpPath: string
29
- // 忽略文件模式
30
- ignore: string | string[]
31
-
32
- constructor(root: string, name: string, ignore: string | string[]) {
33
- this.name = name
34
- this.root = root
35
- this.ignore = ignore
36
- this.funcPath = path.join(root, name)
37
- }
38
-
39
- async getFileCode() {
40
- checkFullAccess(this.funcPath, true)
41
- // 构建临时文件夹存放函数打包结果
42
- this.tmpPath = path.join(this.root, `.cloudbase_tmp_${random()}`)
43
- this.funcDistPath = path.join(this.tmpPath, this.name)
44
- // 生成存放 zip 文件的文件夹
45
- await makeDir(this.funcDistPath)
46
- const zipPath = path.resolve(this.funcDistPath, 'dist.zip')
47
- // 生成 zip 文件
48
-
49
- await zipDir(this.funcPath, zipPath, this.ignore)
50
- // 将 zip 文件转换成 base64
51
- const base64 = fs.readFileSync(zipPath).toString('base64')
52
- // 清除打包文件
53
- this.clean()
54
- return base64
55
- }
56
-
57
- // 获取 Java 代码
58
- getJavaFileCode() {
59
- const { funcPath } = this
60
- // Java 代码为 jar 或 zip 包
61
- const jarExist = checkFullAccess(`${funcPath}.jar`)
62
- const zipExist = checkFullAccess(`${funcPath}.zip`)
63
- if (!jarExist && !zipExist) {
64
- return null
65
- }
66
- const packagePath = jarExist ? `${funcPath}.jar` : `${funcPath}.zip`
67
- return fs.readFileSync(packagePath).toString('base64')
68
- }
69
-
70
- async build(type: CodeType) {
71
- if (type === CodeType.JavaFile) {
72
- try {
73
- const code = await this.getJavaFileCode()
74
- return code
75
- } catch (error) {
76
- this.clean()
77
- throw new CloudBaseError(`函数代码打包失败:\n ${error.message}`)
78
- }
79
- }
80
-
81
- if (type === CodeType.File) {
82
- try {
83
- const code = await this.getFileCode()
84
- return code
85
- } catch (error) {
86
- this.clean()
87
- throw new CloudBaseError(`函数代码打包失败:\n ${error.message}`)
88
- }
89
- }
90
- }
91
-
92
- clean() {
93
- // allow deleting the current working directory and outside
94
- this.funcDistPath && del.sync([this.funcDistPath], { force: true })
95
- this.tmpPath && del.sync([this.tmpPath], { force: true })
96
- }
97
- }
@@ -1,25 +0,0 @@
1
- export * from './net'
2
-
3
- export * from './fs'
4
- export * from './tools'
5
- export * from './output'
6
- export * from './platform'
7
- export * from './reporter'
8
- export * from './cli-table'
9
- export * from './progress-bar'
10
- export * from './function-packer'
11
- export * from './prompt'
12
-
13
- export * from './env'
14
- export * from './log'
15
- export * from './template'
16
-
17
- export * from './config'
18
- export * from './auth'
19
- export * from './store'
20
- export * from './notice'
21
- export * from './parallel'
22
-
23
- export * from './tcbrApi'
24
- export * from './checkTcbrEnv'
25
- export * from './commonParamsCheck'