Skip to content

Commit 05a380b

Browse files
committed
Validation for auth on questionnaire and object for project defaults
1 parent f51a6ab commit 05a380b

File tree

6 files changed

+104
-25
lines changed

6 files changed

+104
-25
lines changed

auth-example.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
2-
"github": {
3-
"token": "YOUR_TOKEN"
4-
}
2+
"github": [
3+
{
4+
"user": "YOUR_USER",
5+
"token": "YOUR_TOKEN"
6+
},
7+
{
8+
"user": "OTHER_USER",
9+
"token": "OTHER_TOKEN"
10+
}
11+
]
512
}

src/auth/index.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
5596
module.exports = {
5697
getToken,
5798
updateToken,
99+
getFirstUser,
58100
};

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ async function run() {
2828
throw new Error(`The project folder '${destPath} already exists. You need to specify a different path.`);
2929
}
3030

31+
const defaults = {
32+
projectName: projectFolder,
33+
gitUserName: await gitHandler.userValue('name'),
34+
gitUserEmail: await gitHandler.userValue('email'),
35+
license: settings.default.license,
36+
version: settings.default.version,
37+
};
38+
3139
// Questionnaire for the options
32-
const answers = await questionnaire.run(projectFolder);
40+
const answers = await questionnaire.run(defaults);
3341

3442
// Add extra values to the answers
3543
Object.assign(answers, {

src/questionnaire/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ async function run(name) {
1111
} else {
1212
Object.assign(resp, await questions.getAuthFile());
1313

14-
const token = await auth.getToken(resp.authPath);
14+
const githubUser = auth.getFirstUser(resp.authPath);
1515

16-
Object.assign(resp, await questions.getAuthToken(token));
16+
Object.assign(resp, await questions.getGithubUser(githubUser));
17+
18+
const token = auth.getToken(resp.authUser, resp.authPath);
19+
20+
Object.assign(resp, await questions.getAuthToken(resp.authUser, token));
1721

1822
if (resp.token) {
19-
if (resp.token !== token && await auth.confirmUpdateToken()) {
20-
auth.updateToken(resp.token, settings.authPath);
23+
if (resp.token !== token && auth.confirmUpdateToken()) {
24+
auth.updateToken(resp.authUser, resp.token, settings.authPath);
2125
}
2226
} else {
2327
resp.useGithub = false;

src/questionnaire/questions.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ const fs = require('fs');
44
const settings = require('../settings');
55
const utils = require('../utils');
66

7-
async function getProjectDetails(name) {
7+
async function getProjectDetails(defaults) {
88
return inquirer.prompt([
99
{
1010
type: 'input',
1111
name: 'name',
1212
message: 'What is your project name?',
13-
default: name,
13+
default: defaults.projectName,
1414
},
1515

1616
{
@@ -23,7 +23,7 @@ async function getProjectDetails(name) {
2323
type: 'input',
2424
name: 'version',
2525
message: 'What version do you want to start with?',
26-
default: '0.1.0',
26+
default: defaults.version,
2727
},
2828

2929
{
@@ -38,19 +38,21 @@ async function getProjectDetails(name) {
3838
name: 'license',
3939
message: 'Please select a license',
4040
choices: settings.licenses,
41-
default: 'MIT',
41+
default: defaults.license,
4242
},
4343

4444
{
4545
type: 'input',
4646
name: 'author.name',
4747
message: 'What is your name?',
48+
default: defaults.gitUserName,
4849
},
4950

5051
{
5152
type: 'input',
5253
name: 'author.email',
5354
message: 'What is your email?',
55+
default: defaults.gitUserEmail,
5456
},
5557

5658
{
@@ -123,12 +125,23 @@ async function getAuthFile() {
123125
]);
124126
}
125127

126-
async function getAuthToken(token) {
128+
async function getGithubUser(user) {
129+
return inquirer.prompt([
130+
{
131+
type: 'input',
132+
name: 'authUser',
133+
message: 'What is your github user?',
134+
default: user,
135+
},
136+
]);
137+
}
138+
139+
async function getAuthToken(user, token) {
127140
return inquirer.prompt([
128141
{
129142
type: 'input',
130143
name: 'token',
131-
message: 'What is your GitHub token?',
144+
message: `What is your GitHub token for user ${user}?`,
132145
default: token,
133146
},
134147
]);
@@ -150,4 +163,5 @@ module.exports = {
150163
getAuthFile,
151164
getAuthToken,
152165
confirmUpdateToken,
166+
getGithubUser,
153167
};

src/settings/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const settings = {
2929
'MIT',
3030
],
3131
authPath: path.join(os.homedir(), 'auth.json'),
32+
default: {
33+
license: 'MIT',
34+
version: '0.1.0',
35+
},
3236
};
3337

3438
module.exports = settings;

0 commit comments

Comments
 (0)