Skip to content

Commit 5ed75e1

Browse files
rjoydipanuraghazra
andauthoredJul 23, 2020
feat: added isTemplate badge & refactored console.logs (anuraghazra#146)
* feat: template option added husky added for same commit disable console in test \ logger utils added env checked for log modified git ignore * changed are done as per the suggesstion * changed style and font * text color dynamic * fix border and using .bagde class as common * simplified the badge svg code through a common method * chore: updated css & fixed tests Co-authored-by: anuraghazra <hazru.anurag@gmail.com>
1 parent 03f55e8 commit 5ed75e1

12 files changed

+78
-33
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
.env
33
node_modules
44
package-lock.json
5+
*.lock
6+
.vscode/
57
coverage

‎api/pin.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
parseBoolean,
55
clampValue,
66
CONSTANTS,
7+
logger,
78
} = require("../src/utils");
89
const fetchRepo = require("../src/fetchRepo");
910
const renderRepoCard = require("../src/renderRepoCard");
@@ -28,7 +29,7 @@ module.exports = async (req, res) => {
2829
try {
2930
repoData = await fetchRepo(username, repo);
3031
} catch (err) {
31-
console.log(err);
32+
logger.error(err);
3233
return res.send(renderError(err.message));
3334
}
3435

@@ -52,7 +53,7 @@ module.exports = async (req, res) => {
5253
}
5354

5455
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
55-
56+
5657
res.send(
5758
renderRepoCard(repoData, {
5859
title_color,

‎package.json

+6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
"axios": "^0.19.2",
1616
"axios-mock-adapter": "^1.18.1",
1717
"css-to-object": "^1.1.0",
18+
"husky": "^4.2.5",
1819
"jest": "^26.1.0"
1920
},
2021
"dependencies": {
2122
"dotenv": "^8.2.0"
23+
},
24+
"husky": {
25+
"hooks": {
26+
"pre-commit": "npm test"
27+
}
2228
}
2329
}

‎src/fetchRepo.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const fetcher = (variables, token) => {
1010
nameWithOwner
1111
isPrivate
1212
isArchived
13+
isTemplate
1314
stargazers {
1415
totalCount
1516
}

‎src/fetchStats.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { request } = require("./utils");
1+
const { request, logger } = require("./utils");
22
const retryer = require("./retryer");
33
const calculateRank = require("./calculateRank");
44
require("dotenv").config();
@@ -61,7 +61,7 @@ async function fetchStats(username) {
6161
let res = await retryer(fetcher, { login: username });
6262

6363
if (res.data.errors) {
64-
console.log(res.data.errors);
64+
logger.error(res.data.errors);
6565
throw Error(res.data.errors[0].message || "Could not fetch user");
6666
}
6767

‎src/fetchTopLanguages.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { request } = require("./utils");
1+
const { request, logger } = require("./utils");
22
const retryer = require("./retryer");
33
require("dotenv").config();
44

@@ -38,7 +38,7 @@ async function fetchTopLanguages(username) {
3838
let res = await retryer(fetcher, { login: username });
3939

4040
if (res.data.errors) {
41-
console.log(res.data.errors);
41+
logger.error(res.data.errors);
4242
throw Error(res.data.errors[0].message || "Could not fetch user");
4343
}
4444

‎src/renderRepoCard.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const renderRepoCard = (repo, options = {}) => {
1414
primaryLanguage,
1515
stargazers,
1616
isArchived,
17+
isTemplate,
1718
forkCount,
1819
} = repo;
1920
const {
@@ -49,14 +50,20 @@ const renderRepoCard = (repo, options = {}) => {
4950
const totalStars = kFormatter(stargazers.totalCount);
5051
const totalForks = kFormatter(forkCount);
5152

52-
const archiveBadge = isArchived
53-
? `
54-
<g data-testid="archive-badge" class="archive-badge" transform="translate(320, 38)">
53+
const getBadgeSVG = (label) => `
54+
<g data-testid="badge" class="badge" transform="translate(320, 38)">
5555
<rect stroke="${textColor}" stroke-width="1" width="70" height="20" x="-12" y="-14" ry="10" rx="10"></rect>
56-
<text fill="${textColor}">Archived</text>
56+
<text
57+
x="23" y="-5"
58+
alignment-baseline="central"
59+
dominant-baseline="central"
60+
text-anchor="middle"
61+
fill="${textColor}"
62+
>
63+
${label}
64+
</text>
5765
</g>
58-
`
59-
: "";
66+
`;
6067

6168
const svgLanguage = `
6269
<g transform="translate(30, 100)">
@@ -90,17 +97,25 @@ const renderRepoCard = (repo, options = {}) => {
9097
.description { font: 400 13px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
9198
.gray { font: 400 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
9299
.icon { fill: ${iconColor} }
93-
.archive-badge { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; }
94-
.archive-badge rect { opacity: 0.2 }
100+
.badge { font: 600 11px 'Segoe UI', Ubuntu, Sans-Serif; }
101+
.badge rect { opacity: 0.2 }
95102
</style>
103+
96104
<rect data-testid="card-bg" x="0.5" y="0.5" width="399" height="99%" rx="4.5" fill="${bgColor}" stroke="#E4E2E2"/>
97105
<svg class="icon" x="25" y="25" viewBox="0 0 16 16" version="1.1" width="16" height="16">
98106
${icons.contribs}
99107
</svg>
100108
101-
${archiveBadge}
102-
103109
<text x="50" y="38" class="header">${header}</text>
110+
111+
${
112+
isTemplate
113+
? getBadgeSVG("Template")
114+
: isArchived
115+
? getBadgeSVG("Archived")
116+
: ""
117+
}
118+
104119
<text class="description" x="25" y="70">${encodeHTML(desc)}</text>
105120
106121
${svgLanguage}

‎src/retryer.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
const { logger } = require("./utils");
2+
13
const retryer = async (fetcher, variables, retries = 0) => {
24
if (retries > 7) {
35
throw new Error("Maximum retries exceeded");
46
}
57
try {
6-
console.log(`Trying PAT_${retries + 1}`);
8+
logger.log(`Trying PAT_${retries + 1}`);
79

810
// try to fetch with the first token since RETRIES is 0 index i'm adding +1
911
let response = await fetcher(
@@ -18,7 +20,7 @@ const retryer = async (fetcher, variables, retries = 0) => {
1820
// if rate limit is hit increase the RETRIES and recursively call the retryer
1921
// with username, and current RETRIES
2022
if (isRateExceeded) {
21-
console.log(`PAT_${retries + 1} Failed`);
23+
logger.log(`PAT_${retries + 1} Failed`);
2224
retries++;
2325
// directly return from the function
2426
return retryer(fetcher, variables, retries);
@@ -32,7 +34,7 @@ const retryer = async (fetcher, variables, retries = 0) => {
3234
const isBadCredential = err.response.data && err.response.data.message === "Bad credentials";
3335

3436
if (isBadCredential) {
35-
console.log(`PAT_${retries + 1} Failed`);
37+
logger.log(`PAT_${retries + 1} Failed`);
3638
retries++;
3739
// directly return from the function
3840
return retryer(fetcher, variables, retries);

‎src/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ function getCardColors({
116116
return { titleColor, iconColor, textColor, bgColor };
117117
}
118118

119+
const fn = () => {};
120+
// return console instance based on the environment
121+
const logger = process.env.NODE_ENV !== "test" ? console : { log: fn, error: fn };
122+
119123
const CONSTANTS = {
120124
THIRTY_MINUTES: 1800,
121125
TWO_HOURS: 7200,
@@ -133,5 +137,6 @@ module.exports = {
133137
FlexLayout,
134138
getCardColors,
135139
clampValue,
140+
logger,
136141
CONSTANTS,
137142
};

‎tests/pin.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const data_repo = {
1717
name: "TypeScript",
1818
},
1919
forkCount: 100,
20+
isTemplate: false
2021
},
2122
};
2223

‎tests/renderRepoCard.test.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -217,33 +217,22 @@ describe("Test renderRepoCard", () => {
217217
);
218218
});
219219

220-
it("should render archive badge if repo is archived", () => {
221-
document.body.innerHTML = renderRepoCard({
222-
...data_repo.repository,
223-
isArchived: true,
224-
});
225-
226-
expect(queryByTestId(document.body, "archive-badge")).toHaveTextContent(
227-
"Archived"
228-
);
229-
});
230-
231220
it("should not render star count or fork count if either of the are zero", () => {
232221
document.body.innerHTML = renderRepoCard({
233222
...data_repo.repository,
234223
stargazers: { totalCount: 0 },
235224
});
236225

237226
expect(queryByTestId(document.body, "stargazers")).toBeNull();
238-
expect(queryByTestId(document.body, "forkcount")).toBeDefined();
227+
expect(queryByTestId(document.body, "forkcount")).toBeInTheDocument();
239228

240229
document.body.innerHTML = renderRepoCard({
241230
...data_repo.repository,
242231
stargazers: { totalCount: 1 },
243232
forkCount: 0,
244233
});
245234

246-
expect(queryByTestId(document.body, "stargazers")).toBeDefined();
235+
expect(queryByTestId(document.body, "stargazers")).toBeInTheDocument();
247236
expect(queryByTestId(document.body, "forkcount")).toBeNull();
248237

249238
document.body.innerHTML = renderRepoCard({
@@ -255,4 +244,26 @@ describe("Test renderRepoCard", () => {
255244
expect(queryByTestId(document.body, "stargazers")).toBeNull();
256245
expect(queryByTestId(document.body, "forkcount")).toBeNull();
257246
});
247+
248+
it("should render badges", () => {
249+
document.body.innerHTML = renderRepoCard({
250+
...data_repo.repository,
251+
isArchived: true,
252+
});
253+
254+
expect(queryByTestId(document.body, "badge")).toHaveTextContent("Archived");
255+
256+
document.body.innerHTML = renderRepoCard({
257+
...data_repo.repository,
258+
isTemplate: true,
259+
});
260+
expect(queryByTestId(document.body, "badge")).toHaveTextContent("Template");
261+
});
262+
263+
it("should not render template", () => {
264+
document.body.innerHTML = renderRepoCard({
265+
...data_repo.repository,
266+
});
267+
expect(queryByTestId(document.body, "badge")).toBeNull();
268+
});
258269
});

‎tests/retryer.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
require("@testing-library/jest-dom");
22
const retryer = require("../src/retryer");
3+
const { logger } = require("../src/utils");
34

45
const fetcher = jest.fn((variables, token) => {
5-
console.log(variables, token);
6+
logger.log(variables, token);
67
return new Promise((res, rej) => res({ data: "ok" }));
78
});
89

0 commit comments

Comments
 (0)
Please sign in to comment.