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" ;
38
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" ;
718
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" ;
1021
1122 static examples = [
1223 "notes create --content='# A new note' --readPermission=owner --writePermission=owner --commentPermission=disabled" ,
@@ -15,54 +26,73 @@ export default class Create extends HackMDCommand {
1526────────────────────── ──────────────────────────────── ────────────────────── ────────
1627raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null` ,
1728
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+ ] ;
2132
2233 static flags = {
23- help : Flags . help ( { char : 'h' } ) ,
34+ help : Flags . help ( { char : "h" } ) ,
2435 title : noteTitle ( ) ,
2536 content : noteContent ( ) ,
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 ( ) ,
30- }
45+ } ;
3146
3247 async run ( ) {
33- const { flags} = await this . parse ( Create )
34- const pipeString = safeStdinRead ( )
48+ const { flags } = await this . parse ( CreateCommand ) ;
49+ 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 {
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 ) ;
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 ) {
64- this . log ( ' Create note failed' )
65- this . error ( e as Error )
94+ this . log ( " Create note failed" ) ;
95+ this . error ( e as Error ) ;
6696 }
6797 }
6898}
0 commit comments