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'
3
3
import * as path from 'path'
4
4
5
5
import { APIClient } from '../api'
6
+ import { pipeToFile as pipeStream } from '../utils'
6
7
7
8
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) '
9
10
10
11
static examples = [
11
12
'$ codimd-cli export [--pdf|--md|--html] <note_id> <output_file>' ,
@@ -39,9 +40,18 @@ export default class Export extends Command {
39
40
exportType = ExportType . MD
40
41
}
41
42
42
- let outputPath = path . resolve ( process . cwd ( ) , args . output )
43
43
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
+ }
45
55
} catch ( e ) {
46
56
this . log ( 'Note export failed' )
47
57
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