Skip to content

Commit 2f8a7c4

Browse files
authored
refactor: replace kleur with native Node.js styleText API (#484)
1 parent 8bb4377 commit 2f8a7c4

File tree

9 files changed

+59
-21
lines changed

9 files changed

+59
-21
lines changed

bin/commands/execute.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { inspect } from "node:util";
66

77
// Import Third-party Dependencies
88
import * as rc from "@nodesecure/rc";
9-
import kleur from "kleur";
109

1110
// Import Internal Dependencies
1211
import { store } from "../../src/localStorage.ts";
12+
import * as utils from "../../src/utils/index.ts";
13+
14+
const { formatter } = utils;
1315

1416
import { fetchPackagesAndRepositoriesData } from "../../src/analysis/fetch.ts";
1517
import * as CONSTANTS from "../../src/constants.ts";
@@ -29,7 +31,7 @@ export async function execute(options: ExecuteOptions = {}) {
2931
const { debug: debugMode } = options;
3032

3133
if (debugMode) {
32-
console.log(kleur.bgMagenta().bold(" > Debug mode enabled \n"));
34+
console.log(formatter.bgMagenta.bold(" > Debug mode enabled \n"));
3335
}
3436

3537
const [configResult] = await Promise.all([
@@ -47,8 +49,8 @@ export async function execute(options: ExecuteOptions = {}) {
4749
throw new Error("At least one reporter must be selected (either 'HTML' or 'PDF')");
4850
}
4951

50-
console.log(`>> title: ${kleur.cyan().bold(report.title)}`);
51-
console.log(`>> reporters: ${kleur.magenta().bold(report.reporters.join(","))}\n`);
52+
console.log(`>> title: ${formatter.cyan.bold(report.title)}`);
53+
console.log(`>> reporters: ${formatter.magenta.bold(report.reporters.join(","))}\n`);
5254

5355
store.run(config, async() => {
5456
try {
@@ -57,7 +59,7 @@ export async function execute(options: ExecuteOptions = {}) {
5759
debug(data);
5860
}
5961
await reporting.proceed(data);
60-
console.log(kleur.green().bold("\n>> Security report successfully generated! Enjoy 🚀.\n"));
62+
console.log(formatter.green.bold("\n>> Security report successfully generated! Enjoy 🚀.\n"));
6163
}
6264
catch (error) {
6365
console.error(error);

bin/commands/init.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Import Third-party Dependencies
22
import * as rc from "@nodesecure/rc";
3-
import kleur from "kleur";
3+
4+
// Import Internal Dependencies
5+
import * as utils from "../../src/utils/index.ts";
6+
7+
const { formatter } = utils;
48

59
export async function init() {
610
const configLocation = process.cwd();
@@ -11,7 +15,7 @@ export async function init() {
1115
});
1216

1317
if (result.ok) {
14-
console.log(kleur.green().bold(
18+
console.log(formatter.green.bold(
1519
"Successfully generated NodeSecure runtime configuration at current location\n"
1620
));
1721
}

bin/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import fs from "node:fs";
55

66
// Import Third-party Dependencies
77
import sade from "sade";
8-
import kleur from "kleur";
98

109
// Import Internal Dependencies
1110
import * as commands from "./commands/index.ts";
11+
import * as utils from "../src/utils/index.ts";
1212

13-
console.log(kleur.grey().bold(`\n > Executing nreport at: ${kleur.yellow().bold(process.cwd())}\n`));
13+
const { formatter } = utils;
14+
15+
console.log(formatter.gray.bold(`\n > Executing nreport at: ${formatter.yellow.bold(process.cwd())}\n`));
1416

1517
const { version } = JSON.parse(
1618
fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8")

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"@topcli/spinner": "^4.0.0",
6161
"esbuild": "^0.27.0",
6262
"filenamify": "^7.0.0",
63-
"kleur": "^4.1.5",
6463
"puppeteer": "^24.10.1",
6564
"sade": "^1.8.1",
6665
"zup": "0.0.2"

src/analysis/fetch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import path from "node:path";
33

44
// Import Third-party Dependencies
5-
import kleur from "kleur";
65
import * as scorecard from "@nodesecure/ossf-scorecard-sdk";
76
import { isHTTPError } from "@openally/httpie";
87

@@ -13,6 +12,8 @@ import * as localStorage from "../localStorage.ts";
1312
import * as utils from "../utils/index.ts";
1413
import * as CONSTANTS from "../constants.ts";
1514

15+
const { formatter } = utils;
16+
1617
// CONSTANTS
1718
const kNotFoundStatusCode = 404;
1819

@@ -59,7 +60,7 @@ async function fetchPackagesStats(
5960
) {
6061
const jsonFiles = await utils.runInSpinner(
6162
{
62-
title: `[Fetcher: ${kleur.yellow().bold("NPM")}]`,
63+
title: `[Fetcher: ${formatter.yellow.bold("NPM")}]`,
6364
start: "Fetching NPM packages metadata on the NPM Registry",
6465
verbose
6566
},
@@ -78,7 +79,7 @@ async function fetchRepositoriesStats(
7879
) {
7980
const jsonFiles = await utils.runInSpinner(
8081
{
81-
title: `[Fetcher: ${kleur.yellow().bold("GIT")}]`,
82+
title: `[Fetcher: ${formatter.yellow.bold("GIT")}]`,
8283
start: "Cloning GIT repositories",
8384
verbose
8485
},

src/reporting/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// Import Third-party Dependencies
2-
import kleur from "kleur";
3-
41
// Import Internal Dependencies
52
import * as utils from "../utils/index.ts";
63
import * as localStorage from "../localStorage.ts";
74

5+
const { formatter } = utils;
6+
87
// Import Reporters
98
import { HTML, type HTMLReportData } from "./html.ts";
109
import { PDF } from "./pdf.ts";
@@ -15,7 +14,7 @@ export async function proceed(
1514
): Promise<void> {
1615
const reportHTMLPath = await utils.runInSpinner(
1716
{
18-
title: `[Reporter: ${kleur.yellow().bold("HTML")}]`,
17+
title: `[Reporter: ${formatter.yellow.bold("HTML")}]`,
1918
start: "Building template and assets",
2019
verbose
2120
},
@@ -29,7 +28,7 @@ export async function proceed(
2928

3029
await utils.runInSpinner(
3130
{
32-
title: `[Reporter: ${kleur.yellow().bold("PDF")}]`,
31+
title: `[Reporter: ${formatter.yellow.bold("PDF")}]`,
3332
start: "Using puppeteer to convert HTML content to PDF",
3433
verbose
3534
},

src/utils/formatter.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Import Node.js Dependencies
2+
import {
3+
styleText,
4+
type InspectColorForeground,
5+
type InspectColorBackground,
6+
type InspectColorModifier
7+
} from "node:util";
8+
9+
type StyleName = InspectColorForeground | InspectColorBackground | InspectColorModifier;
10+
11+
type Formatter = {
12+
(text: string): string;
13+
} & {
14+
[K in StyleName]: Formatter;
15+
};
16+
17+
function createFormatter(styles: StyleName[] = []): Formatter {
18+
return new Proxy(
19+
(text: string) => styleText(styles, text),
20+
{
21+
get: (_, prop: string) => createFormatter([...styles, prop as StyleName])
22+
}
23+
) as Formatter;
24+
}
25+
26+
const formatter = createFormatter();
27+
28+
export { formatter };

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./cloneGITRepository.ts";
33
export * from "./runInSpinner.ts";
44
export * from "./formatNpmPackages.ts";
55
export * from "./charts.ts";
6+
export * from "./formatter.ts";

src/utils/runInSpinner.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Import Third-party Dependencies
22
import { Spinner } from "@topcli/spinner";
3-
import kleur from "kleur";
3+
4+
// Import Internal Dependencies
5+
import { formatter } from "./formatter.ts";
46

57
export interface RunInSpinnerOptions {
68
title: string;
@@ -17,13 +19,13 @@ export async function runInSpinner<R>(
1719
const { title, verbose = true, start = void 0 } = options;
1820

1921
const spinner = new Spinner({ verbose })
20-
.start(start, { withPrefix: `${kleur.gray().bold(title)} - ` });
22+
.start(start, { withPrefix: `${formatter.gray.bold(title)} - ` });
2123

2224
try {
2325
const response = await asyncHandler(spinner);
2426

2527
const elapsed = `${spinner.elapsedTime.toFixed(2)}ms`;
26-
spinner.succeed(kleur.white().bold(`successfully executed in ${kleur.green().bold(elapsed)}`));
28+
spinner.succeed(formatter.white.bold(`successfully executed in ${formatter.green.bold(elapsed)}`));
2729

2830
return response;
2931
}

0 commit comments

Comments
 (0)