Skip to content

Commit

Permalink
refactor: removes 3rd party dependency 'wrap-ansi' (#946)
Browse files Browse the repository at this point in the history
## Description

- removes 3rd party dependency 'wrap-ansi'

## Motivation and Context

Reduces the number of 3rd party dependencies by 7, which is good for
download size & performance and reduces potential vulnerabilities and
maintenance burden.

We _do_ wrap with ansi-codes, but if we apply the ansi codes _after_
wrapping, we don't need the specialised `wrap-ansi` package anymore and
can do with a simple hand-rolled function for wrapping, which is what
this package implements (the function is _ugly_, and might not cover all
edge cases but it works well enough for our use case).

From [npmgraph](https://npmgraph.js.org/?q=wrap-ansi):

<img width="952" alt="shows wrap-ansi's module dependency graph, with
its 6 indirect dependencies"
src="https://github.com/sverweij/dependency-cruiser/assets/4822597/1dd195cd-18c2-4022-a304-3f87416fc4a4">

<img width="406" alt="shows the number of bytes wrap-ansi adds to
dependency-cruiser's install size; 96kb unpacked, with emoji-regex and
get-east-asian-width as the largest contributors"
src="https://github.com/sverweij/dependency-cruiser/assets/4822597/fb372f56-6c8c-4039-8f5f-8ae5fb5ee71c">


## How Has This Been Tested?

- [x] green ci

## Types of changes

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [x] Refactor (non-breaking change which fixes an issue without
changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
  • Loading branch information
sverweij authored Jul 8, 2024
1 parent b574d88 commit 6e618ce
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
14 changes: 11 additions & 3 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@
"semver": "^7.6.2",
"teamcity-service-messages": "0.1.14",
"tsconfig-paths-webpack-plugin": "4.1.0",
"watskeburt": "4.1.0",
"wrap-ansi": "9.0.0"
"watskeburt": "4.1.0"
},
"devDependencies": {
"@babel/core": "7.24.7",
Expand Down
8 changes: 4 additions & 4 deletions src/report/error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ function formatReachabilityViolation(pViolation) {
}

function formatInstabilityViolation(pViolation) {
return `${formatDependencyViolation(pViolation)}${EOL}${wrapAndIndent(
pc.dim(
return `${formatDependencyViolation(pViolation)}${EOL}${pc.dim(
wrapAndIndent(
`instability: ${formatPercentage(pViolation.metrics.from.instability)}${formatPercentage(pViolation.metrics.to.instability)}`,
EXTRA_PATH_INFORMATION_INDENT,
),
EXTRA_PATH_INFORMATION_INDENT,
)}`;
}

Expand All @@ -72,7 +72,7 @@ function formatViolation(pViolation) {
)} ${pViolation.rule.name}: ${lFormattedViolators}` +
`${
pViolation.comment
? `${EOL}${wrapAndIndent(pc.dim(pViolation.comment))}${EOL}`
? `${EOL}${pc.dim(wrapAndIndent(pViolation.comment))}${EOL}`
: ""
}`
);
Expand Down
52 changes: 46 additions & 6 deletions src/utl/wrap-and-indent.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import wrapAnsi from "wrap-ansi";

const DEFAULT_INDENT = 4;

function indentString(pString, pCount) {
Expand All @@ -8,9 +6,51 @@ function indentString(pString, pCount) {
return pString.replace(lRegex, " ".repeat(pCount));
}

export default function wrapAndIndent(pString, pCount = DEFAULT_INDENT) {
const lDogmaticMaxConsoleWidth = 78;
const lMaxWidth = lDogmaticMaxConsoleWidth - pCount;
/**
* @param {string} pLine
* @param {number} pMaxWidth
*/
function splitLine(pLine, pMaxWidth) {
const lWords = pLine.split(" ");
const lWrappedLines = [];
let lCurrentLine = "";
let lCurrentWidth = 0;

for (const lWord of lWords) {
if (lCurrentWidth + lWord.length > pMaxWidth) {
lWrappedLines.push(lCurrentLine.trimEnd());
lCurrentLine = "";
lCurrentWidth = 0;
}

if (lCurrentLine) {
lCurrentLine += " ";
lCurrentWidth += 1;
}

lCurrentLine += lWord;
lCurrentWidth += lWord.length;
}

lWrappedLines.push(lCurrentLine.trimEnd());

return lWrappedLines.join("\n");
}

/**
* @param {string} pString - the string to wrap
* @param {number} pMaxWidth - the maximum width of the wrapped string
*/
function wrapString(pString, pMaxWidth) {
return pString
.split(/\r?\n/)
.map((pLine) => splitLine(pLine, pMaxWidth))
.join("\n");
}

export default function wrapAndIndent(pString, pIndent = DEFAULT_INDENT) {
const lMaxConsoleWidth = 78;
const lMaxWidth = lMaxConsoleWidth - pIndent;

return indentString(wrapAnsi(pString, lMaxWidth), pCount);
return indentString(wrapString(pString, lMaxWidth), pIndent);
}

0 comments on commit 6e618ce

Please sign in to comment.