@@ -5,10 +5,11 @@ const utils = require('../utils');
55
66/**
77 * Get the Github token from the auth file
8- * @param {String } [path=''] The path for the auth.json file
9- * @return {String } The github token
8+ * @param {String } [user=undefined] The user owner of the token
9+ * @param {String } [path=''] The path for the auth.json file
10+ * @return {String } The github token
1011 */
11- async function getToken ( jsonPath = '' ) {
12+ function getToken ( user = undefined , jsonPath = '' ) {
1213 let auth = { } ;
1314 const authPath = jsonPath || settings . authPath ;
1415 const authFile = utils . resolvePath ( authPath ) ;
@@ -19,21 +20,30 @@ async function getToken(jsonPath = '') {
1920 throw error ;
2021 }
2122
22- if ( ! auth . github . token ) {
23+ let { token } = auth . github [ 0 ] ;
24+
25+ if ( user ) {
26+ token = auth . github . filter ( obj => obj . user === user ) [ 0 ] . token ;
27+ }
28+
29+ if ( ! token ) {
2330 throw new Error ( 'Token missing' ) ;
2431 }
2532
26- return auth . github . token ;
33+ return token ;
2734}
2835
2936/**
3037 * 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
38+ * @param {String } [user=undefined] The user owner of the token
39+ * @param {String } token The token
40+ * @param {String } [jsonPath=''] The path for the auth.json file
41+ * @return {Boolean } True in case the file gets updated
3442 */
35- async function updateToken ( token , jsonPath = '' ) {
43+ function updateToken ( user = undefined , token , jsonPath = '' ) {
3644 let auth = { } ;
45+ let currentToken = '' ;
46+ let userIndex = 0 ;
3747 const authPath = jsonPath || settings . authPath ;
3848 const authFile = utils . resolvePath ( authPath ) ;
3949
@@ -43,16 +53,48 @@ async function updateToken(token, jsonPath = '') {
4353 throw error ;
4454 }
4555
46- if ( auth . github . token !== token ) {
47- auth . github . token = token ;
56+ currentToken = auth . github [ 0 ] . token ;
57+
58+ if ( user ) {
59+ for ( let k = 0 ; k < auth . github . length ; k += 1 ) {
60+ if ( auth . github [ k ] . user === user ) {
61+ userIndex = k ;
62+ currentToken = auth . github [ k ] . token ;
63+ break ;
64+ }
65+ }
66+ }
67+
68+ if ( currentToken !== token ) {
69+ auth . github [ userIndex ] . token = token ;
4870 fs . writeFileSync ( authFile , JSON . stringify ( auth ) ) ;
4971 return true ;
5072 }
5173
5274 return false ;
5375}
5476
77+ /**
78+ * Get the first user from the auth file
79+ * @param {String } [jsonPath=''] The auth.json file path
80+ * @return {String } The user
81+ */
82+ function getFirstUser ( jsonPath = '' ) {
83+ let auth = { } ;
84+ const authPath = jsonPath || settings . authPath ;
85+ const authFile = utils . resolvePath ( authPath ) ;
86+
87+ try {
88+ auth = JSON . parse ( fs . readFileSync ( authFile , 'utf8' ) ) ;
89+ } catch ( error ) {
90+ throw error ;
91+ }
92+
93+ return auth . github [ 0 ] . user ;
94+ }
95+
5596module . exports = {
5697 getToken,
5798 updateToken,
99+ getFirstUser,
58100} ;
0 commit comments