Skip to content

Commit c3d7c2f

Browse files
fix: terminate the last line of file output with a newline
1 parent ceb54c7 commit c3d7c2f

2 files changed

Lines changed: 6 additions & 9 deletions

File tree

‎packages/cli/src/conversion.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function encodeToToon(config: {
3838
const toonOutput = encode(data, encodeOptions)
3939

4040
if (config.output) {
41-
await fsp.writeFile(config.output, toonOutput, 'utf-8')
41+
await fsp.writeFile(config.output, `${toonOutput}\n`, 'utf-8')
4242
}
4343
else {
4444
console.log(toonOutput)
@@ -119,9 +119,7 @@ async function writeStream(
119119
isFirst = false
120120
}
121121

122-
// Stdout gets a trailing newline so the shell prompt resumes on a fresh line.
123-
if (!outputPath)
124-
process.stdout.write('\n')
122+
await write('\n')
125123
}
126124
finally {
127125
await fileHandle?.close()

‎packages/cli/test/cli.test.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('toon CLI', () => {
4848
indent: 2,
4949
})
5050

51-
expect(output).toBe(expected)
51+
expect(output).toBe(`${expected}\n`)
5252
expect(stderr).toMatch(/Encoded .* → .*/)
5353
})
5454

@@ -71,7 +71,7 @@ describe('toon CLI', () => {
7171
try {
7272
const { stderr } = await runCli(['--output', 'output.toon'], { cwd: directory })
7373

74-
expect(await readOutput(directory, 'output.toon')).toBe(encode(data))
74+
expect(await readOutput(directory, 'output.toon')).toBe(`${encode(data)}\n`)
7575
expect(stderr).toMatch(/Encoded.*stdin[^\n\r\u2028\u2029\u2192]*\u2192.*output\.toon/)
7676
}
7777
finally {
@@ -87,7 +87,7 @@ describe('toon CLI', () => {
8787

8888
await runCli(['empty.json', '--output', 'output.toon'], { cwd: directory })
8989

90-
expect(await readOutput(directory, 'output.toon')).toBe(encode(data))
90+
expect(await readOutput(directory, 'output.toon')).toBe(`${encode(data)}\n`)
9191
})
9292

9393
it('writes a large JSON input identically to one-shot encoding', async () => {
@@ -104,13 +104,12 @@ describe('toon CLI', () => {
104104

105105
const { stderr } = await runCli(['large-input.json', '--output', 'output.toon'], { cwd: directory })
106106

107-
// Streaming has to produce byte-identical output to the one-shot `encode()`.
108107
const expected = encode(data, {
109108
delimiter: DEFAULT_DELIMITER,
110109
indent: 2,
111110
})
112111

113-
expect(await readOutput(directory, 'output.toon')).toBe(expected)
112+
expect(await readOutput(directory, 'output.toon')).toBe(`${expected}\n`)
114113
expect(stderr).toMatch(/Encoded .* → .*/)
115114
})
116115
})

0 commit comments

Comments
 (0)