Skip to content

Commit 107c804

Browse files
committed
Support export file content to pipe
1 parent 6e21178 commit 107c804

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/commands/export.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import {Command, flags} from '@oclif/command'
33
import * as path from 'path'
44

55
import {APIClient} from '../api'
6+
import {pipeToFile as pipeStream} from '../utils'
67

78
export default class Export extends Command {
8-
static description = 'Export note to local file'
9+
static description = 'Export note to local file or stdout(if the output_file param is omitted)'
910

1011
static examples = [
1112
'$ codimd-cli export [--pdf|--md|--html] <note_id> <output_file>',
@@ -39,9 +40,18 @@ export default class Export extends Command {
3940
exportType = ExportType.MD
4041
}
4142

42-
let outputPath = path.resolve(process.cwd(), args.output)
4343
try {
44-
await APIClient.export(args.noteId, exportType, outputPath)
44+
const stream = await APIClient.exportStream(args.noteId, exportType)
45+
46+
if (args.output) {
47+
const outputPath = path.resolve(process.cwd(), args.output)
48+
await pipeStream(stream, outputPath)
49+
} else {
50+
await APIClient.exportStream(args.noteId, exportType)
51+
52+
// tslint:disable-next-line: align
53+
; (stream as any).pipe(process.stdout)
54+
}
4555
} catch (e) {
4656
this.log('Note export failed')
4757
this.error(e)

src/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as fs from 'fs-extra'
2+
3+
export async function pipeToFile(stream: any, output: string) {
4+
const fileStream = fs.createWriteStream(output)
5+
6+
await new Promise((resolve, reject) => {
7+
if (!stream) {
8+
return reject('No stream given')
9+
}
10+
11+
stream.pipe(fileStream)
12+
stream.on('error', (err: any) => {
13+
reject(err)
14+
})
15+
fileStream.on('finish', function () {
16+
resolve()
17+
})
18+
})
19+
}

0 commit comments

Comments
 (0)