Skip to content
Draft
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
33 changes: 32 additions & 1 deletion packages/cli-kit/src/private/node/content-tokens.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {LinkContentToken} from './content-tokens.js'
import {LinkContentToken, LinesDiffContentToken} from './content-tokens.js'
import colors from '../../public/node/colors.js'
import {describe, expect, test} from 'vitest'

describe('LinkContentToken', () => {
Expand All @@ -20,3 +21,33 @@ describe('LinkContentToken', () => {
expect(got.output()).toEqual(fallback)
})
})

describe('LinesDiffContentToken', () => {
test('formats added and removed lines correctly', () => {
// Given
const changes = [
{value: 'same\n', count: 1},
{value: 'removed\n', count: 1, removed: true},
{value: 'added\n', count: 1, added: true},
]
const token = new LinesDiffContentToken(changes)

// When
const output = token.output()

// Then
expect(output).toEqual(['same\n', colors.magenta('- removed\n'), colors.green('+ added\n')])
})

test('handles multiple lines in a single change part', () => {
// Given
const changes = [{value: 'added1\nadded2\n', count: 2, added: true}]
const token = new LinesDiffContentToken(changes)

// When
const output = token.output()

// Then
expect(output).toEqual([colors.green('+ added1\n'), colors.green('+ added2\n')])
})
})
40 changes: 19 additions & 21 deletions packages/cli-kit/src/private/node/content-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,25 @@ export class JsonContentToken extends ContentToken<any> {

export class LinesDiffContentToken extends ContentToken<Change[]> {
output(): string[] {
return this.value
.map((part) => {
if (part.added) {
return part.value
.split(/\n/)
.filter((line) => line !== '')
.map((line) => {
return colors.green(`+ ${line}\n`)
})
} else if (part.removed) {
return part.value
.split(/\n/)
.filter((line) => line !== '')
.map((line) => {
return colors.magenta(`- ${line}\n`)
})
} else {
return part.value
}
})
.flat()
return this.value.flatMap((part) => {
if (part.added) {
return part.value
.split(/\n/)
.filter((line) => line !== '')
.map((line) => {
return colors.green(`+ ${line}\n`)
})
} else if (part.removed) {
return part.value
.split(/\n/)
.filter((line) => line !== '')
.map((line) => {
return colors.magenta(`- ${line}\n`)
})
} else {
return part.value
}
})
}
}

Expand Down
Loading