1
- import { CommentPermissionType , CreateNoteOptions , NotePermissionRole } from '@hackmd/api/dist/type'
2
- import { CliUx , Flags } from '@oclif/core'
1
+ import {
2
+ CommentPermissionType ,
3
+ CreateNoteOptions ,
4
+ NotePermissionRole ,
5
+ } from "@hackmd/api/dist/type" ;
6
+ import { CliUx , Flags } from "@oclif/core" ;
7
+ import * as fs from "fs" ;
3
8
4
- import HackMDCommand from '../../command'
5
- import { commentPermission , noteContent , notePermission , noteTitle } from '../../flags'
6
- import { safeStdinRead } from '../../utils'
9
+ import HackMDCommand from "../../command" ;
10
+ import {
11
+ commentPermission ,
12
+ noteContent ,
13
+ notePermission ,
14
+ noteTitle ,
15
+ } from "../../flags" ;
16
+ import { safeStdinRead , temporaryMD } from "../../utils" ;
17
+ import openEditor from "../../openEditor" ;
7
18
8
- export default class Create extends HackMDCommand {
9
- static description = ' Create a note'
19
+ export default class CreateCommand extends HackMDCommand {
20
+ static description = " Create a note" ;
10
21
11
22
static examples = [
12
23
"notes create --content='# A new note' --readPermission=owner --writePermission=owner --commentPermission=disabled" ,
@@ -15,54 +26,73 @@ export default class Create extends HackMDCommand {
15
26
────────────────────── ──────────────────────────────── ────────────────────── ────────
16
27
raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null` ,
17
28
18
- ' Or you can pipe content via Unix pipeline:' ,
19
- ' cat README.md | hackmd-cli notes create'
20
- ]
29
+ " Or you can pipe content via Unix pipeline:" ,
30
+ " cat README.md | hackmd-cli notes create" ,
31
+ ] ;
21
32
22
33
static flags = {
23
- help : Flags . help ( { char : 'h' } ) ,
34
+ help : Flags . help ( { char : "h" } ) ,
24
35
title : noteTitle ( ) ,
25
36
content : noteContent ( ) ,
26
37
readPermission : notePermission ( ) ,
27
38
writePermission : notePermission ( ) ,
28
39
commentPermission : commentPermission ( ) ,
40
+ editor : Flags . boolean ( {
41
+ char : "e" ,
42
+ description : "create note with $EDITOR" ,
43
+ } ) ,
29
44
...CliUx . ux . table . flags ( ) ,
30
- }
45
+ } ;
31
46
32
47
async run ( ) {
33
- const { flags} = await this . parse ( Create )
34
- const pipeString = safeStdinRead ( )
48
+ const { flags } = await this . parse ( CreateCommand ) ;
49
+ const pipeString = safeStdinRead ( ) ;
35
50
36
51
const options : CreateNoteOptions = {
37
52
title : flags . title ,
38
53
content : pipeString || flags . content ,
39
54
readPermission : flags . readPermission as NotePermissionRole ,
40
55
writePermission : flags . writePermission as NotePermissionRole ,
41
- commentPermission : flags . commentPermission as CommentPermissionType
56
+ commentPermission : flags . commentPermission as CommentPermissionType ,
57
+ } ;
58
+
59
+ if ( flags . editor ) {
60
+ try {
61
+ const mdFile = temporaryMD ( ) ;
62
+ await openEditor ( mdFile ) ;
63
+
64
+ options . content = fs . readFileSync ( mdFile ) . toString ( ) ;
65
+ } catch ( e ) {
66
+ this . error ( e as Error ) ;
67
+ }
42
68
}
43
69
44
70
try {
45
- const APIClient = await this . getAPIClient ( )
46
- const note = await APIClient . createNote ( options )
71
+ const APIClient = await this . getAPIClient ( ) ;
72
+ const note = await APIClient . createNote ( options ) ;
47
73
48
- CliUx . ux . table ( [ note ] , {
49
- id : {
50
- header : 'ID' ,
51
- } ,
52
- title : { } ,
53
- userPath : {
54
- header : 'User path'
74
+ CliUx . ux . table (
75
+ [ note ] ,
76
+ {
77
+ id : {
78
+ header : "ID" ,
79
+ } ,
80
+ title : { } ,
81
+ userPath : {
82
+ header : "User path" ,
83
+ } ,
84
+ teamPath : {
85
+ header : "Team path" ,
86
+ } ,
55
87
} ,
56
- teamPath : {
57
- header : 'Team path'
88
+ {
89
+ printLine : this . log . bind ( this ) ,
90
+ ...flags ,
58
91
}
59
- } , {
60
- printLine : this . log . bind ( this ) ,
61
- ...flags
62
- } )
92
+ ) ;
63
93
} catch ( e ) {
64
- this . log ( ' Create note failed' )
65
- this . error ( e as Error )
94
+ this . log ( " Create note failed" ) ;
95
+ this . error ( e as Error ) ;
66
96
}
67
97
}
68
98
}
0 commit comments