Skip to content

Commit 52e48ba

Browse files
zntbqwerty541Copilot
committed
refactor: move logger into separate module (anuraghazra#4581)
* refactor: move logger into separate module * Update api/status/pat-info.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Alexandr <qwerty541zxc@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent edc61d2 commit 52e48ba

9 files changed

Lines changed: 26 additions & 13 deletions

File tree

api/status/pat-info.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99

1010
import { request } from "../../src/common/http.js";
11-
import { logger, dateDiff } from "../../src/common/utils.js";
11+
import { logger } from "../../src/common/log.js";
12+
import { dateDiff } from "../../src/common/utils.js";
1213

1314
export const RATE_LIMIT_SECONDS = 60 * 5; // 1 request per 5 minutes
1415

api/status/up.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import { request } from "../../src/common/http.js";
1111
import retryer from "../../src/common/retryer.js";
12-
import { logger } from "../../src/common/utils.js";
12+
import { logger } from "../../src/common/log.js";
1313

1414
export const RATE_LIMIT_SECONDS = 60 * 5; // 1 request per 5 minutes
1515

src/common/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export {
1414
parseArray,
1515
clampValue,
1616
flexLayout,
17-
logger,
1817
measureText,
1918
lowercaseTrim,
2019
chunkArray,

src/common/log.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @ts-check
2+
3+
const noop = () => {};
4+
5+
/**
6+
* Return console instance based on the environment.
7+
*
8+
* @type {Console | {log: () => void, error: () => void}}
9+
*/
10+
const logger =
11+
process.env.NODE_ENV === "test" ? { log: noop, error: noop } : console;
12+
13+
export { logger };
14+
export default logger;

src/common/retryer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22

33
import { CustomError } from "./error.js";
4-
import { logger } from "./utils.js";
4+
import { logger } from "./log.js";
55

66
// Script variables.
77

src/common/utils.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,6 @@ const renderError = ({
211211
`;
212212
};
213213

214-
const noop = () => {};
215-
// return console instance based on the environment
216-
const logger =
217-
process.env.NODE_ENV === "test" ? { log: noop, error: noop } : console;
218-
219214
/**
220215
* Retrieve text length.
221216
*
@@ -329,7 +324,6 @@ export {
329324
parseArray,
330325
clampValue,
331326
flexLayout,
332-
logger,
333327
measureText,
334328
lowercaseTrim,
335329
chunkArray,

src/fetchers/stats.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as dotenv from "dotenv";
55
import githubUsernameRegex from "github-username-regex";
66
import { calculateRank } from "../calculateRank.js";
77
import { retryer } from "../common/retryer.js";
8-
import { logger } from "../common/utils.js";
8+
import { logger } from "../common/log.js";
99
import { excludeRepositories } from "../common/envs.js";
1010
import { CustomError, MissingParamError } from "../common/error.js";
1111
import { wrapTextMultiline } from "../common/fmt.js";

src/fetchers/top-languages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22

33
import { retryer } from "../common/retryer.js";
4-
import { logger } from "../common/utils.js";
4+
import { logger } from "../common/log.js";
55
import { excludeRepositories } from "../common/envs.js";
66
import { CustomError, MissingParamError } from "../common/error.js";
77
import { wrapTextMultiline } from "../common/fmt.js";

tests/retryer.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// @ts-check
2+
13
import { describe, expect, it, jest } from "@jest/globals";
24
import "@testing-library/jest-dom";
35
import { RETRIES, retryer } from "../src/common/retryer.js";
4-
import { logger } from "../src/common/utils.js";
6+
import { logger } from "../src/common/log.js";
57

68
const fetcher = jest.fn((variables, token) => {
79
logger.log(variables, token);
@@ -17,6 +19,7 @@ const fetcherFail = jest.fn(() => {
1719
const fetcherFailOnSecondTry = jest.fn((_vars, _token, retries) => {
1820
return new Promise((res) => {
1921
// faking rate limit
22+
// @ts-ignore
2023
if (retries < 1) {
2124
return res({ data: { errors: [{ type: "RATE_LIMITED" }] } });
2225
}
@@ -28,6 +31,7 @@ const fetcherFailWithMessageBasedRateLimitErr = jest.fn(
2831
(_vars, _token, retries) => {
2932
return new Promise((res) => {
3033
// faking rate limit
34+
// @ts-ignore
3135
if (retries < 1) {
3236
return res({
3337
data: {
@@ -72,6 +76,7 @@ describe("Test Retryer", () => {
7276
await retryer(fetcherFail, {});
7377
} catch (err) {
7478
expect(fetcherFail).toHaveBeenCalledTimes(RETRIES + 1);
79+
// @ts-ignore
7580
expect(err.message).toBe("Downtime due to GitHub API rate limiting");
7681
}
7782
});

0 commit comments

Comments
 (0)