-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreate.ts
More file actions
103 lines (92 loc) · 2.96 KB
/
create.ts
File metadata and controls
103 lines (92 loc) · 2.96 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {
CommentPermissionType,
CreateNoteOptions,
NotePermissionRole,
} from '@hackmd/api/dist/type'
import {Flags, ux} from '@oclif/core'
import * as fs from 'node:fs'
import HackMDCommand from '../../command'
import {
commentPermission,
editor,
noteContent,
notePermission,
noteTags,
noteTitle,
} from '../../flags'
import {openEditor} from '../../open-editor'
import {safeStdinRead, temporaryMD} from '../../utils'
export default class CreateCommand extends HackMDCommand {
static description = 'Create a note'
static examples = [
"notes create --content='# A new note' --readPermission=owner --writePermission=owner --commentPermission=disabled",
`ID Title Tags 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 = {
commentPermission,
content: noteContent,
editor,
help: Flags.help({char: 'h'}),
readPermission: notePermission,
tags: noteTags,
title: noteTitle,
writePermission: notePermission,
...ux.table.flags(),
}
async run() {
const {flags} = await this.parse(CreateCommand)
const pipeString = safeStdinRead()
const options: CreateNoteOptions & {tags?: string[]} = {
commentPermission: flags.commentPermission as CommentPermissionType,
content: pipeString || flags.content,
readPermission: flags.readPermission as NotePermissionRole,
title: flags.title,
writePermission: flags.writePermission as NotePermissionRole,
}
if (flags.tags !== undefined) {
options.tags = flags.tags.split(',').map((t: string) => t.trim()).filter(Boolean)
}
if (flags.editor) {
try {
const mdFile = temporaryMD()
await openEditor(mdFile)
options.content = fs.readFileSync(mdFile).toString()
} catch (error) {
this.error(error as Error)
}
}
try {
const APIClient = await this.getAPIClient()
const note = await APIClient.createNote(options as CreateNoteOptions)
ux.table(
[note],
{
id: {
header: 'ID',
},
tags: {
get: row => (row.tags ?? []).join(', '),
},
teamPath: {
header: 'Team path',
},
title: {},
userPath: {
header: 'User path',
},
},
{
printLine: this.log.bind(this),
...flags,
},
)
} catch (error) {
this.log('Create note failed')
this.error(error as Error)
}
}
}