Skip to content

Commit 4aa819e

Browse files
James TsaiJames Tsai
authored andcommitted
feat: add login, logout with access token and HackMDCommand
1 parent 2a54032 commit 4aa819e

File tree

15 files changed

+122
-67
lines changed

15 files changed

+122
-67
lines changed

src/api.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/command.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import API from '@hackmd/api'
2+
import {CliUx, Command} from '@oclif/core'
3+
import fs from 'fs'
4+
5+
import config from './config'
6+
import {getConfigFilePath} from './utils'
7+
8+
export default abstract class HackMDCommand extends Command {
9+
async getAPIClient() {
10+
const token = config.accessToken || await CliUx.ux.prompt('Enter your access token')
11+
const APIClient = new API(token, config.hackmdAPIEndpointURL)
12+
13+
try {
14+
await APIClient.getMe()
15+
if (!config.accessToken) {
16+
const configFilePath = getConfigFilePath()
17+
const newConfigFile = require(configFilePath)
18+
newConfigFile.accessToken = token
19+
fs.writeFile(configFilePath, JSON.stringify(newConfigFile, null, 2), function (err) {
20+
if (err) {
21+
throw err
22+
}
23+
})
24+
}
25+
26+
return APIClient
27+
} catch (e) {
28+
this.log('Please ensure your credentials are correct')
29+
this.error(e)
30+
}
31+
}
32+
33+
}

src/commands/history.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {CliUx, Command, Flags} from '@oclif/core'
1+
import {CliUx, Flags} from '@oclif/core'
22

3-
import {APIClient} from '../api'
3+
import HackMDCommand from '../command'
44

