-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsmart.ts
132 lines (115 loc) · 3.46 KB
/
smart.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import fs from 'fs'
import os from 'os'
import _ from 'lodash'
import path from 'path'
import chalk from 'chalk'
import { prompt } from 'enquirer'
import { run } from '@cloudbase/framework-core'
import { ConfigParser, getRegion } from '@cloudbase/toolbox'
import {
logger,
getSelectedEnv,
authSupevisor,
loadingFactory,
getSelectRegion,
checkTcbService,
getCloudBaseConfig,
downloadTemplate,
initProjectConfig
} from '../utils'
import * as Hosting from '../hosting'
import * as Function from '../function'
import { checkLogin } from '../auth'
/**
* 智能命令
*/
export async function smartDeploy() {
const loading = loadingFactory()
loading.start('环境检测中')
// 检查登录
await checkLogin()
// 检查是否开通 TCB 服务
await checkTcbService()
const files = fs.readdirSync(process.cwd())
loading.stop()
// 获取当前目录
const home = os.homedir()
const current = process.cwd()
let relative = current
if (current.indexOf(home) > -1) {
relative = path.relative(home, current)
}
// 当期区域
let region = await getRegion(true)
// 当前目录为空,执行初始化项目操作
if (!files.length) {
logger.info('当前目录为空,初始化云开发项目\n')
// 先选择 region,再选择环境
region = await getSelectRegion()
const envId = await getSelectedEnv()
// 下载模板
const projectPath = await downloadTemplate()
await initProjectConfig(envId, region, projectPath)
// 成功提示
logger.success('初始化项目成功!\n')
}
// 初始化项目成功,或当前目录已经存在项目,继续使用 Framework 执行部署
const { setup } = await prompt<any>({
type: 'confirm',
name: 'setup',
message: `是否使用云开发部署当前项目 <${chalk.bold.cyan(relative)}> ?`,
initial: true
})
if (!setup) {
return
}
// 检测是否有 cloudbase 配置
const config = await getCloudBaseConfig()
let envId = config?.envId
// 配置文件中不存在 region,选择 region
if (!config?.region && !region) {
region = await getSelectRegion()
}
// 配置文件不存在
if (!envId) {
envId = await getSelectedEnv()
fs.writeFileSync(
path.join(process.cwd(), 'cloudbaserc.json'),
JSON.stringify({
envId,
region,
version: '2.0',
$schema: 'https://framework-1258016615.tcloudbaseapp.com/schema/latest.json'
})
)
}
// 把 region 写入到配置文件中
const parser = new ConfigParser()
parser.update('region', region)
// 调用 Framework
await callFramework(envId)
}
async function callFramework(envId: string) {
const loginState = await authSupevisor.getLoginState()
const { token, secretId, secretKey } = loginState
const config = await getCloudBaseConfig()
await run(
{
projectPath: process.cwd(),
cloudbaseConfig: {
secretId,
secretKey,
token,
envId
},
config,
logLevel: process.argv.includes('--verbose') ? 'debug' : 'info',
resourceProviders: {
hosting: Hosting,
function: Function
}
},
'deploy',
''
)
}