@cloudbase/cli 2.0.12 → 2.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 (277) hide show
  1. package/.history/.gitignore_20220624162613 +84 -0
  2. package/.history/.gitignore_20220627114054 +86 -0
  3. package/.history/package_20220711204706.json +88 -0
  4. package/.history/package_20220711204818.json +88 -0
  5. package/.history/package_20220712151717.json +88 -0
  6. package/.history/package_20220712212537.json +88 -0
  7. package/.history/package_20220722205058.json +88 -0
  8. package/.history/package_20220726110155.json +88 -0
  9. package/.history/package_20220726110201.json +88 -0
  10. package/.history/package_20220728110905.json +88 -0
  11. package/.history/package_20220728110936.json +88 -0
  12. package/.history/package_20220728111531.json +89 -0
  13. package/.history/package_20220728111810.json +89 -0
  14. package/.history/src/commands/lowcode/app_20220728111717.ts +52 -0
  15. package/{src/commands/lowcode/app.ts → .history/src/commands/lowcode/app_20220728112031.ts} +2 -2
  16. package/{src/index.ts → .history/src/index_20220624162613.ts} +0 -0
  17. package/.history/src/index_20220627113609.ts +15 -0
  18. package/.history/src/index_20220627114102.ts +13 -0
  19. package/.vscode/launch.json +10 -0
  20. package/bin/cloudbase.js +0 -0
  21. package/bin/tcb.js +5 -5
  22. package/lib/commands/account/login.js +3 -6
  23. package/lib/commands/env/index.js +0 -1
  24. package/lib/commands/lowcode/app.js +1 -1
  25. package/lib/commands/run/index.js +0 -1
  26. package/lib/commands/run/service/deploy.js +6 -1
  27. package/lib/commands/run/service/index.js +0 -1
  28. package/lib/commands/run/service/update.js +4 -0
  29. package/lib/constant.js +15 -1
  30. package/lib/env/index.js +1 -11
  31. package/lib/run/index.js +0 -1
  32. package/lib/run/service/common.js +20 -7
  33. package/lib/run/service/config.js +2 -3
  34. package/lib/run/service/deployPackage.js +2 -9
  35. package/lib/run/service/index.js +0 -1
  36. package/lib/run/service/update.js +1 -1
  37. package/lib/utils/debug-logger.js +30 -0
  38. package/lib/utils/net/cloud-api-request.js +7 -1
  39. package/lib/utils/net/http-request.js +16 -5
  40. package/package.json +2 -2
  41. package/types/commands/env/index.d.ts +0 -1
  42. package/types/commands/run/index.d.ts +0 -1
  43. package/types/commands/run/service/index.d.ts +0 -1
  44. package/types/env/index.d.ts +0 -6
  45. package/types/run/index.d.ts +0 -1
  46. package/types/run/service/common.d.ts +8 -1
  47. package/types/run/service/index.d.ts +0 -1
  48. package/types/types.d.ts +4 -0
  49. package/types/utils/debug-logger.d.ts +1 -0
  50. package/types/utils/net/http-request.d.ts +6 -2
  51. package/lib/commands/env/create.js +0 -115
  52. package/lib/commands/run/create.js +0 -162
  53. package/lib/commands/run/service/create.js +0 -144
  54. package/lib/run/create.js +0 -19
  55. package/lib/run/service/create.js +0 -67
  56. package/src/auth/index.ts +0 -2
  57. package/src/auth/login.ts +0 -91
  58. package/src/auth/logout.ts +0 -7
  59. package/src/commands/account/index.ts +0 -2
  60. package/src/commands/account/login.ts +0 -192
  61. package/src/commands/account/logout.ts +0 -24
  62. package/src/commands/common.ts +0 -246
  63. package/src/commands/env/base.ts +0 -90
  64. package/src/commands/env/create.ts +0 -92
  65. package/src/commands/env/domain.ts +0 -186
  66. package/src/commands/env/index.ts +0 -4
  67. package/src/commands/env/login.ts +0 -235
  68. package/src/commands/framework/index.ts +0 -124
  69. package/src/commands/functions/alias/getRoute.ts +0 -76
  70. package/src/commands/functions/alias/index.ts +0 -2
  71. package/src/commands/functions/alias/setRoute.ts +0 -82
  72. package/src/commands/functions/code-download.ts +0 -100
  73. package/src/commands/functions/code-update.ts +0 -62
  74. package/src/commands/functions/concurrency/delete.ts +0 -45
  75. package/src/commands/functions/concurrency/index.ts +0 -3
  76. package/src/commands/functions/concurrency/list.ts +0 -58
  77. package/src/commands/functions/concurrency/set.ts +0 -47
  78. package/src/commands/functions/config-update.ts +0 -76
  79. package/src/commands/functions/copy.ts +0 -62
  80. package/src/commands/functions/delete.ts +0 -79
  81. package/src/commands/functions/deploy.ts +0 -293
  82. package/src/commands/functions/detail.ts +0 -138
  83. package/src/commands/functions/index.ts +0 -16
  84. package/src/commands/functions/invoke.ts +0 -121
  85. package/src/commands/functions/layer/bind.ts +0 -182
  86. package/src/commands/functions/layer/common.ts +0 -8
  87. package/src/commands/functions/layer/create.ts +0 -49
  88. package/src/commands/functions/layer/delete.ts +0 -73
  89. package/src/commands/functions/layer/download.ts +0 -92
  90. package/src/commands/functions/layer/index.ts +0 -7
  91. package/src/commands/functions/layer/list.ts +0 -94
  92. package/src/commands/functions/layer/sort.ts +0 -76
  93. package/src/commands/functions/list.ts +0 -68
  94. package/src/commands/functions/log.ts +0 -148
  95. package/src/commands/functions/run.ts +0 -249
  96. package/src/commands/functions/trigger-create.ts +0 -79
  97. package/src/commands/functions/trigger-delete.ts +0 -105
  98. package/src/commands/functions/version/index.ts +0 -2
  99. package/src/commands/functions/version/list.ts +0 -73
  100. package/src/commands/functions/version/publish.ts +0 -43
  101. package/src/commands/gateway/create.ts +0 -109
  102. package/src/commands/gateway/delete.ts +0 -81
  103. package/src/commands/gateway/domain.ts +0 -159
  104. package/src/commands/gateway/index.ts +0 -5
  105. package/src/commands/gateway/list.ts +0 -76
  106. package/src/commands/gateway/switch.ts +0 -107
  107. package/src/commands/helpers/index.ts +0 -3
  108. package/src/commands/helpers/init.ts +0 -431
  109. package/src/commands/helpers/new.ts +0 -117
  110. package/src/commands/helpers/open.ts +0 -67
  111. package/src/commands/hosting/hosting.ts +0 -360
  112. package/src/commands/hosting/index.ts +0 -1
  113. package/src/commands/index.ts +0 -13
  114. package/src/commands/lowcode/comps.ts +0 -325
  115. package/src/commands/lowcode/index.ts +0 -2
  116. package/src/commands/lowcode/utils.ts +0 -25
  117. package/src/commands/run/create.ts +0 -153
  118. package/src/commands/run/delete.ts +0 -77
  119. package/src/commands/run/image/common.ts +0 -8
  120. package/src/commands/run/image/delete.ts +0 -68
  121. package/src/commands/run/image/download.ts +0 -68
  122. package/src/commands/run/image/index.ts +0 -5
  123. package/src/commands/run/image/list.ts +0 -84
  124. package/src/commands/run/image/upload.ts +0 -76
  125. package/src/commands/run/index.ts +0 -7
  126. package/src/commands/run/list.ts +0 -83
  127. package/src/commands/run/service/config.ts +0 -81
  128. package/src/commands/run/service/create.ts +0 -122
  129. package/src/commands/run/service/deploy.ts +0 -125
  130. package/src/commands/run/service/index.ts +0 -5
  131. package/src/commands/run/service/list.ts +0 -94
  132. package/src/commands/run/service/update.ts +0 -108
  133. package/src/commands/run/standalonegateway/common.ts +0 -8
  134. package/src/commands/run/standalonegateway/create.ts +0 -85
  135. package/src/commands/run/standalonegateway/destroy.ts +0 -59
  136. package/src/commands/run/standalonegateway/index.ts +0 -5
  137. package/src/commands/run/standalonegateway/list.ts +0 -53
  138. package/src/commands/run/standalonegateway/package.ts +0 -62
  139. package/src/commands/run/standalonegateway/turn.ts +0 -63
  140. package/src/commands/run/version/common.ts +0 -8
  141. package/src/commands/run/version/create.ts +0 -369
  142. package/src/commands/run/version/delete.ts +0 -64
  143. package/src/commands/run/version/index.ts +0 -5
  144. package/src/commands/run/version/list.ts +0 -91
  145. package/src/commands/run/version/modify.ts +0 -137
  146. package/src/commands/run/version/update.ts +0 -398
  147. package/src/commands/smart.ts +0 -132
  148. package/src/commands/storage/index.ts +0 -1
  149. package/src/commands/storage/storage.ts +0 -464
  150. package/src/commands/third/index.ts +0 -1
  151. package/src/commands/third/thirdAttach.ts +0 -49
  152. package/src/completion/index.ts +0 -13
  153. package/src/constant.ts +0 -154
  154. package/src/decorators/captureError.ts +0 -25
  155. package/src/decorators/constants.ts +0 -12
  156. package/src/decorators/deprecate.ts +0 -25
  157. package/src/decorators/guard.ts +0 -42
  158. package/src/decorators/index.ts +0 -7
  159. package/src/decorators/injectParams.ts +0 -54
  160. package/src/decorators/params/common.ts +0 -28
  161. package/src/decorators/params/index.ts +0 -35
  162. package/src/env/domain.ts +0 -33
  163. package/src/env/index.ts +0 -63
  164. package/src/env/login.ts +0 -80
  165. package/src/error.ts +0 -36
  166. package/src/function/alias.ts +0 -43
  167. package/src/function/base.ts +0 -253
  168. package/src/function/code.ts +0 -55
  169. package/src/function/concurrency.ts +0 -57
  170. package/src/function/create.ts +0 -78
  171. package/src/function/delete.ts +0 -42
  172. package/src/function/index.ts +0 -11
  173. package/src/function/layer/attach.ts +0 -68
  174. package/src/function/layer/create.ts +0 -63
  175. package/src/function/layer/delete.ts +0 -21
  176. package/src/function/layer/download.ts +0 -54
  177. package/src/function/layer/index.ts +0 -7
  178. package/src/function/layer/list.ts +0 -32
  179. package/src/function/layer/sort.ts +0 -24
  180. package/src/function/trigger.ts +0 -97
  181. package/src/function/update.ts +0 -35
  182. package/src/function/version.ts +0 -39
  183. package/src/function/vpc.ts +0 -22
  184. package/src/gateway/index.ts +0 -137
  185. package/src/help.ts +0 -50
  186. package/src/hosting.ts +0 -212
  187. package/src/logger.ts +0 -17
  188. package/src/run/create.ts +0 -23
  189. package/src/run/delete.ts +0 -15
  190. package/src/run/image/build.ts +0 -36
  191. package/src/run/image/delete.ts +0 -14
  192. package/src/run/image/index.ts +0 -4
  193. package/src/run/image/info.ts +0 -27
  194. package/src/run/image/list.ts +0 -23
  195. package/src/run/index.ts +0 -8
  196. package/src/run/list.ts +0 -30
  197. package/src/run/repo.ts +0 -25
  198. package/src/run/service/common.ts +0 -311
  199. package/src/run/service/config.ts +0 -77
  200. package/src/run/service/create.ts +0 -52
  201. package/src/run/service/deployPackage.ts +0 -65
  202. package/src/run/service/index.ts +0 -7
  203. package/src/run/service/list.ts +0 -29
  204. package/src/run/service/showLogs.ts +0 -98
  205. package/src/run/service/update.ts +0 -82
  206. package/src/run/standalonegateway/create.ts +0 -24
  207. package/src/run/standalonegateway/destroy.ts +0 -19
  208. package/src/run/standalonegateway/index.ts +0 -5
  209. package/src/run/standalonegateway/list.ts +0 -74
  210. package/src/run/standalonegateway/package/index.ts +0 -1
  211. package/src/run/standalonegateway/package/list.ts +0 -24
  212. package/src/run/standalonegateway/turn/index.ts +0 -2
  213. package/src/run/standalonegateway/turn/off.ts +0 -19
  214. package/src/run/standalonegateway/turn/on.ts +0 -19
  215. package/src/run/version/create.ts +0 -68
  216. package/src/run/version/delete.ts +0 -16
  217. package/src/run/version/index.ts +0 -6
  218. package/src/run/version/list.ts +0 -17
  219. package/src/run/version/modify.ts +0 -17
  220. package/src/run/version/repo.ts +0 -28
  221. package/src/run/version/update.ts +0 -59
  222. package/src/storage.ts +0 -114
  223. package/src/third/index.ts +0 -13
  224. package/src/types.ts +0 -672
  225. package/src/utils/auth.ts +0 -15
  226. package/src/utils/checkTcbrEnv.ts +0 -69
  227. package/src/utils/cli-table.ts +0 -23
  228. package/src/utils/commonParamsCheck.ts +0 -65
  229. package/src/utils/config.ts +0 -39
  230. package/src/utils/env.ts +0 -244
  231. package/src/utils/fs/del.ts +0 -5
  232. package/src/utils/fs/index.ts +0 -71
  233. package/src/utils/function-packer.ts +0 -97
  234. package/src/utils/index.ts +0 -25
  235. package/src/utils/log.ts +0 -82
  236. package/src/utils/net/cloud-api-request.ts +0 -62
  237. package/src/utils/net/credential.ts +0 -53
  238. package/src/utils/net/http-request.ts +0 -84
  239. package/src/utils/net/index.ts +0 -5
  240. package/src/utils/net/manager-service.ts +0 -36
  241. package/src/utils/net/proxy.ts +0 -6
  242. package/src/utils/notice.ts +0 -28
  243. package/src/utils/output/highlight.ts +0 -5
  244. package/src/utils/output/index.ts +0 -3
  245. package/src/utils/output/link.ts +0 -10
  246. package/src/utils/output/loading.ts +0 -82
  247. package/src/utils/parallel.ts +0 -82
  248. package/src/utils/platform/index.ts +0 -3
  249. package/src/utils/platform/mac.ts +0 -21
  250. package/src/utils/platform/os.ts +0 -64
  251. package/src/utils/platform/port.ts +0 -10
  252. package/src/utils/progress-bar.ts +0 -38
  253. package/src/utils/prompt/index.ts +0 -1
  254. package/src/utils/prompt/select.ts +0 -60
  255. package/src/utils/reporter/agree.ts +0 -20
  256. package/src/utils/reporter/download.ts +0 -26
  257. package/src/utils/reporter/index.ts +0 -3
  258. package/src/utils/reporter/usage.ts +0 -20
  259. package/src/utils/store/auth.ts +0 -49
  260. package/src/utils/store/common.ts +0 -8
  261. package/src/utils/store/db.ts +0 -68
  262. package/src/utils/store/index.ts +0 -5
  263. package/src/utils/store/usage.ts +0 -12
  264. package/src/utils/tcbrApi/callTcbrApi.ts +0 -28
  265. package/src/utils/tcbrApi/index.ts +0 -1
  266. package/src/utils/template.ts +0 -170
  267. package/src/utils/tools/encoding.ts +0 -8
  268. package/src/utils/tools/index.ts +0 -4
  269. package/src/utils/tools/object.ts +0 -33
  270. package/src/utils/tools/time.ts +0 -38
  271. package/src/utils/tools/uid.ts +0 -19
  272. package/src/utils/validator.ts +0 -65
  273. package/types/commands/env/create.d.ts +0 -12
  274. package/types/commands/run/create.d.ts +0 -13
  275. package/types/commands/run/service/create.d.ts +0 -13
  276. package/types/run/create.d.ts +0 -2
  277. package/types/run/service/create.d.ts +0 -7