5-
export default class History extends Command {
5+
export default class History extends HackMDCommand {
66
static description = 'List user browse history'
77

88
static examples = [
@@ -22,6 +22,7 @@ BnC6gN0_TfStV2KKmPPXeg Welcome to your team's workspace null C
2222
const {flags} = await this.parse(History)
2323

2424
try {
25+
const APIClient = await this.getAPIClient()
2526
const history = await APIClient.getHistory()
2627

2728
CliUx.ux.table(history, {

src/commands/login.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,29 @@
1-
import API from '@hackmd/api'
2-
import {CliUx, Command, Flags} from '@oclif/core'
3-
import fs from 'fs'
1+
import {Flags} from '@oclif/core'
42

5-
import config from '../config'
6-
import {getConfigFilePath} from '../utils'
3+
import HackMDCommand from '../command'
74

8-
export default class Login extends Command {
5+
export default class Login extends HackMDCommand {
96
static description = 'Login to HackMD server from CLI'
107

118
static examples = [
129
`$ hackmd-cli login
1310
14-
Enter your email: MY_ACCESS_TOKEN
11+
Enter your access token: MY_ACCESS_TOKEN
1512
16-
Login successfully!
13+
Login successfully
1714
`
1815
]
1916

2017
static flags = {
21-
help: Flags.help({char: 'h'}),
22-
accessToken: Flags.string({char: 'u', description: 'Login with accesstoken'}),
18+
help: Flags.help({char: 'h'})
2319
}
2420

2521
async run() {
26-
const {flags} = await this.parse(Login)
27-
28-
const token = flags.accessToken || config.accessToken || await CliUx.ux.prompt('Enter your access token')
29-
3022
try {
31-
const APIClient = new API(token, config.hackmdAPIEndpointURL)
32-
await APIClient.getMe()
33-
34-
const configFilePath = getConfigFilePath()
35-
const newConfigFile = require(configFilePath)
36-
newConfigFile.accessToken = token
37-
38-
fs.writeFile(configFilePath, JSON.stringify(newConfigFile, null, 2), function (err) {
39-
if (err) {
40-
throw err
41-
}
42-
})
43-
44-
return this.log('Login successfully')
23+
await this.getAPIClient()
24+
this.log('Login successfully')
4525
} catch (err) {
46-
this.log('Login failed, please ensure your credentials are correct')
26+
this.log('Login failed')
4727
this.error(err)
4828
}
4929
}

src/commands/logout.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {Flags} from '@oclif/core'
2+
import fs from 'fs'
3+
4+
import HackMDCommand from '../command'
5+
import {getConfigFilePath} from '../utils'
6+
7+
export default class Logout extends HackMDCommand {
8+
static description = 'Login to HackMD server from CLI'
9+
10+
static examples = [
11+
`$ hackmd-cli logout
12+
13+
You've logged out successfully
14+
`
15+
]
16+
17+
static flags = {
18+
help: Flags.help({char: 'h'})
19+
}
20+
21+
async run() {
22+
try {
23+
const configFilePath = getConfigFilePath()
24+
const newConfigFile = require(configFilePath)
25+
newConfigFile.accessToken = ''
26+
fs.writeFile(configFilePath, JSON.stringify(newConfigFile, null, 2), function (err) {
27+
if (err) {
28+
throw err
29+
}
30+
})
31+
this.log("You've logged out successfully")
32+
} catch (err) {
33+
this.error(err)
34+
}
35+
}
36+
}

src/commands/notes/create.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {CommentPermissionType, CreateNoteOptions, NotePermissionRole} from '@hackmd/api/dist/type'
2-
import {CliUx, Command, Flags} from '@oclif/core'
2+
import {CliUx, Flags} from '@oclif/core'
33

4-
import {APIClient} from '../../api'
4+
import HackMDCommand from '../../command'
55
import {commentPermission, noteContent, notePermission, noteTitle} from '../../flags'
66
import readStdin from '../../read-stdin-stream'
77

8-
export default class Create extends Command {
8+
export default class Create extends HackMDCommand {
99
static description = 'Create a note'
1010

1111
static examples = [
@@ -42,6 +42,7 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q
4242
}
4343

4444
try {
45+
const APIClient = await this.getAPIClient()
4546
const note = await APIClient.createNote(options)
4647

4748
CliUx.ux.table([note], {

src/commands/notes/delete.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {Command, Flags} from '@oclif/core'
1+
import {Flags} from '@oclif/core'
22

3-
import {APIClient} from '../../api'
3+
import HackMDCommand from '../../command'
44
import {noteId} from '../../flags'
55

6-
export default class Delete extends Command {
6+
export default class Delete extends HackMDCommand {
77
static description = 'Delete a note'
88

99
static examples = [
@@ -24,6 +24,7 @@ export default class Delete extends Command {
2424
}
2525

2626
try {
27+
const APIClient = await this.getAPIClient()
2728
await APIClient.deleteNote(noteId)
2829
} catch (e) {
2930
this.log('Delete note failed')

src/commands/notes/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {CliUx, Command, Flags} from '@oclif/core'
1+
import {CliUx, Flags} from '@oclif/core'
22

3-
import {APIClient} from '../../api'
3+
import HackMDCommand from '../../command'
44
import {noteId} from '../../flags'
55

6-
export default class IndexCommand extends Command {
6+
export default class IndexCommand extends HackMDCommand {
77
static description = 'HackMD notes commands'
88

99
static examples = [
@@ -23,6 +23,7 @@ raUuSTetT5uQbqQfLnz9lA CLI test note gvfz2UB5THiKABQJQnLs6Q n
2323
const {flags} = await this.parse(IndexCommand)
2424

2525
try {
26+
const APIClient = await this.getAPIClient()
2627
const notes = flags.noteId ? [await APIClient.getNote(flags.noteId)] : await APIClient.getNoteList()
2728

2829
CliUx.ux.table(notes, {

src/commands/notes/update.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {Command, Flags} from '@oclif/core'
1+
import {Flags} from '@oclif/core'
22

3-
import {APIClient} from '../../api'
3+
import HackMDCommand from '../../command'
44
import {noteContent, noteId} from '../../flags'
55

6-
export default class Update extends Command {
6+
export default class Update extends HackMDCommand {
77
static description = 'Update note content'
88

99
static examples = [
@@ -25,6 +25,7 @@ export default class Update extends Command {
2525
}
2626

2727
try {
28+
const APIClient = await this.getAPIClient()
2829
await APIClient.updateNoteContent(noteId, content)
2930
} catch (e) {
3031
this.log('Update note content failed')

src/commands/team-notes/create.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {CommentPermissionType, CreateNoteOptions, NotePermissionRole} from '@hackmd/api/dist/type'
2-
import {CliUx, Command, Flags} from '@oclif/core'
2+
import {CliUx, Flags} from '@oclif/core'
33

4-
import {APIClient} from '../../api'
4+
import HackMDCommand from '../../command'
55
import {commentPermission, noteContent, notePermission, noteTitle, teamPath} from '../../flags'
66
import readStdin from '../../read-stdin-stream'
77

8-
export default class Create extends Command {
8+
export default class Create extends HackMDCommand {
99
static description = 'Create a team note'
1010

1111
static examples = [
@@ -47,6 +47,7 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q n
4747
}
4848

4949
try {
50+
const APIClient = await this.getAPIClient()
5051
const note = await APIClient.createTeamNote(teamPath, options)
5152

5253
CliUx.ux.table([note], {

0 commit comments

Comments
 (0)