|
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' |
4 | 2 |
|
5 | | -import config from '../config' |
6 | | -import {getConfigFilePath} from '../utils' |
| 3 | +import HackMDCommand from '../command' |
7 | 4 |
|
8 | | -export default class Login extends Command { |
| 5 | +export default class Login extends HackMDCommand { |
9 | 6 | static description = 'Login to HackMD server from CLI' |
10 | 7 |
|
11 | 8 | static examples = [ |
12 | 9 | `$ hackmd-cli login |
13 | 10 |
|
14 | | -Enter your email: MY_ACCESS_TOKEN |
| 11 | +Enter your access token: MY_ACCESS_TOKEN |
15 | 12 |
|
16 | | -Login successfully! |
| 13 | +Login successfully |
17 | 14 | ` |
18 | 15 | ] |
19 | 16 |
|
20 | 17 | static flags = { |
21 | | - help: Flags.help({char: 'h'}), |
22 | | - accessToken: Flags.string({char: 'u', description: 'Login with accesstoken'}), |
| 18 | + help: Flags.help({char: 'h'}) |
23 | 19 | } |
24 | 20 |
|
25 | 21 | 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 | 22 | 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') |
45 | 25 | } catch (err) { |
46 | | - this.log('Login failed, please ensure your credentials are correct') |
| 26 | + this.log('Login failed') |
47 | 27 | this.error(err) |
48 | 28 | } |
49 | 29 | } |
|
0 commit comments