package/src/utils/log.ts DELETED
@@ -1,82 +0,0 @@
1
- import chalk from 'chalk'
2
- import logSymbols from 'log-symbols'
3
- import { format } from 'util'
4
- import { Console } from 'console'
5
- import terminalLink from 'terminal-link'
6
-
7
- export class Logger {
8
- c = {
9
- _times: new Map(),
10
- log(a: string, ...args: string[]) {
11
- this.debug(format(a, ...args))
12
- }
13
- }
14
-
15
- verboseEnabled: boolean
16
-
17
- constructor(options: { verbose?: boolean } = {}) {
18
- const { verbose } = options
19
- this.verboseEnabled = verbose
20
- }
21
-
22
- breakLine() {
23
- console.log()
24
- }
25
-
26
- log(...args) {
27
- console.log(...args)
28
- }
29
-
30
- info(msg: string) {
31
- console.log(`${logSymbols.info} ${msg}`)
32
- }
33
-
34
- success(msg: string) {
35
- console.log(`${logSymbols.success} ${msg}`)
36
- }
37
-
38
- warn(msg: string) {
39
- console.log(`${logSymbols.warning} ${msg}`)
40
- }
41
-
42
- error(msg: string) {
43
- console.log(`${logSymbols.error} ${msg}`)
44
- }
45
-
46
- verbose(...args: any) {
47
- if (this.verboseEnabled) {
48
- const msg = args.join(' ▶️ ')
49
- console.log(
50
- `${chalk.bold('[debug]')} ${chalk.gray(`[${new Date().toISOString()}]`)} ${msg}`
51
- )
52
- }
53
- }
54
-
55
- genClickableLink(link: string) {
56
- if (terminalLink.isSupported) {
57
- const clickablelink = terminalLink(link, link)
58
- return chalk.bold.cyan(clickablelink)
59
- }
60
- return chalk.bold.underline.cyan(link)
61
- }
62
-
63
- printClickableLink(link: string) {
64
- const clickLink = this.genClickableLink(link)
65
- this.info(clickLink)
66
- }
67
-
68
- async time(label: string, fn: Promise<any> | (() => Promise<any>)) {
69
- const promise = typeof fn === 'function' ? fn() : fn
70
- if (this.verboseEnabled) {
71
- this.c.log(label)
72
- Console.prototype.time.call(this.c, label)
73
- const r = await promise
74
- Console.prototype.timeEnd.call(this.c, label)
75
- return r
76
- }
77
-
78
- return promise
79
- }
80
- }
81
-
82
- export const logger = new Logger()
@@ -1,62 +0,0 @@
1
- import { getCredentialWithoutCheck, getRegion, Credential, getProxy } from '@cloudbase/toolbox'
2
- import { CloudApiService as _CloudApiService } from '@cloudbase/cloud-api'
3
- import { CloudBaseError } from '../../error'
4
- import { REQUEST_TIMEOUT } from '../../constant'
5
-
6
- let commonCredential: Credential
7
-
8
- // token 将在 n 分钟内过期
9
- const isTokenExpired = (credential: Credential, gap = 120) =>
10
- credential.accessTokenExpired && Number(credential.accessTokenExpired) < Date.now() + gap * 1000
11
-
12
- export class CloudApiService {
13
- // 缓存请求实例
14
- static serviceCacheMap: Record<string, CloudApiService> = {}
15
-
16
- // 单例模式
17
- static getInstance(service: string) {
18
- if (CloudApiService.serviceCacheMap?.[service]) {
19
- return CloudApiService.serviceCacheMap[service]
20
- }
21
- const apiService = new CloudApiService(service)
22
- CloudApiService.serviceCacheMap[service] = apiService
23
- return apiService
24
- }
25
-
26
- region: string
27
- apiService: _CloudApiService
28
-
29
- constructor(service: string, baseParams?: Record<string, any>, version = '') {
30
- // 初始化 API 实例
31
- this.apiService = new _CloudApiService({
32
- service,
33
- version: service === 'tcr' ? version : '2019-09-24',
34
- baseParams,
35
- proxy: getProxy(),
36
- timeout: REQUEST_TIMEOUT,
37
- getCredential: async () => {
38
- // 存在未过期的 token
39
- if (commonCredential?.secretId && !isTokenExpired(commonCredential)) {
40
- return commonCredential
41
- }
42
-
43
- const credential = await getCredentialWithoutCheck()
44
- if (!credential) {
45
- throw new CloudBaseError('无有效身份信息,请使用 cloudbase login 登录')
46
- }
47
-
48
- commonCredential = credential
49
-
50
- return {
51
- ...credential,
52
- tokenExpired: Number(credential.accessTokenExpired)
53
- }
54
- }
55
- })
56
- }
57
-
58
- async request(action: string, data: Record<string, any> = {}, method: 'POST' | 'GET' = 'POST') {
59
- const region = this.region || (await getRegion())
60
- return this.apiService.request({ action, data, method, region })
61
- }
62
- }
@@ -1,53 +0,0 @@
1
- import _ from 'lodash'
2
- import { checkAndGetCredential as _getCredentail } from '@cloudbase/toolbox'
3
- import { CloudBaseError } from '../../error'
4
- import { REQUEST_TIMEOUT } from '../../constant'
5
- import { Credential, AuthSecret } from '../../types'
6
-
7
- import { md5 } from '../tools'
8
- import { getMacAddress } from '../platform'
9
- import { getProxy } from './proxy'
10
- import { fetch } from './http-request'
11
-
12
-
13
- const refreshTokenUrl = 'https://iaas.cloud.tencent.com/tcb_refresh'
14
-
15
- // 临时密钥过期后,进行续期
16
- export async function refreshTmpToken(
17
- metaData: Credential & { isLogout?: boolean }
18
- ): Promise<Credential> {
19
- const mac = await getMacAddress()
20
- const hash = md5(mac)
21
- metaData.hash = hash
22
-
23
- const res = await fetch(refreshTokenUrl, {
24
- method: 'POST',
25
- body: JSON.stringify(metaData),
26
- headers: { 'Content-Type': 'application/json' }
27
- })
28
-
29
- if (res.code !== 0) {
30
- throw new CloudBaseError(res.message, {
31
- code: res.code
32
- })
33
- }
34
-
35
- const { data: credential } = res
36
- return credential
37
- }
38
-
39
- export async function checkAndGetCredential(throwError = false): Promise<AuthSecret> {
40
- const credential = await _getCredentail({
41
- proxy: getProxy(),
42
- timeout: REQUEST_TIMEOUT
43
- })
44
-
45
- if (!credential || _.isEmpty(credential)) {
46
- if (throwError) {
47
- throw new CloudBaseError('无有效身份信息,请使用 cloudbase login 登录')
48
- }
49
- return null
50
- }
51
-
52
- return credential
53
- }
@@ -1,84 +0,0 @@
1
- import _fetch, { RequestInit } from 'node-fetch'
2
- import {HttpsProxyAgent} from 'https-proxy-agent'
3
- import { REQUEST_TIMEOUT } from '../../constant'
4
- import { CloudBaseError } from '../../error'
5
- import { getProxy } from './proxy'
6
-
7
- function handleTimeout(e) {
8
- if (e.type === 'request-timeout') {
9
- throw new CloudBaseError(
10
- '请求超时,请检查你的网络,如果终端无法直接访问公网,请设置终端 HTTP 请求代理!'
11
- )
12
- } else {
13
- // 其他错误,抛出
14
- throw e
15
- }
16
- }
17
-
18
- // 使用 fetch + 代理
19
- export async function fetch(url: string, config: RequestInit = {}) {
20
- const proxy = getProxy()
21
- if (proxy) {
22
- config.agent = new HttpsProxyAgent(proxy)
23
- }
24
-
25
- config.timeout = REQUEST_TIMEOUT
26
-
27
- let json
28
- let text
29
- try {
30
- const res = await _fetch(url, config)
31
- text = await res.text()
32
- json = JSON.parse(text)
33
- } catch (e) {
34
- handleTimeout(e)
35
- json = text
36
- }
37
- return json
38
- }
39
-
40
- // 使用 fetch + 代理
41
- export async function postFetch(url: string, data?: Record<string, any>) {
42
- const proxy = getProxy()
43
- const config: RequestInit = {
44
- method: 'POST',
45
- headers: {
46
- 'Content-Type': 'application/json'
47
- },
48
- body: JSON.stringify(data)
49
- }
50
-
51
- if (proxy) {
52
- config.agent = new HttpsProxyAgent(proxy)
53
- }
54
-
55
- config.timeout = REQUEST_TIMEOUT
56
-
57
- let json
58
- let text
59
- try {
60
- const res = await _fetch(url, config)
61
- text = await res.text()
62
- json = JSON.parse(text)
63
- } catch (e) {
64
- handleTimeout(e)
65
- json = text
66
- }
67
- return json
68
- }
69
-
70
- export async function fetchStream(url, config: Record<string, any> = {}) {
71
- const proxy = getProxy()
72
- if (proxy) {
73
- config.agent = new HttpsProxyAgent(proxy)
74
- }
75
-
76
- config.timeout = REQUEST_TIMEOUT
77
-
78
- try {
79
- const res = await _fetch(url, config)
80
- return res
81
- } catch (e) {
82
- handleTimeout(e)
83
- }
84
- }
@@ -1,5 +0,0 @@
1
- export * from './proxy'
2
- export * from './credential'
3
- export * from './http-request'
4
- export * from './manager-service'
5
- export * from './cloud-api-request'
@@ -1,36 +0,0 @@
1
- import CloudBase from '@cloudbase/manager-node'
2
- import { StorageService } from '@cloudbase/manager-node/types/storage'
3
- import { getRegion } from '@cloudbase/toolbox'
4
- import { checkAndGetCredential } from './credential'
5
- import { getProxy } from './proxy'
6
-
7
- export async function getMangerService(envId = ''): Promise<CloudBase> {
8
- const { secretId, secretKey, token } = await checkAndGetCredential(true)
9
- const region = await getRegion()
10
-
11
- const app = new CloudBase({
12
- token,
13
- envId,
14
- region,
15
- secretId,
16
- secretKey,
17
- proxy: getProxy()
18
- })
19
-
20
- return app
21
- }
22
-
23
- export async function getStorageService(envId: string): Promise<StorageService> {
24
- const { secretId, secretKey, token } = await checkAndGetCredential(true)
25
- const region = await getRegion()
26
-
27
- const app = new CloudBase({
28
- token,
29
- envId,
30
- secretId,
31
- region,
32
- secretKey,
33
- proxy: getProxy()
34
- })
35
- return app.storage
36
- }
@@ -1,6 +0,0 @@
1
- // 解析 Proxy 配置
2
- export function getProxy(): string {
3
- const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY
4
- const isTestMode = process.env.TCB_TEST_MODE
5
- return isTestMode ? process.env.TCB_TEST_PROXY || httpProxy : httpProxy
6
- }
@@ -1,28 +0,0 @@
1
- import { postFetch } from './net'
2
- import { commonStore } from './store'
3
-
4
- const url = 'https://tcli.service.tcloudbase.com/notify'
5
-
6
- // 获取通知,最多每天获取一次
7
- const ONE_DAY = 86400000
8
-
9
- export async function getNotification() {
10
- // 不再获取通知
11
- return null
12
- const now = Date.now()
13
- // 上次记录的时间
14
- const lastTime = await commonStore.get('lastNotifyTime')
15
- const diff = now - Number(lastTime)
16
- const day = new Date(lastTime).getDay()
17
- const today = new Date().getDay()
18
- // 日期不同,且时差小于 1 天
19
- if (day === today && diff < ONE_DAY) return null
20
- const lastUid = await commonStore.get('lastNotificationUid')
21
- const data = await postFetch(url)
22
- const uid = data?.uid || 0
23
- // 消息已读
24
- if (Number(lastUid) >= Number(uid)) return
25
- await commonStore.set('readNotificationUid', data.uid)
26
- await commonStore.set('lastNotifyTime', Date.now())
27
- return data
28
- }
@@ -1,5 +0,0 @@
1
- import chalk from 'chalk'
2
-
3
- export function highlightCommand(command: string) {
4
- return chalk.bold.cyan(command)
5
- }
@@ -1,3 +0,0 @@
1
- export * from './loading'
2
- export * from './link'
3
- export * from './highlight'
@@ -1,10 +0,0 @@
1
- import chalk from 'chalk'
2
- import terminalLink from 'terminal-link'
3
-
4
- export function genClickableLink(link: string) {
5
- if (terminalLink.isSupported) {
6
- const clickablelink = terminalLink(link, link)
7
- return chalk.bold.cyan(clickablelink)
8
- }
9
- return chalk.bold.underline.cyan(link)
10
- }
@@ -1,82 +0,0 @@
1
- import ora from 'ora'
2
-
3
- class Loading {
4
- private spinner: ReturnType<typeof ora>
5
-
6
- constructor() {
7
- // @ts-ignore
8
- process.on('tcbExit', this.stop.bind(this))
9
- // @ts-ignore
10
- process.on('tcbError', this.stop.bind(this))
11
- this.spinner = ora({
12
- discardStdin: false
13
- })
14
- }
15
-
16
- // eslint-disable-next-line
17
- set text(text: string) {
18
- this.spinner.text = text
19
- }
20
-
21
- start(text: string) {
22
- this.spinner = this.spinner.start(text)
23
- }
24
-
25
- stop() {
26
- if (this.spinner) {
27
- this.spinner = this.spinner.stop()
28
- }
29
- }
30
-
31
- succeed(text: string) {
32
- if (this.spinner) {
33
- this.spinner = this.spinner.succeed(text)
34
- }
35
- }
36
-
37
- fail(text: string) {
38
- if (this.spinner) {
39
- this.spinner = this.spinner.fail(text)
40
- }
41
- }
42
- }
43
-
44
- export const loadingFactory = (): Loading => {
45
- return new Loading()
46
- }
47
-
48
- type Task<T> = (flush: (text: string) => void, ...args: any[]) => Promise<T>
49
-
50
- export interface ILoadingOptions {
51
- startTip?: string
52
- successTip?: string
53
- failTip?: string
54
- }
55
-
56
- /**
57
- * 执行异步任务,显示 loading 动画
58
- * @param task
59
- * @param options
60
- */
61
- export const execWithLoading = async <T>(
62
- task: Task<T>,
63
- options: ILoadingOptions = {}
64
- ): Promise<T> => {
65
- const { startTip, successTip, failTip } = options
66
- const loading = loadingFactory()
67
-
68
- // 刷新 loading 提示
69
- const flush = (text: string) => {
70
- loading.text = text
71
- }
72
-
73
- try {
74
- loading.start(startTip)
75
- const res = await task(flush)
76
- successTip ? loading.succeed(successTip) : loading.stop()
77
- return res
78
- } catch (e) {
79
- failTip ? loading.fail(failTip) : loading.stop()
80
- throw e
81
- }
82
- }
@@ -1,82 +0,0 @@
1
- export type AsyncTask = () => Promise<any>
2
-
3
- /**
4
- * 异步任务并发控制器,以一定的并发数执行所有任务
5
- * 不具备队列性质,异步任务随机执行
6
- * 单个任务异常,错误会返回,单不会退出执行
7
- * 所有任务执行
8
- */
9
- export class AsyncTaskParallelController {
10
- // 任务最大并发数
11
- maxParallel: number
12
- // 异步任务队列
13
- tasks: AsyncTask[]
14
- // 检查间隔时间,单位 ms
15
- checkInterval: number
16
- // 所有任务数量
17
- totalTasks: number
18
-
19
- constructor(maxParallel: number, checkInterval = 20) {
20
- this.tasks = []
21
- this.maxParallel = Number(maxParallel) || 20
22
- this.checkInterval = checkInterval
23
- }
24
-
25
- loadTasks(tasks: AsyncTask[]) {
26
- this.tasks.push(...tasks)
27
- this.totalTasks = this.tasks.length
28
- }
29
-
30
- push(task: AsyncTask) {
31
- this.tasks.push(task)
32
- this.totalTasks = this.tasks.length
33
- }
34
-
35
- // 开始执行任务
36
- async run(): Promise<any[]> {
37
- // 存储任务执行结果
38
- const results = []
39
- // 记录已经运行的任务
40
- const taskHasRun = []
41
- // 记录任务是否执行完成
42
- const taskDone = []
43
- // 当前正在运行的任务数量
44
- let runningTask = 0
45
-
46
- return new Promise((resolve) => {
47
- // 使用定时器,不阻塞线程
48
- const timer = setInterval(() => {
49
- // 全部任务运行完成
50
- const taskDoneLength = taskDone.filter((item) => item).length
51
- if (runningTask === 0 && taskDoneLength === this.totalTasks) {
52
- clearInterval(timer)
53
- resolve(results)
54
- }
55
-
56
- // 当前运行任务数超过最大并发,不再执行新的任务
57
- if (runningTask >= this.maxParallel) {
58
- return
59
- }
60
-
61
- // 遍历任务列表,开始执行还没有执行的任务
62
- this.tasks.forEach((task, index) => {
63
- if (!taskHasRun[index] && runningTask < this.maxParallel) {
64
- runningTask++
65
- taskHasRun[index] = 1
66
- task()
67
- .then((res) => {
68
- results[index] = res
69
- })
70
- .catch((err) => {
71
- results[index] = err
72
- })
73
- .then(() => {
74
- runningTask--
75
- taskDone[index] = 1
76
- })
77
- }
78
- })
79
- }, this.checkInterval)
80
- })
81
- }
82
- }
@@ -1,3 +0,0 @@
1
- export * from './os'
2
- export * from './mac'
3
- export * from './port'
@@ -1,21 +0,0 @@
1
- import address from 'address'
2
- import { md5 } from '../tools'
3
-
4
- // 获取本机 Mac 地址
5
- export function getMacAddress(): Promise<string> {
6
- return new Promise(resolve => {
7
- address.mac((err, mac) => {
8
- if (err) {
9
- resolve('')
10
- return
11
- }
12
- resolve(mac)
13
- })
14
- })
15
- }
16
-
17
- export async function getMacAddressMd5(): Promise<string> {
18
- const mac = await getMacAddress()
19
- const macMD5 = md5(mac)
20
- return macMD5
21
- }
@@ -1,64 +0,0 @@
1
- import os from 'os'
2
-
3
- const macOSMap = new Map([
4
- [19, 'Catalina'],
5
- [18, 'Mojave'],
6
- [17, 'High Sierra'],
7
- [16, 'Sierra'],
8
- [15, 'El Capitan'],
9
- [14, 'Yosemite'],
10
- [13, 'Mavericks'],
11
- [12, 'Mountain Lion'],
12
- [11, 'Lion'],
13
- [10, 'Snow Leopard'],
14
- [9, 'Leopard'],
15
- [8, 'Tiger'],
16
- [7, 'Panther'],
17
- [6, 'Jaguar'],
18
- [5, 'Puma']
19
- ])
20
-
21
- const winMap = new Map([
22
- ['10.0', '10'],
23
- ['6.3', '8.1'],
24
- ['6.2', '8'],
25
- ['6.1', '7'],
26
- ['6.0', 'Vista'],
27
- ['5.2', 'Server 2003'],
28
- ['5.1', 'XP'],
29
- ['5.0', '2000'],
30
- ['4.9', 'ME'],
31
- ['4.1', '98'],
32
- ['4.0', '95']
33
- ])
34
-
35
- export function getPlatformRelease(platform: string, release: string) {
36
- // macOS
37
- if (platform === 'darwin') {
38
- const releaseNum = Number(release.split('.')[0])
39
- const name = macOSMap.get(releaseNum)
40
- const version = '10.' + (releaseNum - 4)
41
- return `${name} ${version}`
42
- }
43
-
44
- // windows
45
- if (platform === 'win32') {
46
- const version = (/\d+\.\d/.exec(release) || [])[0]
47
-
48
- return `Windows ${winMap.get(version)}`
49
- }
50
-
51
- // 其他 Linux
52
- return 'Linux'
53
- }
54
-
55
- // 获取 hostname 和平台信息
56
- export function getOSInfo() {
57
- const hostname = os.hostname()
58
- const platform = os.platform()
59
- const release = os.release()
60
-
61
- const platformRelease = getPlatformRelease(platform, release)
62
-
63
- return [hostname, platformRelease].join('/')
64
- }
@@ -1,10 +0,0 @@
1
- import portfinder from 'portfinder'
2
- const PORT = 9012
3
-
4
- // 获取本地可用端口
5
- export async function getPort(): Promise<number> {
6
- const port: number = await portfinder.getPortPromise({
7
- port: PORT
8
- })
9
- return port
10
- }
@@ -1,38 +0,0 @@
1
- import ProgressBar from 'progress'
2
-
3
- // 打印文件传输进度条
4
- export function createUploadProgressBar(onFinished: Function, onStart?: Function) {
5
- let bar
6
- let lastLoaded = 0
7
- let finished = false
8
-
9
- return function (data) {
10
- const { total, loaded, percent } = data
11
- if (lastLoaded === 0 && onStart) {
12
- onStart()
13
- }
14
-
15
- if (+percent === 1) {
16
- if (finished) return
17
- finished = true
18
- setTimeout(() => {
19
- onFinished()
20
- }, 500)
21
- bar.tick(total - lastLoaded)
22
- return
23
- }
24
- const tick = loaded - lastLoaded
25
- lastLoaded = loaded
26
- if (bar) {
27
- bar.tick(tick)
28
- } else {
29
- bar = new ProgressBar('文件传输中 [:bar] :percent :etas', {
30
- total,
31
- complete: '=',
32
- incomplete: ' ',
33
- width: 50
34
- })
35
- bar.tick(tick)
36
- }
37
- }
38
- }