Skip to content

Commit 37d2b38

Browse files
committed
feat(cli): 增加配置获取和更新函数
1 parent e2ef4a5 commit 37d2b38

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

packages/cli/src/libs/config.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import fs from 'fs'
2+
import inquirer from "inquirer"
3+
import { configJsonPath, readConfigs } from "@lexmin0412/gcm-api"
4+
import pc from 'picocolors'
5+
6+
export const getConfig = async (type: 'sync') => {
7+
if (type !== 'sync') {
8+
console.error('不支持的配置类型, 请检查你的命令是否正确')
9+
process.exit(1)
10+
}
11+
const configContent = readConfigs()
12+
if (!configContent.sync) {
13+
console.log(pc.red('未找到同步配置'))
14+
process.exit(1)
15+
}
16+
console.log(pc.green('同步配置如下:'))
17+
console.table([{
18+
type: configContent.sync.type,
19+
repoUrl: configContent.sync.repoUrl,
20+
dir: configContent.sync.dir,
21+
filename: configContent.sync.filename
22+
}])
23+
process.exit(0)
24+
25+
}
26+
27+
export const setConfig = async (type: 'sync') => {
28+
// 获取命令中携带的标识参数
29+
if (type !== 'sync') {
30+
console.error('不支持的配置类型, 请检查你的命令是否正确')
31+
process.exit(1)
32+
}
33+
const {
34+
storageType,
35+
repoUrl,
36+
dir,
37+
filename
38+
} = await inquirer.prompt([
39+
{
40+
type: 'list',
41+
choices: [
42+
{
43+
name: 'Github 仓库',
44+
value: 'github'
45+
}
46+
],
47+
name: 'storageType',
48+
message: '请选择存储类型'
49+
},
50+
{
51+
type: 'input',
52+
name: 'repoUrl',
53+
message: '请输入仓库地址(支持 https 和 ssh)'
54+
},
55+
{
56+
type: 'input',
57+
name: 'dir',
58+
message: '请输入子目录',
59+
default: 'gcm'
60+
},
61+
{
62+
type: 'input',
63+
name: 'filename',
64+
message: '请输入文件名称',
65+
default: 'config.json'
66+
},
67+
])
68+
const configContent = readConfigs()
69+
configContent.sync = {
70+
type: storageType,
71+
repoUrl,
72+
dir,
73+
filename
74+
}
75+
fs.writeFileSync(configJsonPath, JSON.stringify(configContent, null, 2))
76+
console.log(pc.green('同步配置写入成功:'))
77+
const newConfigContent = readConfigs()
78+
console.table([{
79+
type: newConfigContent.sync.type,
80+
repoUrl: newConfigContent.sync.repoUrl,
81+
dir: newConfigContent.sync.dir,
82+
filename: newConfigContent.sync.filename
83+
}])
84+
85+
}

0 commit comments

Comments
 (0)