/
create.ts
68 lines (58 loc) · 2.23 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {CommentPermissionType, CreateNoteOptions, NotePermissionRole} from '@hackmd/api/dist/type'
import {CliUx, Flags} from '@oclif/core'
import HackMDCommand from '../../command'
import {commentPermission, noteContent, notePermission, noteTitle} from '../../flags'
import {safeStdinRead} from '../../utils'
export default class Create extends HackMDCommand {
static description = 'Create a note'
static examples = [
"notes create --content='# A new note' --readPermission=owner --writePermission=owner --commentPermission=disabled",
`ID Title User Path Team Path
────────────────────── ──────────────────────────────── ────────────────────── ────────
raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null`,
'Or you can pipe content via Unix pipeline:',
'cat README.md | hackmd-cli notes create'
]
static flags = {
help: Flags.help({char: 'h'}),
title: noteTitle(),
content: noteContent(),
readPermission: notePermission(),
writePermission: notePermission(),
commentPermission: commentPermission(),
...CliUx.ux.table.flags(),
}
async run() {
const {flags} = await this.parse(Create)
const pipeString = safeStdinRead()
const options: CreateNoteOptions = {
title: flags.title,
content: pipeString || flags.content,
readPermission: flags.readPermission as NotePermissionRole,
writePermission: flags.writePermission as NotePermissionRole,
commentPermission: flags.commentPermission as CommentPermissionType
}
try {
const APIClient = await this.getAPIClient()
const note = await APIClient.createNote(options)
CliUx.ux.table([note], {
id: {
header: 'ID',
},
title: {},
userPath: {
header: 'User path'
},
teamPath: {
header: 'Team path'
}
}, {
printLine: this.log.bind(this),
...flags
})
} catch (e) {
this.log('Create note failed')
this.error(e as Error)
}
}
}