Skip to content

Commit

Permalink
🌱 Print the output table with ink
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed Dec 1, 2019
1 parent 62e67b5 commit 143db89
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
20 changes: 13 additions & 7 deletions calcMetrics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const axios = require("axios");
const moment = require("moment");
const importJsx = require('import-jsx');
const table = importJsx('./table');

const getPullRequestsData = (projectName, githubToken, sprint) => axios({
url: 'https://api.github.com/graphql',
Expand Down Expand Up @@ -61,11 +63,10 @@ query {
{ key: 'Requirement', emoji: '📚'},
{ key: 'Coding', emoji: '💅'},
];
console.log(` ${columns.map(({key}) => key).join(' ')} Size Title`);
if (!result.data.data.repository) console.log(result.data.errors);
if (!result.data.data.repository) console.error(result.data.errors);
const pullRequests = result.data.data.repository.pullRequests.edges;
if (!pullRequests) console.log(result.data.data);
pullRequests.filter(pr => (
const data = pullRequests.filter(pr => (
moment(pr.node.mergedAt).isSameOrBefore(moment(sprint.endDate)) &&
moment(pr.node.mergedAt).isSameOrAfter(moment(sprint.endDate).subtract(sprint.lengthDays, 'day'))
)).map(pr => {
Expand All @@ -80,12 +81,17 @@ query {
});
return acc;
}, columns.reduce((accumulator, { key }) => ({ ...accumulator, [key]: 0}), {}));
const columnsDataPrint = columns.map(({key}) => `${prCalc[key]}`.padEnd(key.length)).join(' ');
const prSize = `+${pr.node.additions}/-${pr.node.deletions}`.padEnd(10);
console.log(`${String(pr.node.number).padEnd(4)} ${columnsDataPrint} ${prSize} ${pr.node.title}`);

return {
'#': `#${pr.node.number}`,
...prCalc,
Size: `+${pr.node.additions}/-${pr.node.deletions}`.padEnd(10),
Title: pr.node.title,
};
});
table.printTable(data);
}, error => {
console.log(error);
console.error(error);
});

const calculateNumberOf = (commentsArray, emoji) => commentsArray.reduce((acc, comment) => acc + comment.node.bodyText.split(emoji).length - 1, 0);
Expand Down
36 changes: 22 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
{
"name": "untitled",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "0.19.0",
"moment": "=2.24.0"
}
"name": "untitled",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "0.19.0",
"import-jsx": "=3.0.0",
"ink": "=2.5.0",
"ink-table": "=2.0.1",
"moment": "=2.24.0",
"react": "=16.10.2"
},
"devDependencies": {
"@babel/core": "=7.6.4",
"@babel/preset-react": "=7.6.3"
}
}
21 changes: 21 additions & 0 deletions table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const React = require('react');
const { render, Color } = require('ink');
const Table = require('ink-table').default;

const CustomCell = ({children}) => {
const asNumber = Number(children[1].trim());
const color = asNumber > 3 ? { red: true } : {};
return <Color {...color}>{children}</Color>;
};

const PrTable = ({ data }) => (
<Table data={data} padding={0} cell={CustomCell}/>
);

const printTable = (data) => {
render(<PrTable data={data}/>);
};

module.exports = {
printTable,
};

0 comments on commit 143db89

Please sign in to comment.