File tree Expand file tree Collapse file tree 2 files changed +32
-3
lines changed
Expand file tree Collapse file tree 2 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -3,9 +3,10 @@ import {Command, flags} from '@oclif/command'
33import * as path from 'path'
44
55import { APIClient } from '../api'
6+ import { pipeToFile as pipeStream } from '../utils'
67
78export 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 )
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments