Skip to content

Commit 009422e

Browse files
James TsaiJames Tsai
authored andcommitted
feat: add notes CRUD sub commands
1 parent f56db22 commit 009422e

7 files changed

Lines changed: 133 additions & 12 deletions

File tree

src/commands/notes/create.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {Command, Flags, CliUx} from '@oclif/core'
2+
3+
import {APIClient} from '../../api'
4+
5+
export default class Create extends Command {
6+
static description = 'Create a note'
7+
8+
static examples = [
9+
`notes create --content='# A new note' --readPermission=owner --writePermission=owner --commentPermission=disabled
10+
ID Title User Path Team Path
11+
────────────────────── ──────────────────────────────── ────────────────────── ────────
12+
raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null `
13+
]
14+
15+
static flags = {
16+
help: Flags.help({char: 'h'}),
17+
title: Flags.string(),
18+
content: Flags.string(),
19+
readPermission: Flags.string(),
20+
writePermission: Flags.string(),
21+
commentPermission: Flags.string(),
22+
...CliUx.ux.table.flags(),
23+
}
24+
25+
async run() {
26+
const {flags} = await this.parse(Create)
27+
const options = {...flags}
28+
29+
try {
30+
// TODO: create note options typing
31+
const note = await APIClient.createNote(options as any)
32+
33+
CliUx.ux.table([note], {
34+
id: {
35+
header: 'ID',
36+
},
37+
title: {},
38+
userPath: {
39+
header: 'User path'
40+
},
41+
teamPath:{
42+
header: 'Team path'
43+
}
44+
}, {
45+
printLine: this.log.bind(this),
46+
...flags
47+
})
48+
} catch (e) {
49+
this.log('Create note failed')
50+
this.error(e)
51+
}
52+
}
53+
}
54+

src/commands/notes/delete.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {Command, Flags} from '@oclif/core'
2+
3+
import {APIClient} from '../../api'
4+
5+
export default class Delete extends Command {
6+
static description = 'Delete a note'
7+
8+
static examples = [
9+
`$ hackmd-cli notes delete --noteId=WNkLM6gkS0Cg2cQ8rv7bYA`
10+
]
11+
12+
static flags = {
13+
help: Flags.help({char: 'h'}),
14+
noteId: Flags.string(),
15+
}
16+
17+
async run() {
18+
const {flags} = await this.parse(Delete)
19+
const {noteId} = flags
20+
21+
if(!noteId) {
22+
this.error('Flag noteId could not be empty')
23+
}
24+
25+
try {
26+
await APIClient.deleteNote(noteId)
27+
} catch (e) {
28+
this.log('Delete note failed')
29+
this.error(e)
30+
}
31+
}
32+
}
33+
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Command, Flags, CliUx } from '@oclif/core'
2-
import {APIClient} from '../api'
2+
import {APIClient} from '../../api'
33

4-
export default class Notes extends Command {
4+
export default class IndexCommand extends Command {
55
static description = 'List user notes'
66

77
static examples = [
@@ -13,15 +13,16 @@ raUuSTetT5uQbqQfLnz9lA CLI test note gvfz2UB5THiKABQJQnLs6Q n
1313

1414
static flags = {
1515
help: Flags.help({char: 'h'}),
16+
noteId: Flags.string(),
1617
...CliUx.ux.table.flags(),
1718
}
1819

1920
async run() {
20-
const {flags} = await this.parse(Notes)
21+
const {flags} = await this.parse(IndexCommand)
2122

2223
try {
23-
const notes = await APIClient.getNoteList()
24-
24+
const notes = flags.noteId ? [await APIClient.getNote(flags.noteId)] : await APIClient.getNoteList()
25+
2526
CliUx.ux.table(notes, {
2627
id: {
2728
header: 'ID',

src/commands/notes/update.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {Command, Flags} from '@oclif/core'
2+
3+
import {APIClient} from '../../api'
4+
5+
export default class Update extends Command {
6+
static description = 'Update note content'
7+
8+
static examples = [
9+
`$ hackmd-cli notes update --teamPath=CLI-test --noteId=WNkLM6gkS0Cg2cQ8rv7bYA --content='# A new title'`
10+
]
11+
12+
static flags = {
13+
help: Flags.help({char: 'h'}),
14+
noteId: Flags.string(),
15+
content: Flags.string()
16+
}
17+
18+
async run() {
19+
const {flags} = await this.parse(Update)
20+
const {noteId, content} = flags
21+
22+
if(!noteId) {
23+
this.error('Flag noteId could not be empty')
24+
}
25+
26+
try {
27+
await APIClient.updateNoteContent(noteId, content)
28+
} catch (e) {
29+
this.log('Update note content failed')
30+
this.error(e)
31+
}
32+
}
33+
}

src/commands/team-notes/create.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ export default class Create extends Command {
66
static description = 'Create a team note'
77

88
static examples = [
9-
`team-notes:create --teamPath=CLI-test --content='# A new note created' --readPermission=owner --writePermission=owner --commentPermission=disabled
10-
ID Title User Path Team Path
9+
`team-notes:create --teamPath=CLI-test --content='# A new note' --readPermission=owner --writePermission=owner --commentPermission=disabled
10+
ID Title User Path Team Path
1111
────────────────────── ──────────────────────────────── ────────────────────── ────────
12-
raUuSTetT5uQbqQfLnz9lA CLI test note gvfz2UB5THiKABQJQnLs6Q null `
12+
raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null `
1313
]
1414

1515
static flags = {
@@ -52,7 +52,7 @@ raUuSTetT5uQbqQfLnz9lA CLI test note gvfz2UB5THiKABQJQnLs6Q n
5252
...flags
5353
})
5454
} catch (e) {
55-
this.log('Update team note content failed')
55+
this.log('Create team note failed')
5656
this.error(e)
5757
}
5858
}

src/commands/team-notes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ BnC6gN0_TfStV2KKmPPXeg Welcome to your team's workspace null CLI-test`,
4545
...flags
4646
})
4747
} catch (e) {
48-
this.log('Fetch team note list failed')
48+
this.log('Fetch team notes failed')
4949
this.error(e)
5050
}
5151
}

src/commands/team-notes/update.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Command, Flags} from '@oclif/core'
33
import {APIClient} from '../../api'
44

55
export default class Update extends Command {
6-
static description = 'Update a team note'
6+
static description = 'Update team note content'
77

88
static examples = [
99
`$ hackmd-cli team-notes update --teamPath=CLI-test --noteId=WNkLM6gkS0Cg2cQ8rv7bYA --content='# A new title'`
@@ -27,7 +27,7 @@ export default class Update extends Command {
2727
if(!noteId) {
2828
this.error('Flag noteId could not be empty')
2929
}
30-
30+
3131
try {
3232
await APIClient.updateTeamNoteContent(teamPath, noteId, content)
3333
} catch (e) {

0 commit comments

Comments
 (0)