-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·141 lines (127 loc) · 3.13 KB
/
index.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
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
#!/usr/bin/env node
'use strict'
const program = require('commander')
const fs = require('fs-extra')
const chalk = require('chalk')
const selectName = require('./utils/selectName')
const getName = require('./utils/getName')
const path = require('path')
const inquirer = require('inquirer')
const {
actual,
s3cfgFile,
names,
content,
validName,
s3sDir
} = require('./utils/config')
program
.command('add <file>')
.option('-n, --name <name>', 's3 name')
.description('Add s3 config from s3cfg file.')
.action((file, options) => {
getName(options.name, name => {
return fs.copy(
path.resolve(process.cwd(), file),
path.join(s3sDir, name)
)
})
})
program
.command('ls')
.description('List s3 config.')
.action(() => {
names.map(name => {
if (content[name] === actual) {
return console.log(chalk.blue(`* ${name}`))
}
console.log(`- ${name}`)
})
})
program
.command('rm [name]')
.description('Delete saved s3 config.')
.action(name => {
selectName(name, name => {
inquirer
.prompt([
{
type: 'confirm',
name: 'ok',
message: `Delete ${name} ? `
}
])
.then(res => {
if (res.ok) {
fs.remove(path.join(s3sDir, name))
}
})
})
})
program
.command('save <name>')
.description('Save actual s3 config.')
.action(name => {
if (!validName(name)) {
console.log(chalk.red.bold('Name must has string.'))
return process.exit(1)
}
fs.copy(s3cfgFile, path.join(s3sDir, name))
})
program
.command('use [name]')
.description('Use s3 config.')
.action(name => {
selectName(name, name => {
fs.copy(path.join(s3sDir, name), s3cfgFile, { overwrite: true })
})
})
function goodDest (name) {
if (!validName(name)) {
return 'The name must be a string match with a-zA-Z0-9-'
}
if (names.indexOf(name) !== -1) {
return 'This name already exists.'
}
return true
}
program
.command('mv')
.description('Rename s3 config.')
.option('-s, --source <source>', 'S3 config name source')
.option('-d, --dest <dest>', 'S3 config name destination')
.action(options => {
selectName(options.source, source => {
if (goodDest(options.dest) === true) {
return fs.renameSync(
path.join(s3sDir, source),
path.join(s3sDir, options.dest)
)
}
inquirer
.prompt([
{
type: 'input',
name: 'dest',
default: options.dest,
message: `Rename ${source} dest :`,
validate: dest => {
if (!validName(dest)) {
return 'The name must be a string match with a-zA-Z0-9-'
}
if (names.indexOf(dest) !== -1) {
return 'This name already exists.'
}
return true
}
}
])
.then(res => {
fs.renameSync(path.join(s3sDir, source), path.join(s3sDir, res.dest))
})
})
})
program.parse(process.argv)
if (!process.argv.slice(2).length) {
program.outputHelp()
}