1
- import { CommentPermissionType , CreateNoteOptions , NotePermissionRole } from '@hackmd/api/dist/type'
1
+ import {
2
+ CommentPermissionType ,
3
+ CreateNoteOptions ,
4
+ NotePermissionRole ,
5
+ } from '@hackmd/api/dist/type'
2
6
import { CliUx , Flags } from '@oclif/core'
7
+ import * as fs from 'fs'
3
8
4
9
import HackMDCommand from '../../command'
5
- import { commentPermission , noteContent , notePermission , noteTitle } from '../../flags'
6
- import { safeStdinRead } from '../../utils'
10
+ import {
11
+ commentPermission ,
12
+ noteContent ,
13
+ notePermission ,
14
+ noteTitle ,
15
+ } from '../../flags'
16
+ import openEditor from '../../open-editor'
17
+ import { safeStdinRead , temporaryMD } from '../../utils'
7
18
8
- export default class Create extends HackMDCommand {
19
+ export default class CreateCommand extends HackMDCommand {
9
20
static description = 'Create a note'
10
21
11
22
static examples = [
@@ -16,7 +27,7 @@ export default class Create extends HackMDCommand {
16
27
raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null` ,
17
28
18
29
'Or you can pipe content via Unix pipeline:' ,
19
- 'cat README.md | hackmd-cli notes create'
30
+ 'cat README.md | hackmd-cli notes create' ,
20
31
]
21
32
22
33
static flags = {
@@ -26,40 +37,59 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q
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 )
48
+ const { flags} = await this . parse ( CreateCommand )
34
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
71
const APIClient = await this . getAPIClient ( )
46
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
94
this . log ( 'Create note failed' )
65
95
this . error ( e as Error )
0 commit comments