Skip to content

Commit b9dcece

Browse files
authored
feat: support --editor option for team note creation (#73)
* feat: support --editor option for team note creation * fix: lint
1 parent f216fa5 commit b9dcece

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/commands/notes/create.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as fs from 'fs'
99
import HackMDCommand from '../../command'
1010
import {
1111
commentPermission,
12+
editor,
1213
noteContent,
1314
notePermission,
1415
noteTitle,
@@ -37,10 +38,7 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q
3738
readPermission: notePermission(),
3839
writePermission: notePermission(),
3940
commentPermission: commentPermission(),
40-
editor: Flags.boolean({
41-
char: 'e',
42-
description: 'create note with $EDITOR',
43-
}),
41+
editor,
4442
...CliUx.ux.table.flags(),
4543
}
4644

src/commands/team-notes/create.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {CommentPermissionType, CreateNoteOptions, NotePermissionRole} from '@hackmd/api/dist/type'
22
import {CliUx, Flags} from '@oclif/core'
3+
import fs from 'fs'
34

45
import HackMDCommand from '../../command'
5-
import {commentPermission, noteContent, notePermission, noteTitle, teamPath} from '../../flags'
6-
import {safeStdinRead} from '../../utils'
6+
import {commentPermission, editor, noteContent, notePermission, noteTitle, teamPath} from '../../flags'
7+
import openEditor from '../../open-editor'
8+
import {safeStdinRead, temporaryMD} from '../../utils'
79

810
export default class Create extends HackMDCommand {
911
static description = 'Create a team note'
@@ -26,6 +28,7 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q n
2628
readPermission: notePermission(),
2729
writePermission: notePermission(),
2830
commentPermission: commentPermission(),
31+
editor,
2932
...CliUx.ux.table.flags(),
3033
}
3134

@@ -46,6 +49,17 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q n
4649
this.error('Flag teamPath could not be empty')
4750
}
4851

52+
if (flags.editor) {
53+
try {
54+
const mdFile = temporaryMD()
55+
await openEditor(mdFile)
56+
57+
options.content = fs.readFileSync(mdFile).toString()
58+
} catch (e) {
59+
this.error(e as Error)
60+
}
61+
}
62+
4963
try {
5064
const APIClient = await this.getAPIClient()
5165
const note = await APIClient.createTeamNote(teamPath, options)

src/flags.ts

+5
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ export const notePermission = Flags.build({
2222
export const commentPermission = Flags.build({
2323
description: 'set comment permission: disabled, forbidden, owners, signed_in_users, everyone'
2424
})
25+
26+
export const editor = Flags.boolean({
27+
char: 'e',
28+
description: 'create note with $EDITOR',
29+
})

0 commit comments

Comments
 (0)