1- import { CommentPermissionType , CreateNoteOptions , NotePermissionRole } from '@hackmd/api/dist/type'
1+ import {
2+ CommentPermissionType ,
3+ CreateNoteOptions ,
4+ NotePermissionRole ,
5+ } from '@hackmd/api/dist/type'
26import { CliUx , Flags } from '@oclif/core'
7+ import * as fs from 'fs'
38
49import 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'
718
8- export default class Create extends HackMDCommand {
19+ export default class CreateCommand extends HackMDCommand {
920 static description = 'Create a note'
1021
1122 static examples = [
@@ -16,7 +27,7 @@ export default class Create extends HackMDCommand {
1627raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null` ,
1728
1829 'Or you can pipe content via Unix pipeline:' ,
19- 'cat README.md | hackmd-cli notes create'
30+ 'cat README.md | hackmd-cli notes create' ,
2031 ]
2132
2233 static flags = {
@@ -26,40 +37,59 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q
2637 readPermission : notePermission ( ) ,
2738 writePermission : notePermission ( ) ,
2839 commentPermission : commentPermission ( ) ,
40+ editor : Flags . boolean ( {
41+ char : 'e' ,
42+ description : 'create note with $EDITOR' ,
43+ } ) ,
2944 ...CliUx . ux . table . flags ( ) ,
3045 }
3146
3247 async run ( ) {
33- const { flags} = await this . parse ( Create )
48+ const { flags} = await this . parse ( CreateCommand )
3449 const pipeString = safeStdinRead ( )
3550
3651 const options : CreateNoteOptions = {
3752 title : flags . title ,
3853 content : pipeString || flags . content ,
3954 readPermission : flags . readPermission as NotePermissionRole ,
4055 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+ }
4268 }
4369
4470 try {
4571 const APIClient = await this . getAPIClient ( )
4672 const note = await APIClient . createNote ( options )
4773
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+ } ,
5587 } ,
56- teamPath : {
57- header : 'Team path'
88+ {
89+ printLine : this . log . bind ( this ) ,
90+ ...flags ,
5891 }
59- } , {
60- printLine : this . log . bind ( this ) ,
61- ...flags
62- } )
92+ )
6393 } catch ( e ) {
6494 this . log ( 'Create note failed' )
6595 this . error ( e as Error )
0 commit comments