UNPKG

vinit

Version:

Automatic Generation repo

84 lines (81 loc) 2.87 kB
const octokit = require('@octokit/rest')(); // github api const Configstore = require('configstore'); // stroing Config const pkg = require('../package.json'); // package.json const _ = require('lodash'); // a tool lib const CLI = require('clui'); // witing icon tip show const Spinner = CLI.Spinner; const chalk = require('chalk'); const inquirer = require('./inquirer'); const conf = new Configstore(pkg.name); const status = new Spinner('😘 Authenticating you, please wait...'); module.exports = { getInstance: function () { return octokit; }, githubAuth: (token) => { octokit.authenticate({ type: 'oauth', token: token }); }, getStoredGithubToken: function () { return conf.get('github.token'); }, setGithubCredentials: async function () { const creatCredential = await inquirer.askGithubCredentials(); octokit.authenticate( _.extend( { type: 'basic', }, creatCredential ) ) }, registerNewToken: async function () { status.start(); try { const response = await octokit.authorization.create({ scopes: ['user', 'public_repo', 'repo', 'repo:status'], note: 'vinit, the command-line tool for initalizing Git repos' }); const token = response.data.token; if (token) { conf.set('github.token', token); return token; } else { throw new Error("Missing Token", "GitHub token was not found in the response"); } } catch (error) { throw error; } finally { status.stop(); } }, hasAccessToken : async () => { const status = new Spinner('Authenticating you, please wait...'); status.start(); try { const response = await octokit.authorization.getAll(); const accessToken = _.find(response.data, (row) => { if(row.note) { return row.note.indexOf('vinit') !== -1; } }); return accessToken; } catch (err) { throw err; } finally { status.stop(); } }, regenerateNewToken : async (id) => { const tokenUrl = 'https://github.com/settings/tokens/' + id; console.log('Please visit ' + chalk.underline.blue.bold(tokenUrl) + ' and click the ' + chalk.red.bold('Regenerate Token Button.\n')); const input = await inquirer.askRegeneratedToken(); if(input) { conf.set('github.token', input.token); return input.token; } } }