-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
executable file
·255 lines (236 loc) · 9.65 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
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
248
249
250
251
252
253
254
255
#!/usr/bin/env node
'use strict';
// EXTERNAL DEPENDENCIES
const fs = require('fs');
const prompt = require('inquirer').prompt;
const pathTool = require('path');
const gitLabel = require('git-label');
// UTILS ARE STANDALONE METHODS WITH NO DEPENDENCIES
const alertDeletes = require('./utils/alert-deletes.js');
const banner = require('./utils/banners.js');
const configGitLabel = require('./utils/config-git-label.js');
const filterRemovalLabels = require('./utils/filter-removal-labels.js');
const removeAllFromStr = require('./utils/remove-all-from-str.js');
const validateRemovals = require('./utils/validate-removals.js');
// PROMPTS ARE THE PROMPTS ARRAYS FOR VARIOUS QUESTIONS
const prompts = {
addCustom: require('./prompts/add-custom.js'),
deleteConfirm: require('./prompts/delete-confirm.js'),
mainMenu: require('./prompts/main-menu.js'),
};
// MODULES ARE UTILS WITH DEPENDENCIES
const convertRGBToHex = require('./modules/convert-rgb-to-hex.js');
const doCustomLabelPrompts = require('./modules/do-custom-label-prompts.js')(prompts.addCustom);
const readRepo = require('./modules/read-repo.js');
const setToken = require('./modules/set-token.js');
const fetchToken = require('./modules/fetch-token.js');
const isGitRepo = require('./modules/is-git-repo.js');
const readGitConfig = require('./modules/read-git-config.js');
const requestLabels = require('./modules/request-labels.js');
const validateAddPackages = require('./modules/validate-add-packages.js');
// Kicks things off, named so that it can be called at any time
// The params will sometimes come thru if we've just set the token, so if we got them we alter the call a lil...
const gitLabelmaker = (token) => {
Promise.all([isGitRepo(), readGitConfig(), fetchToken(token)])
.then(values => {
// TODO: this is a bit callback-hellish
readRepo(values[1])
.then(_repo => {
const _token = values[2];
banner.welcome();
prompt(prompts.mainMenu, handleMainPrompts.bind(null, _repo, _token)); // eslint-disable-line no-use-before-define
})
.catch(e => {
console.error(e);
process.exit(1);
});
})
.catch(e => {
switch (e.id) {
case 'TOKEN':
return setToken(gitLabelmaker);
case 'QUIT':
banner.seeYa();
return process.exit(0);
case 'PASSWORD':
banner.wrongPassword();
return gitLabelmaker();
default:
console.warn(e);
return process.exit(1);
}
});
};
exports.gitLabelmaker = gitLabelmaker;
// resetToken function
const resetToken = () => {
banner.resetToken();
return setToken(gitLabelmaker);
};
exports.resetToken = resetToken;
// addCustom labels function
const addCustom = (repo, token) => {
banner.addCustom();
return doCustomLabelPrompts([], newLabels => {
newLabels.map(newLabel => {
if (newLabel.color.indexOf(',') > -1) {
try {
newLabel.color = convertRGBToHex(newLabel.color); // eslint-disable-line
} catch (e) {
// graceful quit if one of the values isn't actually rgb;
console.log(e);
gitLabelmaker(token);
}
}
return newLabel;
}).map(newLabel => {
const _newLabel = newLabel;
_newLabel.color = `#${newLabel.color}`;
return _newLabel;
});
gitLabel.add(configGitLabel(repo, token), newLabels)
.then(console.log)
.catch(console.warn);
});
};
exports.addCustom = addCustom;
// addFromPackage function
const addFromPackage = (repo, token, path) => {
gitLabel.find(removeAllFromStr(path, ['`', '"', '\'']))
.then(newLabels => gitLabel.add(
configGitLabel(repo, token), newLabels)
)
.then(console.log)
.catch(console.warn);
};
exports.addFromPackage = addFromPackage;
const setGlobalPackage = (path, token) => {
fs.writeFile('.git-labelmaker-config', pathTool.resolve(__dirname, '..', path), (err) => {
if (err) throw err;
console.log(`Saved ${path} as a global. When you specify no file when adding from a package, the global will be used instead.`);
gitLabelmaker(token);
});
};
// Not in use... yet :)
// const getGlobalPackage = () => {
// return fs.readFileSync('.git-labelmaker-config', 'utf-8');
// };
// removeLabels function
const removeLabels = (repo, token, answers) => {
// Tell the user what they're about to lose
console.log('About to delete the following labels:');
alertDeletes(answers.removals);// alerts the list of labels to be removed
// Ya sure ya wanna do this bud?
prompt(prompts.deleteConfirm)
.then(confirmRemove => {
if (confirmRemove.youSure) {
return gitLabel.remove(configGitLabel(repo, token), answers.removals);
}
return gitLabelmaker();
})
.then(console.log)
.catch(console.warn);
};
exports.removeLabels = removeLabels;
// Callback for the main prompts, handles program flow
const handleMainPrompts = (repo, token, ans) => {
let labelPkg = [];
switch (ans.main.toLowerCase()) {
case 'quit':
banner.seeYa();
return process.exit(1);
case 'reset token':
return resetToken();
case 'add custom labels':
return addCustom(repo, token);
case 'add global package':
return prompt([{
name: 'path',
type: 'input',
message: 'What is the pathname you want to use for your package? (Must be valid json)',
validate: validateAddPackages,
}])
.then((globalAns) => setGlobalPackage(globalAns.path, token))
.catch(console.warn);
case 'add labels from package':
banner.addFromPackage();
return prompt([{
name: 'path',
type: 'input',
message: 'What is the path & name of the package you want to use? (eg: `packages/my-label-pkg.json`)',
validate: validateAddPackages,
}])
.then(answer => addFromPackage(repo, token, answer.path))
.catch(console.warn);
case 'create a package from labels':
banner.createPkgFromLabels();
return requestLabels(repo, token)
.then(labels => {
if (labels.length <= 0) return new Error('This repo has no labels to generate a package with!');
// tell the user what labels we are going to remove
console.log('Creating a labels package from these labels:');
labelPkg = labels.map(label => {
console.log(` - ${label.name}`);
return { name: label.name, color: `#${label.color}`, };
});
return prompt([{
name: 'name',
type: 'input',
message: 'What would you like to name this .json file?',
default: 'labels.json',
}]);
})
.then(answer => {
const _answer = answer;
if (answer.name.indexOf('.json') < 0) {
_answer.name = `${answer.name}.json`;
}
fs.writeFile(answer.name, JSON.stringify(labelPkg, null, 2), e => {
if (e) throw e;
console.log(`Saved labels as ${answer.name}`);
gitLabelmaker(token);
});
})
.catch(console.warn);
case 'remove labels':
banner.removeLabels();
// If there are no labels to be removed then we can skip this part
return requestLabels(repo, token)
.then(labels => {
if (labels.length <= 0) return new Error('This repo has no labels to remove!');
return prompt([{
name: 'removals',
type: 'checkbox',
message: 'Which labels would you like to remove?',
choices: labels.map((label) => label.name),
validate: validateRemovals,
filter: filterRemovalLabels.bind(null, labels),
}]);
})
.then(answers => {
if (answers.removals) {
return removeLabels(repo, token, answers);
}
return console.log(answers);
})
.catch(console.warn);
case 'remove all labels':
banner.removeAllLabels();
return requestLabels(repo, token)
.then(labels => {
if (labels.length !== 0) {
removeLabels(repo, token, {
removals: labels,
});
} else {
console.log('No labels to remove.');
}
})
.catch(console.warn);
default:
return gitLabelmaker();
}
};
exports.handleMainPrompts = handleMainPrompts;
// Begin our application
gitLabelmaker();