Skip to content

Commit 2a54032

Browse files
James TsaiJames Tsai
authored andcommitted
feat: add login with accessToken command
1 parent 4cb7048 commit 2a54032

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

src/commands/login.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import API from '@hackmd/api'
2+
import {CliUx, Command, Flags} from '@oclif/core'
3+
import fs from 'fs'
4+
5+
import config from '../config'
6+
import {getConfigFilePath} from '../utils'
7+
8+
export default class Login extends Command {
9+
static description = 'Login to HackMD server from CLI'
10+
11+
static examples = [
12+
`$ hackmd-cli login
13+
14+
Enter your email: MY_ACCESS_TOKEN
15+
16+
Login successfully!
17+
`
18+
]
19+
20+
static flags = {
21+
help: Flags.help({char: 'h'}),
22+
accessToken: Flags.string({char: 'u', description: 'Login with accesstoken'}),
23+
}
24+
25+
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+
30+
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')
45+
} catch (err) {
46+
this.log('Login failed, please ensure your credentials are correct')
47+
this.error(err)
48+
}
49+
}
50+
}

src/config.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import * as fs from 'fs-extra'
22
import defaults from 'lodash/defaults'
3-
import {homedir} from 'os'
4-
import * as path from 'path'
5-
6-
let configDir
7-
if (process.env.HMD_CLI_CONFIG_DIR) {
8-
configDir = process.env.HMD_CLI_CONFIG_DIR || ''
9-
} else {
10-
configDir = path.join(homedir(), '.hackmd')
11-
}
123

13-
const configFilePath = path.join(configDir, 'config.json')
4+
import {getConfigFilePath} from './utils'
5+
6+
const configFilePath = getConfigFilePath()
147

158
const defaultConfig = {
169
hackmdAPIEndpointURL: 'https://api.hackmd.io/v1'

src/utils.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import * as fs from 'fs-extra'
1+
import {homedir} from 'os'
2+
import * as path from 'path'
23

3-
export async function pipeToFile(stream: any, output: string) {
4-
const fileStream = fs.createWriteStream(output)
4+
export function getConfigFilePath() {
5+
let configDir
6+
if (process.env.HMD_CLI_CONFIG_DIR) {
7+
configDir = process.env.HMD_CLI_CONFIG_DIR || ''
8+
} else {
9+
configDir = path.join(homedir(), '.hackmd')
10+
}
511

6-
await new Promise((resolve, reject) => {
7-
if (!stream) {
8-
return reject('No stream given')
9-
}
10-
11-
stream.pipe(fileStream)
12-
stream.on('error', (err: any) => {
13-
reject(err)
14-
})
15-
fileStream.on('finish', function () {
16-
resolve()
17-
})
18-
})
12+
return path.join(configDir, 'config.json')
1913
}

0 commit comments

Comments
 (0)