Skip to content

Commit 06ad35a

Browse files
Merge pull request himynameisdave#7 from himynameisdave/reset-token
Ability to reset the token
2 parents e3e4af5 + 9d6184a commit 06ad35a

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed
File renamed without changes.

components/main-prompts.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
module.exports = [
4+
{
5+
type: "list",
6+
name: "main",
7+
message: "Welcome to git-labelmaker!\nWhat would you like to do?",
8+
choices: [ "Add Labels", "Reset Token" ]
9+
}
10+
];

index.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
const fs = require("fs"),
55
iq = require("inquirer"),
66
gitLabel = require("git-label"),
7-
prompts = require("./components/prompts"),
87
readRepo = require("./components/read-repo"),
9-
cleanup = require("./components/remove-tmp-pkg");
8+
cleanup = require("./components/remove-tmp-pkg"),
9+
mainPrompts = require("./components/main-prompts"),
10+
addPrompts = require("./components/add-prompts");
1011

1112

1213
// Responsible for fetching and returning our config/token
@@ -37,16 +38,17 @@ const setToken = (done) => {
3738
});
3839
};
3940
// Recursivly asks if you want to add another new label, then calls a callback when youre all done
40-
const doPrompts = ( newLabels, done ) => {
41-
iq.prompt( prompts, ( answers ) => {
41+
const doAddPrompts = ( newLabels, done ) => {
42+
iq.prompt( addPrompts, ( answers ) => {
4243
newLabels.push({ name: answers.labelName, color: answers.labelColor });
4344
if ( answers.addAnother ){
44-
doPrompts( newLabels, done );
45+
doAddPrompts( newLabels, done );
4546
}else{
4647
done( newLabels );
4748
}
4849
});
4950
};
51+
5052
// Promise-based check to see if we're even in a Git repo
5153
const isGitRepo = () => {
5254
return new Promise((res, rej) => {
@@ -74,30 +76,40 @@ const configGitLabel = (repo, token) => {
7476
token: token
7577
}
7678
};
77-
// Responsible for actually calling the prompts
78-
const handlePrompts = ( repo, token ) => {
79-
doPrompts( [], (newLabels) => {
80-
console.log(newLabels);
81-
gitLabel.add( configGitLabel(repo, token), newLabels )
82-
.then(console.log)
83-
.catch(console.warn);
84-
});
79+
80+
const handleAddPrompts = (repo, token, newLabels) => {
81+
gitLabel.add( configGitLabel(repo, token), newLabels )
82+
.then(console.log)
83+
.catch(console.warn);
8584
};
8685

87-
Promise.all([ isGitRepo(), readGitConfig() ])
88-
.then(( values )=>{
89-
let repo = readRepo(values[1].split("\n"));
90-
fetchToken()
91-
.then((token)=>{
92-
handlePrompts( repo, token );
86+
const handleMainPrompts = (repo, ans) => {
87+
if ( ans.main.toLowerCase() === "reset token" ){
88+
setToken((token) => {
89+
iq.prompt( mainPrompts, handleMainPrompts.bind(null, repo));
9390
})
94-
.catch((msg)=>{
95-
console.log(msg);
96-
setToken((token) => {
97-
handlePrompts( repo, token );
91+
}
92+
if ( ans.main.toLowerCase() === "add labels" ){
93+
fetchToken()
94+
.then((token)=>{
95+
doAddPrompts( [], handleAddPrompts.bind(null, repo, token));
96+
})
97+
.catch((msg)=>{
98+
console.log(msg);
99+
setToken((token) => {
100+
iq.prompt( mainPrompts, handleMainPrompts.bind(null, repo));
101+
});
98102
});
99-
});
103+
}
104+
};
105+
106+
107+
// LET'S DO IT
100108

109+
Promise.all([ isGitRepo(), readGitConfig() ])
110+
.then(( values )=>{
111+
let repo = readRepo(values[1].split("\n"));
112+
iq.prompt( mainPrompts, handleMainPrompts.bind(null, repo));
101113
})
102114
.catch((e)=>{
103115
console.warn(e);

0 commit comments

Comments
 (0)