Skip to content

Commit

Permalink
Merge pull request #15 from ibarsi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ibarsi authored Mar 4, 2017
2 parents 4d3ba6e + 7885ce0 commit adfd6c0
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 334 deletions.
14 changes: 13 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
{
"presets": [ "es2015" ],
"presets": [
"es2015",
"es2016",
"es2017"
],
"plugins": [
["transform-runtime", {
"helpers": false,
"polyfill": false,
"regenerator": true,
"moduleName": "babel-runtime"
}]
],
"sourceMaps": true,
"retainLines": true
}
169 changes: 80 additions & 89 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
INDEX
================================================== */

import 'babel-polyfill';
import path from 'path';
import clear from 'clear';
import chalk from 'chalk';
import figlet from 'figlet';
import inquirer from 'inquirer';

import { isFile, wrapSpinner, async } from './modules/helpers';
import { isFile, wrapSpinner } from './modules/helpers';
import { TYPES, Commits, getRepositoryTypeFromUrl } from './modules/commits';
import { FORMATS } from './modules/velocity';
import CommitsDashboard from './modules/dashboard';
Expand All @@ -33,122 +32,114 @@ console.log(

// START

async(function* () {
(async function start() {
try {
const { type } = yield getRepositoryType();
const { type } = await getRepositoryType();
const commits = Commits(type);

const isAuthorized = yield commits.isAuthorized();
const isAuthorized = await commits.isAuthorized();

if (!isAuthorized) {
console.log();
console.log(chalk.white('Creating auth token in root.'));

const { username, password } = yield getRepositoryCreds(type);
const { username, password } = await getRepositoryCreds(type);

commits.authorize(username, password);
}

console.log();
console.log(chalk.white('Provide information regarding the repository you\'d like to analyze.'));

const { repository, owner } = yield getRepositoryInfo();
const data = yield wrapSpinner(commits.getCommitsByRepo, 'Pulling commits...')(repository, owner);
const { format } = yield getVelocityFormat();
const { repository, owner } = await getRepositoryInfo();
const data = await wrapSpinner(commits.getCommitsByRepo, 'Pulling commits...')(repository, owner);
const { format } = await getVelocityFormat();

const dashboard = CommitsDashboard();
yield dashboard.render(format, data);
await dashboard.render(format, data);
}
catch (error) {
console.error(chalk.red('=== ERROR ==='));
console.log(error);
}
});
})();

// PROMPTS

function getRepositoryType() {
return new Promise(resolve => {
const questions = [
{
type: 'list',
name: 'type',
message: 'Select repository type:',
choices: [
TYPES.GITHUB,
TYPES.BITBUCKET
],
default: repository_package ?
getRepositoryTypeFromUrl(typeof repository_package.repository === 'string' ?
repository_package.repository :
repository_package.repository.type || repository_package.repository.url) :
TYPES.GITHUB
}
];

inquirer.prompt(questions).then(resolve);
});
async function getRepositoryType() {
const questions = [
{
type: 'list',
name: 'type',
message: 'Select repository type:',
choices: [
TYPES.GITHUB,
TYPES.BITBUCKET
],
default: repository_package ?
getRepositoryTypeFromUrl(typeof repository_package.repository === 'string' ?
repository_package.repository :
repository_package.repository.type || repository_package.repository.url) :
TYPES.GITHUB
}
];

return await inquirer.prompt(questions);
}

function getRepositoryCreds(type) {
return new Promise(resolve => {
const questions = [
{
name: 'username',
type: 'input',
message: `Enter ${ type } username:`,
validate: value => value.length ? true : 'Please enter a value.'
},
{
name: 'password',
type: 'password',
message: `Enter ${ type } password:`,
validate: value => value.length ? true : 'Please enter a value.'
}
];

inquirer.prompt(questions).then(resolve);
});
async function getRepositoryCreds(type) {
const questions = [
{
name: 'username',
type: 'input',
message: `Enter ${ type } username:`,
validate: value => value.length ? true : 'Please enter a value.'
},
{
name: 'password',
type: 'password',
message: `Enter ${ type } password:`,
validate: value => value.length ? true : 'Please enter a value.'
}
];

return await inquirer.prompt(questions);
}

function getRepositoryInfo() {
return new Promise(resolve => {
const questions = [
{
name: 'repository',
type: 'input',
message: 'Enter the slugged name of the repository:',
default: path.basename(process.cwd()),
validate: value => value.length ? true : 'Please enter a value.'
},
{
name: 'owner',
type: 'input',
message: 'Enter the owner of the repository:',
default: repository_package && repository_package.author ? repository_package.author : '',
validate: value => value.length ? true : 'Please enter a value.'
}
];

inquirer.prompt(questions).then(resolve);
});
async function getRepositoryInfo() {
const questions = [
{
name: 'repository',
type: 'input',
message: 'Enter the slugged name of the repository:',
default: path.basename(process.cwd()),
validate: value => value.length ? true : 'Please enter a value.'
},
{
name: 'owner',
type: 'input',
message: 'Enter the owner of the repository:',
default: repository_package && repository_package.author ? repository_package.author : '',
validate: value => value.length ? true : 'Please enter a value.'
}
];

return await inquirer.prompt(questions);
}

function getVelocityFormat() {
return new Promise(resolve => {
const questions = [
{
type: 'list',
name: 'format',
message: 'Velocity calculation format:',
choices: [
FORMATS.WEEK,
FORMATS.MONTH
],
default: FORMATS.WEEK
}
];

inquirer.prompt(questions).then(resolve);
});
async function getVelocityFormat() {
const questions = [
{
type: 'list',
name: 'format',
message: 'Velocity calculation format:',
choices: [
FORMATS.WEEK,
FORMATS.MONTH
],
default: FORMATS.WEEK
}
];

return await inquirer.prompt(questions);
}
49 changes: 16 additions & 33 deletions main/modules/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,28 @@

import fs from 'fs';

import { isFile, partial } from './helpers';

// PUBLIC
import { isFile } from './helpers';

export function Auth(token) {
return {
isCredsTokenInitialized: partial(_isCredsTokenInitialized, token),
getCreds: partial(_getCreds, token),
storeCreds: partial(_storeCreds, token)
isCredsTokenInitialized: async function() {
return isFile(`${ process.env.HOME }/${ token }`);
},
getCreds: async function() {
return JSON.parse(fs.readFileSync(`${ process.env.HOME }/${ token }`, 'utf8'));
},
storeCreds(username, password) {
return new Promise((resolve, reject) => {
fs.writeFile(
`${ process.env.HOME }/${ token }`,
JSON.stringify({ username, password }),
error => error ? reject(error) : resolve()
);
});
}
};
}

export default {
Auth
};

// PRIVATE

function _isCredsTokenInitialized(token) {
return new Promise(resolve => resolve(isFile(`${ process.env.HOME }/${ token }`)));
}

function _getCreds(token) {
return new Promise((resolve, reject) => {
try {
resolve(JSON.parse(fs.readFileSync(`${ process.env.HOME }/${ token }`, 'utf8')));
}
catch (error) {
reject(error);
}
});
}

function _storeCreds(token, username, password) {
return new Promise((resolve, reject) => {
fs.writeFile(
`${ process.env.HOME }/${ token }`,
JSON.stringify({ username, password }),
error => error ? reject(error) : resolve()
);
});
}
Loading

0 comments on commit adfd6c0

Please sign in to comment.