Skip to content

Commit

Permalink
Gradient numbers in logMessages, plural util
Browse files Browse the repository at this point in the history
  • Loading branch information
Netfloex committed May 6, 2022
1 parent 76ed38b commit 339d847
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chok from "chokidar";
import type { FSWatcher } from "chokidar";
import chok from "chokidar";
import { pathExists, readdir, remove } from "fs-extra";
import { join } from "path";

Expand Down
44 changes: 28 additions & 16 deletions src/lib/logMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { YAMLException } from "js-yaml";
import { ZodIssue } from "zod";

import { Log, Tag } from "@lib/logger";
import { gradientNumber } from "@utils/gradientNumber";
import { msToDays } from "@utils/msToDays";
import { plural } from "@utils/plural";
import settings from "@utils/settings";
import { startedToSeconds } from "@utils/startedToSeconds";

Expand Down Expand Up @@ -111,9 +113,9 @@ export const logMessages = defineLogList({
requestingCertificates: ({ count = -1 }: { count: number }) => [
Log.info,
Tag.certbot,
chalk`{yellow Requesting certificates for {dim ${count}} domain${
count != 1 ? "s" : ""
}}${
chalk`{yellow Requesting certificates for {dim ${count}} domain${plural(
count
)}}${
!settings.staging
? chalk`{yellow ...}`
: chalk`, using {bold Staging} environment`
Expand Down Expand Up @@ -164,8 +166,10 @@ export const logMessages = defineLogList({
}) => [
Log.info,
Tag.certbot,
chalk`The certificate for {dim ${serverName}} is valid for {bold ${Math.round(
days
chalk`The certificate for {dim ${serverName}} is valid for {bold ${gradientNumber(
Math.round(days),
0,
90
)}} days`
],
certificateExpiry: ({
Expand All @@ -182,7 +186,9 @@ export const logMessages = defineLogList({
Tag.certbot,
chalk`{yellow The certificate for {dim ${serverName}}, expire${
hasExpired ? "d" : "s in"
} {bold ${Math.round(days)}} days ${hasExpired ? "ago" : ""}}`
} {bold ${gradientNumber(Math.round(days), 0, 90)}} days ${
hasExpired ? "ago" : ""
}}`
];
},
certificateParseFailed: ({
Expand Down Expand Up @@ -373,7 +379,7 @@ export const logMessages = defineLogList({
cachedJS: ({ url }: { url: string }) => [
Log.done,
Tag.js,
chalk`{blue JS file is already downloaded, skipping:} {dim ${url}}`
chalk`{blue JS file is already downloaded}, skipping: {dim ${url}}`
],
downloadedJS: ({ url }: { url: string }) => [
Log.done,
Expand All @@ -388,15 +394,21 @@ export const logMessages = defineLogList({
Tag.cloudflare,
chalk`{yellow Updating Cloudflare ip list...}`
],
cloudflareCached: ({ timeAgo }: { timeAgo: number }) => [
Log.info,
Tag.cloudflare,
chalk`Using cache: current ip list has been updated ${
msToDays(timeAgo) == 0
? "today"
: chalk`{dim ${msToDays(timeAgo)}} days ago`
}`
],
cloudflareCached: ({ timeAgo }: { timeAgo: number }) => {
const days = msToDays(timeAgo);

return [
Log.info,
Tag.cloudflare,
chalk`Using cache: current ip list has been updated ${
days == 0
? "today"
: `${gradientNumber(days, 0, 9, true)} day${plural(
days
)} ago`
}`
];
},
cloudflareExpired: () => [
Log.info,
Tag.cloudflare,
Expand Down
13 changes: 13 additions & 0 deletions src/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { fixedLength } from "@utils/fixedLength";
import { msToDays } from "@utils/msToDays";
import { parseCertificateExpiry } from "@utils/parseCertificateExpiry";
import { parseIntDefault } from "@utils/parseIntDefault";
import { plural } from "@utils/plural";
import { sslFilesFor } from "@utils/sslFilesFor";
import { startedToSeconds } from "@utils/startedToSeconds";

Expand Down Expand Up @@ -47,6 +48,18 @@ describe("Utilities", () => {
).toHaveLength(length);
});

test.todo("Gradient Number");

test("Plural", () => {
expect(plural(0)).toBe("s");
expect(plural(1)).toBe("");
expect(plural(2)).toBe("s");
const carsMessage = (n: number): string => `${n} car${plural(n)}`;
expect(carsMessage(0)).toBe("0 cars");
expect(carsMessage(1)).toBe("1 car");
expect(carsMessage(2)).toBe("2 cars");
});

test("Milliseconds to days", () => {
const msPerDay = 1000 * 60 * 60 * 24;
expect(msToDays(10 * msPerDay)).toBe(10);
Expand Down
33 changes: 33 additions & 0 deletions src/utils/gradientNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import chalk from "chalk";

const colors: chalk.Chalk[] = [
chalk.redBright,
chalk.red,
chalk.yellowBright,
chalk.yellow,
chalk.greenBright,
chalk.green
];
/*
Returns a colorized number depending on its value
Lower numbers are red,
Medium numbers yellow,
High values green
if reverse is true, this list is reversed
*/
export const gradientNumber = (
number: number,
low: number,
high: number,
reverse = false
): string => {
const diffPerColor = (high - low) / colors.length;
const colorIndex = Math.floor(number / diffPerColor);

// Clamp between the length of array

const ci = Math.max(0, Math.min(colorIndex, colors.length - 1));

return (reverse ? colors.reverse() : colors)[ci](number);
};
4 changes: 4 additions & 0 deletions src/utils/plural.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Adds an 's' if the number is not equal to 0
*/
export const plural = (count: number): "" | "s" => (count != 1 ? "s" : "");

0 comments on commit 339d847

Please sign in to comment.