-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreate.ts
More file actions
108 lines (95 loc) · 3.84 KB
/
Copy pathcreate.ts
File metadata and controls
108 lines (95 loc) · 3.84 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
104
105
106
107
108
import type {CommentPermissionType, CreateNoteOptions, NotePermissionRole} from '@hackmd/api'
import {Flags, ux} from '@oclif/core'
import fs from 'node:fs'
import HackMDCommand from '../../command'
import {
commentPermission, editor, noteContent, notePermission, noteTags, noteTitle, parentFolderId, teamPath,
} from '../../flags'
import {openEditor} from '../../open-editor'
import {safeStdinRead, temporaryMD} from '../../utils'
export default class Create extends HackMDCommand {
static description = 'Create a team note'
static examples = [
`$ hackmd-cli team-notes create --teamPath=CLI-test --content='# A new note' --readPermission=owner --writePermission=owner --commentPermission=disabled
ID Title User Path Team Path
────────────────────── ──────────────────────────────── ────────────────────── ────────
raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null `,
[
'$ hackmd-cli team-notes create --teamPath=CLI-test ',
'--parentFolderId=fc7a3d48-4a07-4cbf-bf4f-e65dd896e01c ',
"--content='# A new note' --readPermission=owner ",
'--writePermission=owner --commentPermission=disabled\n',
'ID Title User Path Team Path\n',
'────────────────────── ──────────────────────────────── ────────────────────── ────────\n',
'raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null ',
].join(''),
'Or you can pipe content via Unix pipeline:',
'cat README.md | hackmd-cli team-notes create --teamPath=CLI-test',
]
static flags = {
commentPermission,
content: noteContent,
editor,
help: Flags.help({char: 'h'}),
parentFolderId,
readPermission: notePermission,
tags: noteTags,
teamPath,
title: noteTitle,
writePermission: notePermission,
...ux.table.flags(),
}
async run() {
const {flags} = await this.parse(Create)
const pipeString = safeStdinRead()
const {commentPermission, content, parentFolderId, readPermission, tags, teamPath, title, writePermission} = flags
const options: CreateNoteOptions & {tags?: string[]} = {
commentPermission: commentPermission as CommentPermissionType,
content: pipeString || content,
parentFolderId,
readPermission: readPermission as NotePermissionRole,
title,
writePermission: writePermission as NotePermissionRole,
}
if (tags !== undefined) {
options.tags = tags.split(',').map((t: string) => t.trim()).filter(Boolean)
}
if (!teamPath) {
this.error('Flag teamPath could not be empty')
}
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.createTeamNote(teamPath, 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 team note failed')
this.error(error as Error)
}
}
}