-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
65 lines (59 loc) · 1.67 KB
/
github.js
File metadata and controls
65 lines (59 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const inquirer = require("./inquirer");
const { createBasicAuth } = require("@octokit/auth");
const { Octokit } = require("@octokit/rest");
const Configstore = require("configstore");
const packageJson = require("./package.json");
const CLI = require("clui");
const Spinner = CLI.Spinner;
const conf = new Configstore(packageJson.name);
let octokit;
const getStoredGithubToken = () => conf.get("github.token");
const getPersonalGithubToken = async () => {
const status = new Spinner("Authenticating you, please wait...");
try {
const { username, password } = await inquirer.askGithubCredentials();
status.start();
const auth = createBasicAuth({
username,
password,
async on2Fa() {
status.stop();
const { code } = await inquirer.getTwoFactorAuthenticationCode();
status.start();
return code;
},
token: {
scopes: ["user", "public_repo", "repo", "repo:status"],
note: "ginit, the command-line tool for initalizing Git repos",
},
});
const res = await auth();
if (res.token) {
conf.set("github.token", res.token);
return res.token;
} else {
throw new Error("GitHub token was not found in the response");
}
} catch (e) {
console.error(e);
} finally {
status.stop();
}
};
module.exports = {
authenticate: async () => {
let token;
token = getStoredGithubToken();
if (!token) {
token = await getPersonalGithubToken();
}
console.log("token -----: ", token);
octokit = new Octokit({ auth: token });
},
getInstance: () => {
return octokit;
},
clear: () => conf.clear(),
getStoredGithubToken,
getPersonalGithubToken,
};