-
Notifications
You must be signed in to change notification settings - Fork 1
/
new.ts
67 lines (60 loc) · 2.36 KB
/
new.ts
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
import Command from '../lib/base'
import {flags} from '@oclif/command'
import {AxiosInstance} from 'axios'
const chalk = require('chalk')
import cli from 'cli-ux'
const PicGo = require('picgo')
const picgo = new PicGo()
export default class New extends Command {
static description = 'post a new giki under your account'
static flags = {
help: flags.help({char: 'h'}),
tag: flags.string({char: 't', description: 'tag[s] of the new giki, you can set more than 1 tags but you must put the -t flag after the text argument, like: `giki new "text to talk" -t <tag1> -t <tag2>`', multiple: true}),
action: flags.string({char: 'a', description: 'action of the new giki', options: ['weibo', 'i']}),
image: flags.string({char: 'i', description: 'image file path to upload, note that image uploading is powered by https://github.com/PicGo/PicGo-Core, please make sure you have the correct configuration.'}),
}
static args = [{name: 'text', required: true, description: 'text to giki'}]
async uploadImage(imagePath: string) {
return new Promise((resolve, reject) => {
picgo.on('finished', (ctx: any) => resolve(ctx.output))
picgo.on('error', (err: any) => {
this.log(chalk.red('failed to upload image'))
reject(err)
})
picgo.upload([imagePath])
})
}
async doCommand(userConfig: Record<string, any>, client: AxiosInstance) {
const {args, flags} = this.parse(New)
const actions = []
if (flags.action) {
actions.push(flags.action)
}
let tags: string[] = []
if (flags.tag && flags.tag.length > 0) {
tags = flags.tag
}
let text = args.text
let image_url = ''
if (flags.image) {
const img_resp: any = await this.uploadImage(flags.image)
// only get first image url
image_url = img_resp[0].imgUrl
// upload image must success to continue
if (!image_url) {
this.log(chalk.red('failed to upload image, please check your picgo config'))
return
}
text = `${text}\n\n![](${image_url})\n`
}
cli.action.start('creating new giki')
await client.post('talks/create', {
text: text,
tags: tags,
actions: actions,
})
cli.action.stop()
// here we believe the request is success, otherwise the cli.action will exit with non-0 value and cannot reaches here
this.log(chalk.green('giki created!'))
}
}