Skip to content

Commit

Permalink
Ensure trailing newline is written to files and stdout (#879)
Browse files Browse the repository at this point in the history
Ensure that a trailing newline is written to files and `stdout`.

## Description

When writing non-binary output, ensure that a trailing newline is
written to files and `stdout`.

## Motivation and Context

It would be nice to have a trailing newline in the
`.dependency-cruiser-known-violations.json` file generated by
`depcruise-baseline` for less friction when the file is subsequently
consumed by other tools. I believe that something similar applies to
other report files generated by dependency cruiser.

## How Has This Been Tested?

I ran `depcruise-baseline` as a manual test and verified that an
additional line feed character gets added to the generated file as a
result of this change.

I could look into adding an automated test for that, but I have not done
that so far. Please let me know in case it is desired.

One of the existing test files needed to be adapted to capture this
change, so in that sense it is somewhat covered by existing unit tests.

- [ ] green ci

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] 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)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] 📖

  - My change doesn't require a documentation update, or ...
  - it _does_ and I have updated it

- [x] ⚖️
- The contribution will be subject to [The MIT
license](https://github.com/sverweij/dependency-cruiser/blob/main/LICENSE),
and I'm OK with that.
  - The contribution is my own original work.
- I am ok with the stuff in
[**CONTRIBUTING.md**](https://github.com/sverweij/dependency-cruiser/blob/main/.github/CONTRIBUTING.md).
  • Loading branch information
martinslota committed Dec 1, 2023
1 parent 19033e8 commit 17c7e1c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/cli/utl/io.mjs
Expand Up @@ -35,15 +35,18 @@ function writeToStdOut(pString, pBufferSize = PIPE_BUFFER_SIZE) {
const lChunkStart = lIndex * pBufferSize;
process.stdout.write(
pString.substring(lChunkStart, lChunkStart + pBufferSize),
"utf8"
"utf8",
);
}
}
export function write(pOutputTo, pContent) {
const lContentWithTrailingNewline = pContent.endsWith("\n")
? pContent
: `${pContent}\n`;
if ("-" === pOutputTo) {
writeToStdOut(pContent);
writeToStdOut(lContentWithTrailingNewline);
} else {
writeToFile(pOutputTo, pContent);
writeToFile(pOutputTo, lContentWithTrailingNewline);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/cli/__fixtures__/cjs.dir.filtered.html
Expand Up @@ -267,4 +267,4 @@
</table>

</body>
</html>
</html>

0 comments on commit 17c7e1c

Please sign in to comment.