-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.js
66 lines (56 loc) · 1.78 KB
/
profile.js
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
const os = require('os')
const fs = require('fs-extra')
const path = require('path')
const { Command } = require('commander')
const program = new Command()
program
.usage('[ProfileName]')
.option('-l, --list', 'list all the profiles')
.option('-s, --save <ProfileName>', 'save the current leaf config to this profile')
.option('-d, --delete <ProfileName>', 'delete the specified profile')
.parse(process.argv)
async function main() {
const aliProfile = path.join(process.env.HOME || os.homedir(), '.fcli', 'config.yaml')
if (!await fs.pathExists(aliProfile)) {
console.log('Please run "leaf config" first')
return
}
const tmpPath = path.join(os.homedir(), 'leaf-profiles')
await fs.ensureDir(tmpPath)
if (program.list) {
console.log('List profiles')
const files = await fs.readdir(tmpPath)
console.log(files.length ? files.join('\n') : '<empty>')
return
}
if (program.save) {
const profileName = program.save
console.log('Save current leaf config to profile', profileName)
await fs.copy(aliProfile, path.join(tmpPath, profileName))
return
}
if (program.delete) {
const profileName = program.delete
console.log('Delete profile', profileName)
const file = path.join(tmpPath, profileName)
if (!await fs.pathExists(file)) {
console.log('Profile', profileName, 'is not exists')
return
}
await fs.remove(file)
return
}
if (program.args[0]) {
const profileName = program.args[0]
const file = path.join(tmpPath, profileName)
if (!await fs.pathExists(file)) {
console.log('Profile', profileName, 'is not exists')
return
}
console.log('Switch config to profile', profileName)
await fs.copy(path.join(tmpPath, profileName), aliProfile)
return
}
console.log('-h or --help for command help')
}
main().catch((e) => console.error(e.errorMessage || e))