Skip to content

Commit

Permalink
Only output and log unix newlines
Browse files Browse the repository at this point in the history
The formatter will properly convert for logfiles
  • Loading branch information
lukekarrys committed Apr 12, 2024
1 parent 3a0de03 commit 1e46f0c
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 15 deletions.
9 changes: 4 additions & 5 deletions lib/commands/ls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { resolve, relative, sep } = require('path')
const relativePrefix = `.${sep}`
const { EOL } = require('os')

const archy = require('archy')
const { breadth } = require('treeverse')
Expand Down Expand Up @@ -200,7 +199,7 @@ class LS extends ArboristWorkspaceCmd {

if (shouldThrow) {
throw Object.assign(
new Error([...problems].join(EOL)),
new Error([...problems].join('\n')),
{ code: 'ELSPROBLEMS' }
)
}
Expand Down Expand Up @@ -289,7 +288,7 @@ const getHumanOutputItem = (node, { args, chalk, global, long }) => {
if (hasNoPackageJson || global) {
printable = path
} else {
printable += `${long ? EOL : ' '}${path}`
printable += `${long ? '\n' : ' '}${path}`
}
}

Expand Down Expand Up @@ -333,7 +332,7 @@ const getHumanOutputItem = (node, { args, chalk, global, long }) => {
) +
(isGitNode(node) ? ` (${node.resolved})` : '') +
(node.isLink ? ` -> ${relativePrefix}${targetLocation}` : '') +
(long ? `${EOL}${node.package.description || ''}` : '')
(long ? `\n${node.package.description || ''}` : '')

return augmentItemWithIncludeMetadata(node, { label, nodes: [] })
}
Expand Down Expand Up @@ -566,7 +565,7 @@ const parseableOutput = ({ global, long, seenNodes }) => {
out += node[_invalid] ? ':INVALID' : ''
out += node.overridden ? ':OVERRIDDEN' : ''
}
out += EOL
out += '\n'
}
}
return out.trim()
Expand Down
3 changes: 1 addition & 2 deletions lib/commands/outdated.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const os = require('node:os')
const { resolve } = require('node:path')
const { stripVTControlCharacters } = require('node:util')
const pacote = require('pacote')
Expand Down Expand Up @@ -335,7 +334,7 @@ class Outdated extends ArboristWorkspaceCmd {
}

return out.join(':')
}).join(os.EOL)
}).join('\n')
}

makeJSON (list) {
Expand Down
3 changes: 1 addition & 2 deletions lib/commands/sbom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const { EOL } = require('os')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const BaseCommand = require('../base-command.js')
const log = require('proc-log')
Expand Down Expand Up @@ -77,7 +76,7 @@ class SBOM extends BaseCommand {

if (errors.size > 0) {
throw Object.assign(
new Error([...errors].join(EOL)),
new Error([...errors].join('\n')),
{ code: 'ESBOMPROBLEMS' }
)
}
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function STRIP_C01 (str) {
const formatWithOptions = ({ prefix: prefixes = [], eol = '\n', ...options }, ...args) => {
const prefix = prefixes.filter(p => p != null).join(' ')
const formatted = STRIP_C01(baseFormatWithOptions(options, ...args))
// Splitting could be changed to only `\n` once we are sure we only emit unix newlines.
// The eol param to this function will put the correct newlines in place for the returned string.
const lines = formatted.split(/\r?\n/)
return lines.reduce((acc, l) => `${acc}${prefix}${prefix && l ? ' ' : ''}${l}${eol}`, '')
}
Expand Down
3 changes: 1 addition & 2 deletions test/lib/utils/exit-handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const t = require('tap')
const os = require('os')
const fs = require('fs')
const fsMiniPass = require('fs-minipass')
const { join, resolve } = require('path')
Expand Down Expand Up @@ -146,7 +145,7 @@ t.test('handles unknown error with logs and debug file', async (t) => {

let skippedLogs = 0
logs.forEach((logItem, i) => {
const logLines = logItem.split(os.EOL).map(l => `${i} ${l}`)
const logLines = logItem.split('\n').map(l => `${i} ${l}`)
for (const line of logLines) {
if (line.includes('logfile') && line.includes('cleaning')) {
skippedLogs++
Expand Down
6 changes: 2 additions & 4 deletions workspaces/libnpmdiff/lib/format-diff.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const EOL = '\n'

const colorizeDiff = require('@npmcli/disparity-colors')
const jsDiff = require('diff')

Expand Down Expand Up @@ -35,15 +33,15 @@ const formatDiff = ({ files, opts = {}, refs, versions }) => {
}

if (opts.diffNameOnly) {
res += `${filename}${EOL}`
res += `${filename}\n`
continue
}

let patch = ''
let headerLength = 0
const header = str => {
headerLength++
patch += `${str}${EOL}`
patch += `${str}\n`
}

// manually build a git diff-compatible header
Expand Down

0 comments on commit 1e46f0c

Please sign in to comment.