-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
99 lines (77 loc) · 2.69 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import boxen, { Options } from 'boxen';
import { cli } from 'cli-ux';
import chalk from 'chalk';
import Command, { flags, GetFlagsType } from '../../base';
import { GetIssueQuery } from '../../generated/_documents';
import { render } from '../../components';
import { issueArgs, getIssueId, IssueArgs } from '../../utils/issueId';
dayjs.extend(relativeTime);
type Issue = GetIssueQuery['issue'];
const boxenOptions: Options = { padding: 1, borderStyle: 'round' };
export default class IssueIndex extends Command {
static description = 'Show issue info';
static aliases = ['i'];
static args = issueArgs;
static examples = [
'$ lr issue LIN-14',
'$ lr issue LIN 14',
'$ lr issue 14 (looks in default team)',
];
static flags = {
description: flags.boolean({ char: 'd', description: 'Show issue description' }),
comments: flags.boolean({ char: 'c', description: 'Show issue comments' }),
open: flags.boolean({ char: 'o', description: 'Open issue in web browser' }),
};
renderIssueComments(issue: Issue) {
if (issue.comments?.nodes.length === 0) {
this.log(`Issue ${issue.identifier} does not have any comments`);
}
const dim = chalk.dim;
for (const comment of issue.comments!.nodes.reverse()) {
const author = comment.user.displayName;
const markdown = render
.Markdown(`${comment.body}`)
.replace(/\n\n$/, '')
.padEnd(author.length + 6);
const authorLabel = ` ${comment.user.displayName} `;
let commentBox = boxen(markdown, boxenOptions);
const lengthOfBox = commentBox.match(/╭.*╮/)![0].length;
commentBox = commentBox.replace(
/╭.*╮/,
`╭─${authorLabel.padEnd(lengthOfBox - 4, '─')}─╮`
);
const createdAt = dim(dayjs(comment.createdAt).fromNow());
this.log('');
this.log(`${commentBox}\n ${createdAt}`);
}
}
renderIssueDescription(issue: Issue) {
const markdown = `${issue.identifier}\n # ${issue.title}\n${issue.description ?? ''}`;
this.log('');
this.log(boxen(render.Markdown(markdown), boxenOptions));
}
async run() {
const { flags, args } = this.parse<GetFlagsType<typeof IssueIndex>, IssueArgs>(
IssueIndex
);
const issueId = getIssueId(args);
const issue = await this.linear.query.issue(issueId, {
withComments: flags.comments,
});
if (flags.open) {
cli.open(issue.url);
return;
}
if (flags.comments) {
this.renderIssueComments(issue);
return;
}
if (flags.description) {
this.renderIssueDescription(issue);
return;
}
render.IssueCard(issue);
}
}