This repository has been archived by the owner on May 3, 2022. It is now read-only.
/
code.ts
111 lines (103 loc) · 2.83 KB
/
code.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
100
101
102
103
104
105
106
107
108
109
110
111
import { flags } from '@oclif/command';
import * as chalk from 'chalk';
import { ApiResponse } from '../api';
import Command, {
buildFlags,
FormatOptions,
TableResult,
} from '../base-command';
export default class Code extends Command {
static description =
'search github code. https://developer.github.com/v3/search/#search-code';
static examples = [
`$ ghs code --extension js "import _ from 'lodash'"
`,
];
static flags = buildFlags(
{
in: flags.string({
description:
'Qualifies which fields are searched. With this qualifier you can restrict the search to the file contents (file), the file path (path), or both.',
}),
language: flags.string({
char: 'l',
description: "Searches code based on the language it's written in.",
}),
size: flags.string({
description: 'Finds files that match a certain size (in bytes).',
}),
path: flags.string({
description:
'Specifies the path prefix that the resulting file must be under.',
}),
filename: flags.string({
description: 'Matches files by a substring of the filename.',
}),
extension: flags.string({
description: 'Matches files with a certain extension after a dot.',
}),
user: flags.string({
char: 'u',
description:
'Limits searches to a specific user. Use @me for your username',
}),
repo: flags.string({
char: 'r',
description: 'Limits searches to a specific repository.',
}),
org: flags.string({
char: 'o',
description: 'Limits searchs to a specific organization',
}),
text: flags.boolean({
char: 't',
description: 'Show full text match',
}),
sort: flags.enum({
char: 's',
options: ['indexed'],
description:
'The sort field. Can only be indexed, which indicates how recently a file has been indexed by the GitHub search infrastructure. Default: results are sorted by best match.',
}),
},
['sort', 'text'],
);
static args = [...Command.args];
format(data: ApiResponse, opts: FormatOptions): TableResult {
const rows = data.items.reduce((acc, item) => {
const repo = chalk.cyan(item.repository.name);
const fullPath: string = item.html_url;
const [blobSegment] = fullPath.match(
/blob\/[0-9a-f]{40}\//,
) as RegExpMatchArray;
let shortenedPath = fullPath;
if (blobSegment) {
shortenedPath = fullPath.replace(
blobSegment,
`${blobSegment.substring(0, 12)}/`,
);
}
const url = shortenedPath;
acc.push({
repo,
url,
text: opts.text
? chalk.green(
item.text_matches
.map((textMatch: any) => textMatch.fragment)
.join('/n'),
)
: undefined,
});
return acc;
}, []);
return {
rows,
columns: {
...(opts.text ? { text: { header: 'code' } } : { repo: {} }),
url: {},
},
options: opts.text ? { 'no-truncate': true } : undefined,
};
}
}