-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·127 lines (108 loc) · 2.78 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
#!/usr/bin/env node
const fs = require('fs')
let config = require('./china-plate.json')
const { prompt, Separator } = require('inquirer')
const chalk = require('chalk')
const exec = require('./exec')
const { pcore, parray } = require('pico-lambda')
const { curry, pipe } = pcore
const { push, map } = parray
const logData = (param) => { console.log(param); return param }
const buildUserSelection = curry((selectFn, user) => ({ name: user.name, value: () => selectFn(user) }))
const buildUserPrompt = curry((message, extraOptions, userFunction, users) =>
({
type: "list",
name: "user",
pageSize: 10,
message,
choices: pipe(
map(buildUserSelection(userFunction)),
push(new Separator()),
push(...extraOptions)
)(users)
})
)
const buildChangePrompt = buildUserPrompt("Please select who's committing",
[{ name: "Add Person...", value: addPrompt },
{ name: "Delete Person...", value: deletePrompt }],
changeUser)
const buildDeletePrompt = buildUserPrompt("select a user to DELETE",
[{ name: "Return...", value: changePrompt }],
deleteUser)
const addUserPrompt = [
{
type: "input",
name: "name",
message: "User's name",
},
{
type: "input",
name: "username",
message: "Git username",
},
{
type: "input",
name: "email",
message: "git email address",
}
]
function deletePrompt() {
prompt(buildDeletePrompt(config))
.then((answers) => {
if (answers && typeof answers.user === "function") {
answers.user()
}
})
}
function changePrompt() {
prompt(buildChangePrompt(config))
.then((answers) => {
if (answers && typeof answers.user === "function") {
answers.user()
}
})
}
function addPrompt() {
prompt(addUserPrompt)
.then((answers) => {
addUser(answers)
})
}
// const createAddUser = config => user => pipe(push(user), writeFile)
function addUser(user) {
config = pipe(
push(user),
writeFile
)(config)
changePrompt()
}
function success() { }
function error(e) {
if (e) {
console.log(chalk.yellow(`Whoops! We ${e}ed
No worries, it is probably not our fault.
Can you please check
1. If this is a git repo
2. If you are in the correct directory`))
process.exit(0)
}
}
function changeUser({ username, email }) {
exec('git', ["config", `user.name "${username}"`], error)
exec('git', ["config", `user.email "${email}"`], error)
}
function deleteUser(user) {
config = config.filter(u => u.username !== user.username)
writeFile(config)
changePrompt()
}
function writeFile(objToWrite) {
fs.writeFile("china-plate.json", JSON.stringify(objToWrite, null, 2), function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
})
return objToWrite
}
changePrompt()