@seedneey/gint
Version:
creating a repo (in Github) for your project in one command
141 lines (127 loc) • 4.33 kB
JavaScript
const CLI = require("clui");
const Configstore = require("configstore");
const { Octokit } = require("@octokit/rest");
const Spinner = CLI.Spinner;
const { createBasicAuth } = require("@octokit/auth-basic");
var keygen = require("ssh-keygen");
const homedir = require("os").homedir();
const path = require("path");
const { resolve } = require("path");
const request = require("request");
const fs = require("fs");
const pkg = require("../package.json");
const conf = new Configstore(pkg.name);
const files = require("./files");
const inquirer = require("./inquirer");
const chalk = require("chalk");
let octokit;
module.exports = {
getInstance: () => {
return octokit;
},
getStoredGithubToken: () => {
return conf.get("github.token");
},
githubAuth: (token) => {
octokit = new Octokit({
auth: token,
});
},
getPersonalAccesToken: async () => {
const credentials = await inquirer.askGithubCredentials();
const status = new Spinner("Authenticating you, please wait...");
status.start();
const auth = createBasicAuth({
username: credentials.username,
password: credentials.password,
async on2Fa() {
status.stop();
const res = await inquirer.getTwoFactorAuthenticationCode();
status.start();
return res.twoFactorAuthenticationCode;
},
token: {
scopes: ["user", "public_repo", "repo", "repo:status"],
note: "gint, the command-line tool for initalizing Git repos",
},
});
try {
const res = await auth();
if (res.token) {
if (files.directoryExists(path.join(homedir, "/.ssh/id_rsa.pub"))) {
// const sshKeys = JSON.parse(
// require(path.join(homedir, "/.ssh/id_rsa.pub"))
// );
let sshKeys = fs.readFileSync(
resolve(`${homedir}/.ssh/id_rsa.pub`),
"utf8"
);
sshKeys = sshKeys.replace(/(\r\n|\n|\r)/gm, "");
var dataString = `{"title":"${res.username}","key":"${sshKeys}"}`;
var options = {
url: "https://api.github.com/user/keys",
method: "POST",
body: dataString,
headers: { "User-Agent": `${res.username}` },
auth: {
user: `${res.username}`,
pass: `${credentials.password}`,
},
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
// console.log(body);
} else {
console.log("error po");
// console.log(dataString);
}
}
request(options, callback);
conf.set("github.token", res.token);
conf.set("github.username");
return res.token;
} else {
await keygen(
{
location: path.join(homedir, "/.ssh/id_rsa"),
comment: res.username,
read: true,
},
(err, out) => {
if (err) return console.log("Something went wrong: " + err);
const sshKeys = out.pubKey.replace(/(\r\n|\n|\r)/gm, "");
var dataString = `{"title":"${res.username}","key":"${sshKeys}"}`;
var options = {
url: "https://api.github.com/user/keys",
method: "POST",
body: dataString,
headers: { "User-Agent": `sbercasio` },
auth: {
user: `${res.username}`,
pass: `${credentials.password}`,
},
};
function callback(error, response, body) {
if (!error && response.statusCode == 201) {
// console.log(body);
} else {
console.log("error po1");
console.log(response.statusCode);
}
}
request(options, callback);
}
);
conf.set("github.token", res.token);
conf.set("github.username");
return res.token;
}
} else {
throw new Error("GitHub token was not found in the response");
}
} finally {
status.stop();
console.log(chalk.green("✔️ Github account authenticated"));
}
},
};