-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
247 lines (219 loc) · 5.03 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { program } from 'commander'
import { resolve } from 'node:path'
import { readFile, mkdir } from 'node:fs/promises'
import { homedir } from 'node:os'
import simpleGit from 'simple-git'
import inquirer from 'inquirer'
import type { ChoiceOptions } from 'inquirer'
const CONFIG_DIR = resolve(homedir(), `.config/git-switcher`)
type Config = {
profiles: {
[profileName: string]: {
email: string
name: string
}
}
}
async function readConfig() {
const defaultConfig: Config = {
profiles: {},
}
try {
const config = await readFile(resolve(CONFIG_DIR, 'config.json'))
const parsed: Config = JSON.parse(config.toString())
return {
...defaultConfig,
...(parsed ?? {}),
}
} catch (error) {
return defaultConfig
}
}
program
.command('use')
.alias('u')
.action(async () => {
const config = await readConfig()
if (Object.keys(config.profiles).length === 0) {
console.log(
'⚠️ There are no profiles to use, add one with `git-switcher add`',
)
return
}
const {
profileNameToUse,
}: {
profileNameToUse: string
} = await inquirer.prompt([
{
type: 'list',
choices: Object.entries(config.profiles).map(
([profileName]): ChoiceOptions => {
return {
name: profileName,
value: profileName,
}
},
),
loop: true,
message: 'Profile to use',
name: 'profileNameToUse',
},
])
const profile = config.profiles[profileNameToUse]
if (!profile) {
console.log('❌ Profile not found')
return
}
await simpleGit()
.addConfig('user.email', profile.email)
.addConfig('user.name', profile.name)
console.log('✅ Profile changed')
})
program
.command('edit')
.alias('e')
.action(async () => {
const {
profileName,
}: {
profileName: string
} = await inquirer.prompt([
{
type: 'input',
name: 'profileName',
message: 'Profile name (unique identifier)',
},
])
const config = await readConfig()
const profile = config.profiles[profileName]
if (!profile) {
console.log('❌ Profile not found')
return
}
const {
email,
name,
}: {
email: string
name: string
} = await inquirer.prompt([
{
type: 'input',
name: 'email',
message: 'Profile email',
},
{
type: 'input',
name: 'name',
message: 'Profile username',
},
])
await Bun.write(
resolve(CONFIG_DIR, 'config.json'),
JSON.stringify(
{
...config,
profiles: {
...config.profiles,
[profileName]: {
email,
name,
},
},
} satisfies Config,
null,
2,
),
)
})
program.command('rm').action(async () => {
const config = await readConfig()
const {
profileNameToDelete,
}: {
profileNameToDelete: string
} = await inquirer.prompt([
{
type: 'list',
choices: Object.entries(config.profiles).map(
([profileName]): ChoiceOptions => {
return {
name: profileName,
value: profileName,
}
},
),
loop: true,
message: 'Profile to delete',
name: 'profileNameToDelete',
},
])
delete config.profiles[profileNameToDelete]
await Bun.write(
resolve(CONFIG_DIR, 'config.json'),
JSON.stringify(config, null, 2),
)
})
program
.command('add')
.alias('a')
.action(async () => {
await mkdir(CONFIG_DIR, { recursive: true })
const {
profileName,
email,
name,
}: {
profileName: string
email: string
name: string
} = await inquirer.prompt([
{
type: 'input',
name: 'profileName',
message: 'Profile name (unique identifier)',
},
{
type: 'input',
name: 'email',
message: 'Profile email',
},
{
type: 'input',
name: 'name',
message: 'Profile username',
},
])
const config = await readConfig()
const exists = config.profiles[profileName]
if (exists) {
console.log('❌ Profile already exists')
return
}
await Bun.write(
resolve(CONFIG_DIR, 'config.json'),
JSON.stringify(
{
...config,
profiles: {
...config.profiles,
[profileName]: {
email,
name,
},
},
} satisfies Config,
null,
2,
),
)
})
program
.command('list')
.alias('l')
.action(async () => {
const config = await readConfig()
console.log(config)
})
program.parse()