-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·180 lines (174 loc) · 5.4 KB
/
cli.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
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
#!/usr/bin/env node
const inquirer = require('inquirer')
const pj = require('prettyjson')
const {
listProjects,
listProjectLanguages,
cleanTranslationJSON,
importNewTerms,
synchronizeTerms,
updateTranslations,
viewProject,
listContributors,
addContributor,
removeContributor,
} = require('./gitpo')
const {
PROJECT_VIEW,
PROJECT_UPDATE,
PROJECT_TERMS,
PROJECT_CLEAN,
CONTRIBUTORS,
CONTRIBUTORS_LIST,
CONTRIBUTORS_ADD,
CONTRIBUTORS_REMOVE,
} = require('./utils/konstants')
const handleError = (error) => {
console.log('Something went wrong')
console.error(error)
}
let languages = []
inquirer
.prompt([
{
type: 'list',
name: 'action',
message: 'What would you like to do:',
choices: [
{ name: 'View Project Details', value: PROJECT_VIEW },
{ name: 'Update Code (POEditor → GitHub)', value: PROJECT_UPDATE },
{ name: 'Update POEditor (GitHub → POEditor)', value: PROJECT_TERMS },
{ name: 'Remove default Translations', value: PROJECT_CLEAN },
{ name: 'Manage contributors', value: CONTRIBUTORS },
],
},
{
when: ({ action }) => action === CONTRIBUTORS,
type: 'list',
name: 'action',
message: 'What would you like to do:',
choices: [
{ name: 'List collaborators', value: CONTRIBUTORS_LIST },
{ name: 'Add collaborator', value: CONTRIBUTORS_ADD },
{ name: 'Remove collaborator', value: CONTRIBUTORS_REMOVE },
],
},
{
when: ({ action }) => action === CONTRIBUTORS_ADD,
type: 'input',
name: 'fullname',
message: 'Full Name:',
},
{
when: ({ action }) => [CONTRIBUTORS_ADD, CONTRIBUTORS_REMOVE].includes(action),
type: 'input',
name: 'email',
message: 'Email:',
},
{
when: ({ action }) => [CONTRIBUTORS_ADD, CONTRIBUTORS_REMOVE].includes(action),
type: 'checkbox',
name: 'projects',
message: 'Select project(s):',
choices: async () => {
const projects = await listProjects()
return projects.map(({ name, id: value }) => ({ name, value }))
},
},
{
when: ({ action }) => action === PROJECT_CLEAN,
type: 'input',
name: 'file',
message: 'Where is the file to work on?',
},
{
when: ({ action }) => action === PROJECT_CLEAN,
type: 'confirm',
name: 'override',
message: 'Override existing file? (if not, a file will be created next to the existing one)',
default: false,
},
{
when: ({ action }) => [PROJECT_VIEW, PROJECT_UPDATE, PROJECT_TERMS].includes(action),
type: 'list',
name: 'project',
message: 'Select a project:',
choices: async () => {
const projects = await listProjects()
return projects.map(({ name, id: value }) => ({ name, value }))
},
},
{
when: async ({ action, project, projects }) => {
if ([PROJECT_UPDATE, CONTRIBUTORS_ADD, CONTRIBUTORS_REMOVE].includes(action)) {
if (projects && !project) {
project = projects[0] // NOTE: assuming all projects have the same language set here
}
languages = await listProjectLanguages(project)
return true
}
return false
},
type: 'checkbox',
name: 'languages',
message: 'Select Language(s):',
choices: async () => languages.map(({ name, code }) => ({ name, value: code })),
},
{
when: ({ action }) => action === PROJECT_TERMS,
type: 'confirm',
name: 'destructiveUpdate',
message: 'Would you like to remove obsolete terms? (⚠ Destructive Update ⚠)',
default: false,
},
])
.then(({ project, action, languages, file, override, destructiveUpdate, email, fullname, projects }) => {
switch (action) {
case PROJECT_VIEW:
viewProject(project).then((res) => console.log(pj.render(res)))
break
case PROJECT_TERMS:
destructiveUpdate
? synchronizeTerms(project)
.then(() => console.log('Syncing done'))
.catch(handleError)
: importNewTerms(project)
.then(() => console.log('Import done'))
.catch(handleError)
break
case PROJECT_UPDATE:
updateTranslations(project, languages)
.then(() => console.log('Languages updated'))
.catch(handleError)
break
case PROJECT_CLEAN:
cleanTranslationJSON(file, override)
.then(() => console.log('File ready'))
.catch(handleError)
break
case CONTRIBUTORS_LIST:
listContributors().then((res) =>
console.log(
pj.render(
res.contributors.map(({ name, email, permissions }) => ({
name,
email,
projects: permissions.map(
({ project: { name }, type, languages = ['all'] }) =>
`${type} | (${languages.sort().join(', ')}) > ${name}`,
),
})),
),
),
)
break
case CONTRIBUTORS_ADD:
addContributor(email, fullname, projects, languages).then(() => console.log('Contributor added'))
break
case CONTRIBUTORS_REMOVE:
removeContributor(email, projects, languages).then(() => console.log('Contributor removed'))
break
default:
console.log('Sorry, I did not get what it is that you want...')
}
})