From 967627febfb9ea7f667af95d60a6f99526dd802c Mon Sep 17 00:00:00 2001 From: Kouadio Fabrice Nguessan Date: Sat, 4 Jan 2025 13:19:21 +0000 Subject: [PATCH] =?UTF-8?q?chore:=20update=20copyright=20and=20replace=20k?= =?UTF-8?q?leur=20=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8Bwith=20styleText?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LICENSE | 2 +- bin/index.js | 12 ++++---- package.json | 1 - src/commands/config.js | 32 ++++++--------------- src/commands/http.js | 8 +++--- src/commands/lang.js | 11 ++++--- src/commands/scanner.js | 36 ++++++++++++++--------- src/commands/scorecard.js | 17 ++++------- src/commands/summary.js | 7 ++--- src/commands/verify.js | 60 ++++++++++++++++++++------------------- src/http-server/index.js | 7 +++-- src/utils.js | 6 ++-- 12 files changed, 97 insertions(+), 102 deletions(-) diff --git a/LICENSE b/LICENSE index 0e5b2476..9f7efbde 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022-2024 NodeSecure +Copyright (c) 2022-2025 NodeSecure Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/bin/index.js b/bin/index.js index 113c68e7..659d2741 100755 --- a/bin/index.js +++ b/bin/index.js @@ -6,9 +6,9 @@ dotenv.config(); import path from "node:path"; import { createRequire } from "node:module"; import { fileURLToPath } from "node:url"; +import { styleText } from "node:util"; // Import Third-party Dependencies -import kleur from "kleur"; import sade from "sade"; import semver from "semver"; import * as i18n from "@nodesecure/i18n"; @@ -30,11 +30,13 @@ await i18n.extendFromSystemPath( path.join(__dirname, "..", "i18n") ); -console.log(kleur.grey().bold(`\n > ${i18n.getTokenSync("cli.executing_at")}: ${kleur.yellow().bold(process.cwd())}\n`)); +console.log( + styleText(["gray", "bold"], `\n > ${i18n.getTokenSync("cli.executing_at")}: ${styleText(["yellow", "bold"], process.cwd())}\n`) +); const minVersion = semver.minVersion(manifest.engines.node); if (semver.lt(process.versions.node, minVersion)) { - console.log(kleur.red().bold(` [!] ${i18n.getTokenSync("cli.min_nodejs_version", minVersion)}\n`)); + console.log(styleText(["red", "bold"], ` [!] ${i18n.getTokenSync("cli.min_nodejs_version", minVersion)}\n`)); process.exit(0); } @@ -138,10 +140,10 @@ function defaultScannerCommand(name, options = {}) { function checkNodeSecureToken() { if (!process.env.NODE_SECURE_TOKEN) { - const varEnvName = kleur.yellow().bold("NODE_SECURE_TOKEN"); + const varEnvName = styleText(["yellow", "bold"], "NODE_SECURE_TOKEN"); console.log( - kleur.red().bold(`${i18n.getTokenSync("cli.missingEnv", varEnvName)}\n`) + styleText(["red", "bold"], `${i18n.getTokenSync("cli.missingEnv", varEnvName)}\n`) ); } } diff --git a/package.json b/package.json index d3a12dbf..7ffcf33b 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,6 @@ "filenamify": "^6.0.0", "highlightjs-line-numbers.js": "^2.8.0", "ini": "^5.0.0", - "kleur": "^4.1.5", "ms": "^2.1.3", "open": "^10.1.0", "pino": "^9.3.2", diff --git a/src/commands/config.js b/src/commands/config.js index 988daf80..b964c0e7 100644 --- a/src/commands/config.js +++ b/src/commands/config.js @@ -3,10 +3,10 @@ import os from "node:os"; import path from "node:path"; import fs from "node:fs/promises"; import { existsSync } from "node:fs"; +import { styleText } from "node:util"; // Import Third-party Dependencies import * as RC from "@nodesecure/rc"; -import kleur from "kleur"; import { spawn } from "node:child_process"; const K_HOME_PATH = path.join(os.homedir(), "nodesecure"); @@ -19,20 +19,12 @@ function spawnCodeAtNodeSecureRc(path, isCwd = false) { } console.log( - kleur - .white() - .bold( - `\n ${kleur.yellow().bold(`There is no .nodesecurerc here: ${path}`)}` - ) + styleText(["white", "bold"], `\n ${styleText(["yellow", "bold"], `There is no .nodesecurerc here: ${path}`)}`) ); if (isCwd) { console.log( - kleur - .white() - .bold( - `\n ${kleur.yellow().bold("You must try to run nsecure config create")}` - ) + styleText(["white", "bold"], `\n ${styleText(["yellow", "bold"], "You must try to run nsecure config create")}`) ); } @@ -43,12 +35,9 @@ export function editConfigFile() { console.log(""); console.log( - kleur - .white() - .bold( - `\n ${kleur.yellow().bold("Try to opened up your Nodesecure configuration in vscode")}` - ) - ); + styleText( + ["white", "bold"], `\n ${styleText(["yellow", "bold"], "Try to opened up your Nodesecure configuration in vscode")}` + )); const isNodeSecureRcAtHomeDir = spawnCodeAtNodeSecureRc(path.join(K_HOME_PATH, RC.CONSTANTS.CONFIGURATION_NAME)); @@ -63,12 +52,9 @@ export async function createConfigFile(configuration = "minimal", opts) { const pathConfigFile = cwd ? process.cwd() : K_HOME_PATH; console.log(""); console.log( - kleur - .white() - .bold( - `\n ${kleur.yellow().bold(`We are going to create the Nodesecure configuration file at: ${pathConfigFile}`)}` - ) - ); + styleText(["white", "bold"], + `\n ${styleText(["yellow", "bold"], `We are going to create the Nodesecure configuration file at: ${pathConfigFile}`)}` + )); await fs.mkdir(pathConfigFile, { recursive: true }); await RC.read(pathConfigFile, { createIfDoesNotExist: true, createMode: configuration }); diff --git a/src/commands/http.js b/src/commands/http.js index ab24c37f..8aba7eee 100644 --- a/src/commands/http.js +++ b/src/commands/http.js @@ -1,10 +1,10 @@ // Import Node.js Dependencies import fs from "node:fs"; import path from "node:path"; +import { styleText } from "node:util"; // Import Third-party Dependencies import * as SemVer from "semver"; -import kleur from "kleur"; import * as i18n from "@nodesecure/i18n"; // Import Internal Dependencies @@ -47,13 +47,13 @@ function assertScannerVersion( if (!SemVer.satisfies(scannerVersion, kRequiredScannerRange)) { const error = i18n.getTokenSync( "cli.startHttp.invalidScannerVersion", - kleur.yellow(scannerVersion), - kleur.yellow(kRequiredScannerRange) + styleText("yellow", scannerVersion), + styleText("yellow", kRequiredScannerRange) ); const regenerate = i18n.getTokenSync("cli.startHttp.regenerate"); console.log(" > " + path.basename(dataFilePath)); - console.log(" > " + kleur.red().bold(error)); + console.log(" > " + styleText(["red", "bold"], error)); console.log(` > ${regenerate}`); console.log(); diff --git a/src/commands/lang.js b/src/commands/lang.js index 25f2af76..a2ad0461 100644 --- a/src/commands/lang.js +++ b/src/commands/lang.js @@ -1,12 +1,14 @@ +// Import Node.js Dependencies +import { styleText } from "node:util"; + // Import Third-party Dependencies import * as i18n from "@nodesecure/i18n"; import { select } from "@topcli/prompts"; -import kleur from "kleur"; export async function set() { const langs = await i18n.getLanguages(); const selectedLang = await select( - kleur.green().bold(` ${i18n.getTokenSync("cli.commands.lang.question_text")}`), + styleText(["green", "bold"], ` ${i18n.getTokenSync("cli.commands.lang.question_text")}`), { choices: langs } @@ -16,7 +18,8 @@ export async function set() { await i18n.getLocalLang(); console.log( - kleur.white().bold(`\n ${i18n.getTokenSync("cli.commands.lang.new_selection", kleur.yellow().bold(selectedLang))}`) - ); + styleText( + ["white", "bold"], `\n ${i18n.getTokenSync("cli.commands.lang.new_selection", styleText(["yellow", "bold"], selectedLang))}` + )); console.log(); } diff --git a/src/commands/scanner.js b/src/commands/scanner.js index 01536dba..7beb1a34 100644 --- a/src/commands/scanner.js +++ b/src/commands/scanner.js @@ -2,9 +2,9 @@ import fs from "node:fs"; import path from "node:path"; import events from "node:events"; +import { styleText } from "node:util"; // Import Third-party Dependencies -import kleur from "kleur"; import filenamify from "filenamify"; import { Spinner } from "@topcli/spinner"; import ms from "ms"; @@ -108,11 +108,13 @@ function initLogger(spec, verbose = true) { if (eventName === "fetchManifest") { spinner[eventName] - .start(kleur.white().bold(i18n.getTokenSync(spinner.i18n.start[eventName], kleur.green().bold(spec)))); + .start( + styleText(["white", "bold"], i18n.getTokenSync(spinner.i18n.start[eventName], styleText(["green", "bold"], spec))) + ); } else { spinner[eventName] - .start(kleur.white().bold(i18n.getTokenSync(spinner.i18n.start[eventName]))); + .start(styleText(["white", "bold"], i18n.getTokenSync(spinner.i18n.start[eventName]))); } }); @@ -121,8 +123,10 @@ function initLogger(spec, verbose = true) { return; } - const stats = kleur.gray().bold(`[${kleur.yellow().bold(logger.count(eventName))}/${logger.count("walkTree")}]`); - spinner[eventName].text = kleur.white().bold(`${i18n.getTokenSync(spinner.i18n.tick[eventName])} ${stats}`); + const stats = styleText( + ["white", "bold"], `[${styleText(["yellow", "bold"], logger.count(eventName))}/${logger.count("walkTree")}]` + ); + spinner[eventName].text = styleText(["white", "bold"], `${i18n.getTokenSync(spinner.i18n.tick[eventName])} ${stats}`); }); logger.on("end", (eventName) => { @@ -132,20 +136,23 @@ function initLogger(spec, verbose = true) { const spin = spinner[eventName]; const tokenName = spinner.i18n.end[eventName]; - const execTime = kleur.cyan().bold(ms(Number(spin.elapsedTime.toFixed(2)))); + const execTime = styleText(["cyan", "bold"], ms(Number(spin.elapsedTime.toFixed(2)))); if (eventName === "walkTree") { - spin.succeed(kleur.white().bold( - i18n.getTokenSync(tokenName, kleur.yellow().bold(i18n.getTokenSync("depWalker.dep_tree")), execTime))); + spin.succeed(styleText(["white", "bold"], + i18n.getTokenSync(tokenName, styleText(["yellow", "bold"], i18n.getTokenSync("depWalker.dep_tree")), execTime))); } else if (eventName === "registry") { - spin.succeed(kleur.white().bold(i18n.getTokenSync(tokenName))); + spin.succeed(styleText(["white", "bold"], i18n.getTokenSync(tokenName))); } else if (eventName === "tarball") { - spin.succeed(kleur.white().bold(i18n.getTokenSync(tokenName, kleur.green().bold(logger.count("walkTree")), execTime))); + spin.succeed( + styleText(["white", "bold"], + i18n.getTokenSync(tokenName, styleText(["green", "bold"], logger.count("walkTree")), execTime))); } else if (eventName === "fetchManifest") { - spin.succeed(kleur.white().bold(i18n.getTokenSync(tokenName, kleur.green().bold(spec), execTime))); + spin.succeed( + styleText(["white", "bold"], i18n.getTokenSync(tokenName, styleText(["green", "bold"], spec), execTime))); console.log(""); } }); @@ -161,9 +168,9 @@ function logAndWrite(payload, output = "nsecure-result") { } if (payload.warnings.length > 0) { - console.log(`\n ${kleur.yellow().underline().bold("Global Warning:")}\n`); + console.log(`\n ${styleText(["yellow", "underline", "bold"], "Global Warning:")}\n`); for (const warning of payload.warnings) { - console.log(kleur.red().bold(warning)); + console.log(styleText(["red", "bold"], warning)); } } @@ -176,7 +183,8 @@ function logAndWrite(payload, output = "nsecure-result") { fs.writeFileSync(filePath, ret); console.log(""); - console.log(kleur.white().bold(i18n.getTokenSync("cli.successfully_written_json", kleur.green().bold(filePath)))); + console.log( + styleText(["white", "bold"], i18n.getTokenSync("cli.successfully_written_json", styleText(["green", "bold"], filePath)))); console.log(""); return filePath; diff --git a/src/commands/scorecard.js b/src/commands/scorecard.js index 47b4d348..4c4dafa0 100644 --- a/src/commands/scorecard.js +++ b/src/commands/scorecard.js @@ -1,17 +1,15 @@ // Import Node.js Dependencies import fs from "node:fs"; +import { styleText } from "node:util"; // Import Third-party Dependencies import cliui from "@topcli/cliui"; -import kleur from "kleur"; import * as scorecard from "@nodesecure/ossf-scorecard-sdk"; import ini from "ini"; import { Ok, Err } from "@openally/result"; -// VARS -const { yellow, grey, cyan, white } = kleur; function separatorLine() { - return grey("-".repeat(80)); + return styleText("grey", "-".repeat(80)); } export function getCurrentRepository(vcs = "github") { @@ -44,7 +42,7 @@ export async function main(repo, opts) { platform = vcs.slice(-4) === ".com" ? vcs : `${vcs}.com`; } catch (error) { - console.log(white().bold(result.err)); + console.log(styleText(["white", "bold"], result.err)); process.exit(); } @@ -58,17 +56,14 @@ export async function main(repo, opts) { } catch (error) { console.log( - kleur - .white() - .bold(white().bold(`${repository} is not part of the OSSF Scorecard BigQuery public dataset.`)) - ); + styleText(["white", "bold"], `${repository} is not part of the OSSF Scorecard BigQuery public dataset.`)); process.exit(); } const ui = cliui({ width: 80 }); - ui.div({ text: cyan("OSSF Scorecard"), align: "center", padding: [1, 1, 1, 1] }); + ui.div({ text: styleText("cyan", "OSSF Scorecard"), align: "center", padding: [1, 1, 1, 1] }); ui.div( { text: "Repository", width: 20 }, @@ -87,7 +82,7 @@ export async function main(repo, opts) { for (const check of data.checks) { const { score, name, reason } = check; ui.div( - { text: yellow(name), width: 77 }, + { text: styleText("yellow", name), width: 77 }, { text: !score || score < 0 ? 0 : score, width: 3, align: "right" } ); diff --git a/src/commands/summary.js b/src/commands/summary.js index dc800cd5..dbcad742 100644 --- a/src/commands/summary.js +++ b/src/commands/summary.js @@ -1,16 +1,13 @@ // Import Node.js Dependencies import fs from "node:fs/promises"; import path from "node:path"; +import { styleText } from "node:util"; // Import Third-party Dependencies import cliui from "@topcli/cliui"; -import kleur from "kleur"; import * as i18n from "@nodesecure/i18n"; import { formatBytes } from "@nodesecure/utils"; -// VARS -const { yellow, grey, white, green, cyan, red } = kleur; - function separatorLine() { return grey("-".repeat(80)); } @@ -22,7 +19,7 @@ export async function main(json = "nsecure-result.json") { const { rootDependencyName, dependencies } = JSON.parse(rawAnalysis); const ui = cliui({ width: 80 }); - const title = `${white().bold(`${i18n.getTokenSync("ui.stats.title")}:`)} ${cyan().bold(rootDependencyName)}`; + const title = `${styleText(["white", "bold"], `${i18n.getTokenSync("ui.stats.title")}:`)} ${cyan().bold(rootDependencyName)}`; ui.div( { text: title, width: 50 } ); diff --git a/src/commands/verify.js b/src/commands/verify.js index d75a89ef..fbfc932f 100644 --- a/src/commands/verify.js +++ b/src/commands/verify.js @@ -1,14 +1,13 @@ +// Import Node.js Dependencies +import { styleText } from "node:util"; + // Import Third-party Dependencies import cliui from "@topcli/cliui"; -import kleur from "kleur"; import { verify } from "@nodesecure/scanner"; import { formatBytes, locationToString } from "@nodesecure/utils"; -// VARS -const { yellow, grey, white, green, cyan, red, magenta } = kleur; - function separatorLine() { - return grey("-".repeat(80)); + return styleText("grey", "-".repeat(80)); } export async function main( @@ -25,21 +24,21 @@ export async function main( const ui = cliui({ width: 80 }); ui.div( - { text: cyan().bold("directory size:"), width: 20 }, - { text: yellow().bold(formatBytes(directorySize)), width: 10 } + { text: styleText(["cyan", "bold"], "directory size:"), width: 20 }, + { text: styleText(["yellow", "bold"], formatBytes(directorySize)), width: 10 } ); ui.div( - { text: cyan().bold("unique licenses:"), width: 20 }, - { text: white().bold(uniqueLicenseIds.join(", ")), width: 10 } + { text: styleText(["cyan", "bold"], "unique licenses:"), width: 20 }, + { text: styleText(["white", "bold"], uniqueLicenseIds.join(", ")), width: 10 } ); console.log(`${ui.toString()}\n`); ui.resetOutput(); { ui.div( - { text: white().bold("ext"), width: 10, align: "center" }, - { text: white().bold("files"), width: 40 }, - { text: white().bold("minified files"), width: 30 } + { text: styleText(["white", "bold"], "ext"), width: 10, align: "center" }, + { text: styleText(["white", "bold"], "files"), width: 40 }, + { text: styleText(["white", "bold"], "minified files"), width: 30 } ); const maxLen = files.list.length > files.extensions.length ? files.list.length : files.extensions.length; @@ -50,9 +49,9 @@ export async function main( for (const [ext, file, min] of divArray) { ui.div( - { text: cyan().bold(ext), width: 10, align: "center" }, + { text: styleText(["cyan", "bold"], ext), width: 10, align: "center" }, { text: file, width: 40 }, - { text: red().bold(min), width: 30 } + { text: styleText(["red", "bold"], min), width: 30 } ); } } @@ -60,17 +59,17 @@ export async function main( ui.resetOutput(); ui.div({ text: separatorLine() }); - ui.div({ text: cyan().bold("Required dependency and files"), align: "center" }); + ui.div({ text: styleText(["cyan", "bold"], "Required dependency and files"), align: "center" }); ui.div({ text: separatorLine() }); ui.div(); for (const [fileName, deps] of Object.entries(ast.dependencies)) { - ui.div({ text: magenta().bold(fileName), align: "center" }); + ui.div({ text: styleText(["magenta", "bold"], fileName), align: "center" }); ui.div({ text: separatorLine() }); ui.div( - { text: white().bold("required stmt"), width: 30 }, - { text: white().bold("try/catch"), width: 20, align: "center" }, - { text: white().bold("source location"), width: 30, align: "right" } + { text: styleText(["white", "bold"], "required stmt"), width: 30 }, + { text: styleText(["white", "bold"], "try/catch"), width: 20, align: "center" }, + { text: styleText(["white", "bold"], "source location"), width: 30, align: "right" } ); for (const [depName, infos] of Object.entries(deps)) { const { start, end } = infos.location; @@ -78,8 +77,11 @@ export async function main( ui.div( { text: depName, width: 30 }, - { text: (infos.inTry ? green : red)().bold(infos.inTry), width: 20, align: "center" }, - { text: grey().bold(position), width: 30, align: "right" } + { + text: infos.inTry ? + styleText(["green", "bold"], infos.inTry) : styleText(["red", "bold"], infos.inTry), width: 20, align: "center" + }, + { text: styleText(["grey", "bold"], position), width: 30, align: "right" } ); } ui.div(); @@ -88,14 +90,14 @@ export async function main( } ui.div({ text: separatorLine() }); - ui.div({ text: cyan().bold("AST Warnings"), align: "center" }); + ui.div({ text: styleText(["cyan", "bold"], "AST Warnings"), align: "center" }); ui.div({ text: separatorLine() }); ui.div(); ui.div( - { text: white().bold("file"), width: 30 }, - { text: white().bold("kind"), width: 20, align: "center" }, - { text: white().bold("source location"), width: 30, align: "right" } + { text: styleText(["white", "bold"], "file"), width: 30 }, + { text: styleText(["white", "bold"], "kind"), width: 20, align: "center" }, + { text: styleText(["white", "bold"], "source location"), width: 30, align: "right" } ); for (const warning of ast.warnings) { @@ -104,13 +106,13 @@ export async function main( locationToString(warning.location); ui.div( - { text: warning.file || grey().bold("NONE"), width: 30 }, - { text: magenta().bold(warning.kind), width: 20, align: "center" }, - { text: grey().bold(position), width: 30, align: "right" } + { text: warning.file || styleText(["grey", "bold"], "NONE"), width: 30 }, + { text: styleText(["magenta", "bold"], warning.kind), width: 20, align: "center" }, + { text: styleText(["grey", "bold"], position), width: 30, align: "right" } ); if (warning.value) { ui.div(); - ui.div({ text: yellow().bold(warning.value), align: "center" }); + ui.div({ text: styleText(["yellow", "bold"], warning.value), align: "center" }); } ui.div({ text: separatorLine() }); } diff --git a/src/http-server/index.js b/src/http-server/index.js index bab1b82d..11e1efec 100644 --- a/src/http-server/index.js +++ b/src/http-server/index.js @@ -1,8 +1,8 @@ // Import Node.js Dependencies import fs from "node:fs"; +import { styleText } from "node:util"; // Import Third-party Dependencies -import kleur from "kleur"; import polka from "polka"; import open from "open"; import * as i18n from "@nodesecure/i18n"; @@ -55,7 +55,8 @@ export function buildServer(dataFilePath, options = {}) { httpServer.listen(httpConfigPort, async() => { const port = httpServer.server.address().port; const link = `http://localhost:${port}`; - console.log(kleur.magenta().bold(await i18n.getToken("cli.http_server_started")), kleur.cyan().bold(link)); + console.log( + styleText(["magenta", "bold"], await i18n.getToken("cli.http_server_started")), styleText(["cyan", "bold"], link)); if (openLink) { open(link); @@ -85,6 +86,6 @@ export function buildServer(dataFilePath, options = {}) { } process.on("SIGINT", () => { - console.log(kleur.red().bold("SIGINT signal received.")); + console.log(styleText(["red", "bold"], "SIGINT signal received.")); process.exit(0); }); diff --git a/src/utils.js b/src/utils.js index f363bc09..490d9f90 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,8 @@ +// Import Node.js Dependencies +import { styleText } from "node:util"; + // Import Third-party Dependencies import * as i18n from "@nodesecure/i18n"; -import kleur from "kleur"; export function taggedI18nString(strings, ...keys) { return function cur(...i18nParameters) { @@ -15,6 +17,6 @@ export function taggedI18nString(strings, ...keys) { ); }); - return kleur.white().bold(finalString.join("")); + return styleText(["white", "bold"], finalString.join("")); }; }