|
4 | 4 | const fs = require("fs"),
|
5 | 5 | iq = require("inquirer"),
|
6 | 6 | gitLabel = require("git-label"),
|
7 |
| - prompts = require("./components/prompts"), |
8 | 7 | 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"); |
10 | 11 |
|
11 | 12 |
|
12 | 13 | // Responsible for fetching and returning our config/token
|
@@ -37,16 +38,17 @@ const setToken = (done) => {
|
37 | 38 | });
|
38 | 39 | };
|
39 | 40 | // 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 ) => { |
42 | 43 | newLabels.push({ name: answers.labelName, color: answers.labelColor });
|
43 | 44 | if ( answers.addAnother ){
|
44 |
| - doPrompts( newLabels, done ); |
| 45 | + doAddPrompts( newLabels, done ); |
45 | 46 | }else{
|
46 | 47 | done( newLabels );
|
47 | 48 | }
|
48 | 49 | });
|
49 | 50 | };
|
| 51 | + |
50 | 52 | // Promise-based check to see if we're even in a Git repo
|
51 | 53 | const isGitRepo = () => {
|
52 | 54 | return new Promise((res, rej) => {
|
@@ -74,30 +76,40 @@ const configGitLabel = (repo, token) => {
|
74 | 76 | token: token
|
75 | 77 | }
|
76 | 78 | };
|
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); |
85 | 84 | };
|
86 | 85 |
|
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)); |
93 | 90 | })
|
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 | + }); |
98 | 102 | });
|
99 |
| - }); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + |
| 107 | +// LET'S DO IT |
100 | 108 |
|
| 109 | + Promise.all([ isGitRepo(), readGitConfig() ]) |
| 110 | + .then(( values )=>{ |
| 111 | + let repo = readRepo(values[1].split("\n")); |
| 112 | + iq.prompt( mainPrompts, handleMainPrompts.bind(null, repo)); |
101 | 113 | })
|
102 | 114 | .catch((e)=>{
|
103 | 115 | console.warn(e);
|
|
0 commit comments