Skip to content

Commit e1ed603

Browse files
committed
Update the token on the auth.json
1 parent fe59ead commit e1ed603

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
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"

src/auth/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
2955
module.exports = {
3056
getToken,
57+
updateToken,
3158
};

src/questionnaire/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const questions = require('./questions');
22
const auth = require('../auth');
3+
const settings = require('../settings');
34

45
async 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

src/questionnaire/questions.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff 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+
};

0 commit comments

Comments
 (0)