Skip to content

Commit

Permalink
Download file as stream
Browse files Browse the repository at this point in the history
  • Loading branch information
dariober authored and garrettjstevens committed Sep 20, 2024
1 parent 4ead5e0 commit 7112709
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/apollo-cli/src/commands/file/download.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { writeFileSync } from 'node:fs'
import { createWriteStream, writeFileSync } from 'node:fs'
import { Writable } from 'node:stream'

import { Flags } from '@oclif/core'
import { Response } from 'undici'
Expand Down Expand Up @@ -60,12 +61,14 @@ export default class Download extends BaseCommand<typeof Download> {
access.accessToken,
`files/${fileId}`,
)
const text = await res.text()

let { output } = flags
if (output === undefined) {
output = fileRec['basename' as keyof typeof fileRec] as string
}
writeFileSync(output === '-' ? 1 : output, text)
const fileWriteStream = createWriteStream(output)
await res.body?.pipeTo(
Writable.toWeb(output === '-' ? process.stdout : fileWriteStream),
)
fileWriteStream.close()
}
}

0 comments on commit 7112709

Please sign in to comment.