Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replaces chalk with picocolors #945

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:
check-windows:
env:
PLATFORM: windows-latest
NO_COLOR: 1
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@
"scm:push:gitlab-mirror:commits": "git push gitlab-mirror",
"scm:push:gitlab-mirror:tags": "git push --tags gitlab-mirror",
"scm:stage": "git add .",
"test": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings mocha",
"test:i": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings mocha --grep \"^\\[[I]\\]\"",
"test:u": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings mocha --grep \"^\\[[U]\\]\"",
"test:e": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings mocha --grep \"^\\[[E]\\]\"",
"test:cover": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings c8 mocha",
"test": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings NO_COLOR=1 mocha",
"test:i": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings NO_COLOR=1 mocha --grep \"^\\[[I]\\]\"",
"test:u": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings NO_COLOR=1 mocha --grep \"^\\[[U]\\]\"",
"test:e": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings NO_COLOR=1 mocha --grep \"^\\[[E]\\]\"",
"test:cover": "LANG=en_US.UTF-8 NODE_OPTIONS=--no-warnings NO_COLOR=1 c8 mocha",
"test:glob": "set -f && test \"`bin/dependency-cruise.mjs --no-config test/extract/__mocks__/gather-globbing/packages/**/src/**/*.js | grep \"no dependency violations found\"`\" = \"✔ no dependency violations found (6 modules, 0 dependencies cruised)\"",
"test:load": "hyperfine --warmup 3 --runs 30 \"bin/dependency-cruise.mjs src bin test configs types tools --ignore-known --no-cache --no-progress\"",
"test:load:short": "hyperfine --warmup 1 --runs 5 \"bin/dependency-cruise.mjs src bin test configs types tools --ignore-known --no-cache --no-progress\"",
Expand All @@ -210,7 +210,6 @@
"acorn-loose": "8.4.0",
"acorn-walk": "8.3.3",
"ajv": "8.16.0",
"chalk": "5.3.0",
"commander": "12.1.0",
"enhanced-resolve": "5.17.0",
"ignore": "5.3.1",
Expand All @@ -219,6 +218,7 @@
"json5": "2.2.3",
"lodash": "4.17.21",
"memoize": "10.0.0",
"picocolors": "1.0.1",
"picomatch": "4.0.2",
"prompts": "2.4.2",
"rechoir": "^0.8.0",
Expand Down
6 changes: 3 additions & 3 deletions src/cli/format-meta-info.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import chalk from "chalk";
import pc from "picocolors";

import { getAvailableTranspilers, allExtensions } from "#main/index.mjs";

function bool2Symbol(pBool) {
return pBool ? chalk.green("✔") : chalk.red("x");
return pBool ? pc.green("✔") : pc.red("x");
}

