-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig.ts
77 lines (68 loc) · 2.14 KB
/
config.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
import _ from 'lodash'
import path from 'path'
import yargs, { Arguments } from 'yargs'
import { ConfigParser, ICloudBaseConfig } from '@cloudbase/toolbox'
import { Credential } from '../types'
export interface IArgs {
envId: string
region: string
verbose: boolean
configPath: string
[x: string]: unknown
}
export const getArgs = (): Arguments<IArgs> => {
// console.log(yargs.argv)
return yargs.alias('e', 'envId').alias('r', 'region').argv as any
}
const hasOwn = (obj: object, name: string): boolean => {
return Object.prototype.hasOwnProperty.call(obj ?? {}, name)
}
/**
* 用是否设置privateSettings来决定它是否是私有化环境
*/
export function getPrivateSettings(
config: ICloudBaseRcSettings,
cmd?: string
): undefined | IPrivateSettings {
const commonConfig = config
const currentConfig = cmd ? config?.[cmd] : config
if (
hasOwn(currentConfig || {}, 'privateSettings') ||
hasOwn(commonConfig || {}, 'privateSettings')
) {
return { ...commonConfig.privateSettings, ...currentConfig.privateSettings }
}
return undefined
}
type IAbsUrl = `http://${string}` | `https://${string}`
/**
* 私有化配置
*/
export interface IPrivateSettings {
credential: Credential
endpoints: {
editor: IAbsUrl
cliApi: IAbsUrl
}
privateUin: string
}
export interface ICloudBaseRcSettings extends ICloudBaseConfig {
privateSettings?: IPrivateSettings
}
// 获取 cloudbase 配置
export const getCloudBaseConfig = async (configPath?: string): Promise<ICloudBaseRcSettings> => {
const args = getArgs()
let specificConfigPath = configPath || args.configPath
specificConfigPath = specificConfigPath ? path.resolve(specificConfigPath) : undefined
const parser = new ConfigParser({
configPath: specificConfigPath
})
const config: ICloudBaseConfig = await parser.get()
// 合并默认配置
if (config?.functionDefaultConfig && config?.functions?.length) {
config.functions = config.functions.map((rawConfig) =>
_.merge({}, config.functionDefaultConfig, rawConfig)
)
}
return config
}