File tree Expand file tree Collapse file tree 4 files changed +53
-9
lines changed
Expand file tree Collapse file tree 4 files changed +53
-9
lines changed Original file line number Diff line number Diff line change 1919 "private" : false ,
2020 "scripts" : {
2121 "start" : " node src/index.js" ,
22- "cleanup" : " node test/cleanup.js" ,
22+ "cleanup" : " node test/cleanup.js"
2323 },
2424 "engines" : {
2525 "node" : " >=10.0.0"
Original file line number Diff line number Diff line change @@ -26,6 +26,33 @@ async function getToken(jsonPath = '') {
2626 return auth . github . token ;
2727}
2828
29+ /**
30+ * Update the Token
31+ * @param {String } token The token
32+ * @param {String } [jsonPath=''] The path for the auth.json file
33+ * @return {Boolean } True in case the file gets updated
34+ */
35+ async function updateToken ( token , jsonPath = '' ) {
36+ let auth = { } ;
37+ const authPath = jsonPath || settings . authPath ;
38+ const authFile = path . resolve ( authPath ) ;
39+
40+ try {
41+ auth = JSON . parse ( fs . readFileSync ( authFile , 'utf8' ) ) ;
42+ } catch ( error ) {
43+ throw error ;
44+ }
45+
46+ if ( auth . github . token !== token ) {
47+ auth . github . token = token ;
48+ fs . writeFileSync ( authFile , JSON . stringify ( auth ) ) ;
49+ return true ;
50+ }
51+
52+ return false ;
53+ }
54+
2955module . exports = {
3056 getToken,
57+ updateToken,
3158} ;
Original file line number Diff line number Diff line change 11const questions = require ( './questions' ) ;
22const auth = require ( '../auth' ) ;
3+ const settings = require ( '../settings' ) ;
34
45async function run ( name ) {
56 const resp = await questions . getProjectDetails ( name ) ;
@@ -14,9 +15,12 @@ async function run(name) {
1415 Object . assign ( resp , await questions . getAuthFile ( ) ) ;
1516 const token = await auth . getToken ( resp . authPath ) ;
1617 Object . assign ( resp , await questions . getAuthToken ( token ) ) ;
17- if ( resp . token !== token ) {
18- // TODO Update the token on the auth.json?
19- // TODO probably ask if they want to update it?
18+ if ( resp . token ) {
19+ if ( resp . token !== token && await auth . confirmUpdateToken ( ) ) {
20+ auth . updateToken ( resp . token , settings . authPath ) ;
21+ }
22+ } else {
23+ resp . useGithub = false ;
2024 }
2125 }
2226
Original file line number Diff line number Diff line change @@ -134,8 +134,21 @@ async function getAuthToken(token) {
134134 ] ) ;
135135}
136136
137- exports . getProjectDetails = getProjectDetails ;
138- exports . getGitRemoteDetails = getGitRemoteDetails ;
139- exports . getTestingDetails = getTestingDetails ;
140- exports . getAuthFile = getAuthFile ;
141- exports . getAuthToken = getAuthToken ;
137+ async function confirmUpdateToken ( ) {
138+ return inquirer . prompt ( [
139+ {
140+ type : 'confirm' ,
141+ name : 'updateToken' ,
142+ message : 'Do you want to update the auth.json file with this token?' ,
143+ } ,
144+ ] ) ;
145+ }
146+
147+ module . exports = {
148+ getProjectDetails,
149+ getGitRemoteDetails,
150+ getTestingDetails,
151+ getAuthFile,
152+ getAuthToken,
153+ confirmUpdateToken,
154+ } ;
You can’t perform that action at this time.
0 commit comments