function formatTranspilers() {
Expand All @@ -28,7 +28,7 @@ export default function formatMetaInfo() {
return `
Supported:

If you need a supported, but not enabled transpiler ('${chalk.red(
If you need a supported, but not enabled transpiler ('${pc.red(
"x",
)}' below), just install
it in the same folder dependency-cruiser is installed. E.g. 'npm i livescript'
Expand Down
12 changes: 7 additions & 5 deletions src/cli/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from "node:path";
import picomatch from "picomatch";
import set from "lodash/set.js";
import isInstalledGlobally from "is-installed-globally";
import chalk from "chalk";
import pc from "picocolors";

import assertFileExistence from "./utl/assert-file-existence.mjs";
import normalizeCliOptions from "./normalize-cli-options.mjs";
Expand Down Expand Up @@ -159,11 +159,13 @@ export default async function executeCli(
/* c8 ignore start */
if (isInstalledGlobally) {
lStreams.stderr.write(
`\n ${chalk.yellow(
`\n ${pc.yellow(
"WARNING",
)}: You're running a globally installed dependency-cruiser.\n\n` +
` We recommend to ${chalk.bold.italic.underline(
"install and run it as a local devDependency",
` We recommend to ${pc.bold(
pc.italic(
pc.underline("install and run it as a local devDependency"),
),
)} in\n` +
` your project instead. There it has your project's environment and\n` +
` transpilers at its disposal. That will ensure it can find e.g.\n` +
Expand All @@ -183,7 +185,7 @@ export default async function executeCli(
lExitCode = await runCruise(pFileDirectoryArray, lCruiseOptions);
}
} catch (pError) {
lStreams.stderr.write(`\n ${chalk.red("ERROR")}: ${pError.message}\n`);
lStreams.stderr.write(`\n ${pc.red("ERROR")}: ${pError.message}\n`);
bus.emit("end");
lExitCode = 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/init-config/write-config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFileSync } from "node:fs";
import chalk from "chalk";
import pc from "picocolors";
import {
fileExists,
getDefaultConfigFileName,
Expand Down Expand Up @@ -29,7 +29,7 @@ export default function writeConfig(
try {
writeFileSync(pFileName, pConfig);
pOutStream.write(
`\n ${chalk.green("✔")} Successfully created '${pFileName}'\n\n`,
`\n ${pc.green("✔")} Successfully created '${pFileName}'\n\n`,
);
/* c8 ignore start */
} catch (pError) {
Expand Down
8 changes: 3 additions & 5 deletions src/cli/init-config/write-run-scripts-to-manifest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable security/detect-object-injection */
import { writeFileSync } from "node:fs";
import { EOL } from "node:os";
import chalk from "chalk";
import pc from "picocolors";
import { PACKAGE_MANIFEST as _PACKAGE_MANIFEST } from "../defaults.mjs";
import { readManifest } from "./environment-helpers.mjs";
import { folderNameArrayToRE } from "./utl.mjs";
Expand Down Expand Up @@ -126,16 +126,14 @@ export function addRunScriptsToManifest(pManifest, pAdditionalRunScripts) {
}

function getSuccessMessage(pDestinationManifestFileName) {
const lExplanationIndent = 6;
sverweij marked this conversation as resolved.
Show resolved Hide resolved

return EXPERIMENTAL_SCRIPT_DOC.reduce(
(pAll, pScript) => {
return `${pAll}${
`\n ${chalk.green("►")} ${pScript.headline}` +
`\n ${pc.green("►")} ${pScript.headline}` +
`\n${pScript.description}\n\n`
}`;
},
` ${chalk.green("✔")} Run scripts added to '${pDestinationManifestFileName}':\n`,
` ${pc.green("✔")} Run scripts added to '${pDestinationManifestFileName}':\n`,
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/cli/listeners/cli-feedback.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk from "chalk";
import pc from "picocolors";
import { SUMMARY } from "#utl/bus.mjs";

const FULL_ON = 100;
Expand All @@ -17,7 +17,7 @@ function getPercentageBar(pPercentage, pParameters) {
const lBlocks = Math.floor(lParameters.barSize * lPercentage);
const lBlanks = lParameters.barSize - lBlocks;

return `${chalk.green(lParameters.block.repeat(lBlocks))}${chalk.green(
return `${pc.green(lParameters.block.repeat(lBlocks))}${pc.green(
lParameters.blank.repeat(lBlanks),
)} ${Math.round(FULL_ON * lPercentage)}%`;
}
Expand Down
8 changes: 4 additions & 4 deletions src/cli/listeners/performance-log/format-helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk from "chalk";
import pc from "picocolors";
import { INFO } from "#utl/bus.mjs";

const MS_PER_SECOND = 1000;
Expand Down Expand Up @@ -43,7 +43,7 @@ export function formatDividerLine() {
}

export function formatHeader() {
return chalk
return pc
.bold(
`${
pad("∆ rss") +
Expand All @@ -59,7 +59,7 @@ export function formatHeader() {
}

function formatMessage(pMessage, pLevel) {
return pLevel >= INFO ? chalk.dim(pMessage) : pMessage;
return pLevel >= INFO ? pc.dim(pMessage) : pMessage;
}

export function formatTime(
Expand All @@ -81,7 +81,7 @@ export function formatMemory(pBytes, pLevel) {
);

return formatMessage(
(pBytes < 0 ? chalk.blue(lReturnValue) : lReturnValue).concat(" "),
(pBytes < 0 ? pc.blue(lReturnValue) : lReturnValue).concat(" "),
pLevel,
);
}
Expand Down
32 changes: 16 additions & 16 deletions src/report/error.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { EOL } from "node:os";
import chalk from "chalk";
import pc from "picocolors";
import {
formatPercentage,
formatViolation as _formatViolation,
} from "./utl/index.mjs";
import { findRuleByName } from "#graph-utl/rule-set.mjs";
import wrapAndIndent from "#utl/wrap-and-indent.mjs";

const SEVERITY2CHALK = new Map([
["error", chalk.red],
["warn", chalk.yellow],
["info", chalk.cyan],
["ignore", chalk.gray],
const SEVERITY2COLOR_FN = new Map([
["error", pc.red],
["warn", pc.yellow],
["info", pc.cyan],
["ignore", pc.gray],
]);

const EXTRA_PATH_INFORMATION_INDENT = 6;
Expand All @@ -26,26 +26,26 @@ function formatMiniDependency(pMiniDependency) {
}

function formatModuleViolation(pViolation) {
return chalk.bold(pViolation.from);
return pc.bold(pViolation.from);
}

function formatDependencyViolation(pViolation) {
return `${chalk.bold(pViolation.from)} → ${chalk.bold(pViolation.to)}`;
return `${pc.bold(pViolation.from)} → ${pc.bold(pViolation.to)}`;
}

function formatCycleViolation(pViolation) {
return `${chalk.bold(pViolation.from)} → ${formatMiniDependency(pViolation.cycle)}`;
return `${pc.bold(pViolation.from)} → ${formatMiniDependency(pViolation.cycle)}`;
}

function formatReachabilityViolation(pViolation) {
return `${chalk.bold(pViolation.from)} → ${chalk.bold(
return `${pc.bold(pViolation.from)} → ${pc.bold(
pViolation.to,
)}${formatMiniDependency(pViolation.via)}`;
}

function formatInstabilityViolation(pViolation) {
return `${formatDependencyViolation(pViolation)}${EOL}${wrapAndIndent(
chalk.dim(
pc.dim(
`instability: ${formatPercentage(pViolation.metrics.from.instability)} → ${formatPercentage(pViolation.metrics.to.instability)}`,
),
EXTRA_PATH_INFORMATION_INDENT,
Expand All @@ -67,12 +67,12 @@ function formatViolation(pViolation) {
);

return (
`${SEVERITY2CHALK.get(pViolation.rule.severity)(
`${SEVERITY2COLOR_FN.get(pViolation.rule.severity)(
pViolation.rule.severity,
)} ${pViolation.rule.name}: ${lFormattedViolators}` +
`${
pViolation.comment
? `${EOL}${wrapAndIndent(chalk.dim(pViolation.comment))}${EOL}`
? `${EOL}${wrapAndIndent(pc.dim(pViolation.comment))}${EOL}`
: ""
}`
);
Expand All @@ -93,7 +93,7 @@ function formatSummary(pSummary) {
pSummary.totalCruised
} modules, ${pSummary.totalDependenciesCruised} dependencies cruised.${EOL}`;

return pSummary.error > 0 ? chalk.red(lMessage) : lMessage;
return pSummary.error > 0 ? pc.red(lMessage) : lMessage;
}

function addExplanation(pRuleSet, pLong) {
Expand All @@ -107,7 +107,7 @@ function addExplanation(pRuleSet, pLong) {

function formatIgnoreWarning(pNumberOfIgnoredViolations) {
if (pNumberOfIgnoredViolations > 0) {
return chalk.yellow(
return pc.yellow(
`‼ ${pNumberOfIgnoredViolations} known violations ignored. Run with --no-ignore-known to see them.${EOL}`,
);
}
Expand All @@ -120,7 +120,7 @@ function report(pResults, pLong) {
);

if (lNonIgnorableViolations.length === 0) {
return `${EOL}${chalk.green("✔")} no dependency violations found (${
return `${EOL}${pc.green("✔")} no dependency violations found (${
pResults.summary.totalCruised
} modules, ${
pResults.summary.totalDependenciesCruised
Expand Down
4 changes: 2 additions & 2 deletions src/report/metrics.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EOL } from "node:os";
import chalk from "chalk";
import pc from "picocolors";
import { formatNumber, formatPercentage } from "./utl/index.mjs";

/**
Expand Down Expand Up @@ -224,7 +224,7 @@ function formatToTextData(pData, pMetaData) {
* @returns {string}
*/
function formatToTextTable(pData, pMetaData) {
return [chalk.bold(formatToTextHeader(pMetaData))]
return [pc.bold(formatToTextHeader(pMetaData))]
.concat(formatToTextDemarcationLine(pMetaData))
.concat(formatToTextData(pData, pMetaData))
.join(EOL)
Expand Down
4 changes: 2 additions & 2 deletions src/report/text.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk from "chalk";
import pc from "picocolors";

const DEFAULT_OPTIONS = {
highlightFocused: false,
Expand Down Expand Up @@ -36,7 +36,7 @@ function toFlatDependencies(pModules, pModulesInFocus, pHighlightFocused) {
}

function stringifyModule(pModule) {
return pModule.highlight ? chalk.underline(pModule.name) : pModule.name;
return pModule.highlight ? pc.underline(pModule.name) : pModule.name;
}

function stringify(pFlatDependency) {
Expand Down
9 changes: 2 additions & 7 deletions test/cli/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { doesNotThrow, equal, throws, match } from "node:assert/strict";
// path.posix instead of path because otherwise on win32 the resulting
// outputTo would contain \\ instead of / which for this unit test doesn't matter
import { join, posix as path } from "node:path";
import chalk from "chalk";
import { assertFileEqual, assertJSONFileEqual } from "./asserthelpers.utl.mjs";
import deleteDammit from "./delete-dammit.utl.cjs";
import {
Expand Down Expand Up @@ -238,16 +237,12 @@ describe("[E] cli/index", () => {
});

describe("[E] specials", () => {
let lChalkLevel = chalk.level;
const lOriginalStdoutWrite = process.stdout.write;

before("disable chalk coloring", () => {
chalk.level = 0;
});
after("enable chalk coloring again", () => {
chalk.level = lChalkLevel;
after("enable coloring again", () => {
process.stdout.write = lOriginalStdoutWrite;
});

it("dependency-cruises multiple files and folders in one go", async () => {
const lOutputFileName = "multiple-in-one-go.json";
const lOutputTo = path.join(OUT_DIR, lOutputFileName);
Expand Down
Loading