-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcmd-analytics.ts
More file actions
111 lines (98 loc) · 3.1 KB
/
cmd-analytics.ts
File metadata and controls
111 lines (98 loc) · 3.1 KB
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 colors from 'yoctocolors-cjs'
import { displayAnalytics } from './display-analytics.ts'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
import { getDefaultToken } from '../../utils/sdk.ts'
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
const config: CliCommandConfig = {
commandName: 'analytics',
description: `Look up analytics data`,
hidden: false,
flags: {
...commonFlags,
...outputFlags,
scope: {
type: 'string',
shortFlag: 's',
default: 'org',
description:
"Scope of the analytics data - either 'org' or 'repo', default: org"
},
time: {
type: 'number',
shortFlag: 't',
default: 7,
description: 'Time filter - either 7, 30 or 90, default: 7'
},
repo: {
type: 'string',
shortFlag: 'r',
default: '',
description: 'Name of the repository'
},
file: {
type: 'string',
shortFlag: 'f',
default: '',
description: 'Path to a local file to save the output'
}
},
help: (parentName, { commandName, flags }) => `
Usage
$ ${parentName} ${commandName} --scope=<scope> --time=<time filter>
Default parameters are set to show the organization-level analytics over the
last 7 days.
Options
${getFlagListOutput(flags, 6)}
Examples
$ ${parentName} ${commandName} --scope=org --time=7
$ ${parentName} ${commandName} --scope=org --time=30
$ ${parentName} ${commandName} --scope=repo --repo=test-repo --time=30
`
}
export const cmdAnalytics = {
description: config.description,
hidden: config.hidden,
run: run
}
async function run(
argv: readonly string[],
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
const cli = meowOrExit({
argv,
config,
importMeta,
parentName
})
const { repo, scope, time } = cli.flags
const badScope = scope !== 'org' && scope !== 'repo'
const badTime = time !== 7 && time !== 30 && time !== 90
const badRepo = scope === 'repo' && !repo
if (badScope || badTime || badRepo) {
console.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
- Scope must be "repo" or "org" ${badScope ? colors.red('(bad!)') : colors.green('(ok)')}\n
- The time filter must either be 7, 30 or 90 ${badTime ? colors.red('(bad!)') : colors.green('(ok)')}\n
- Repository name using --repo when scope is "repo" ${badRepo ? colors.red('(bad!)') : colors.green('(ok)')}\n
`)
cli.showHelp()
return
}
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API token.'
)
}
return await displayAnalytics({
apiToken,
scope,
time,
repo: String(repo || ''),
outputJson: Boolean(cli.flags['json']),
filePath: String(cli.flags['file'] || '')
})
}