-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathanalytics-command.ts
More file actions
112 lines (98 loc) · 2.94 KB
/
analytics-command.ts
File metadata and controls
112 lines (98 loc) · 2.94 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
112
import meowOrDie from 'meow'
import colors from 'yoctocolors-cjs'
import { runAnalytics } from './run-analytics.ts'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError, InputError } from '../../utils/errors'
import { getFlagListOutput } from '../../utils/output-formatting'
import { getDefaultToken } from '../../utils/sdk.ts'
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
import type { CommandContext } from '../types.ts'
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 analyticsCommand = {
description: config.description,
hidden: config.hidden,
run: run
}
async function run(
argv: readonly string[],
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
const cli = meowOrDie(config.help(parentName, config), {
argv,
description: config.description,
importMeta,
flags: config.flags
})
const { repo, scope, time } = cli.flags
if (scope !== 'org' && scope !== 'repo') {
throw new InputError("The scope must either be 'org' or 'repo'")
}
if (time !== 7 && time !== 30 && time !== 90) {
throw new InputError('The time filter must either be 7, 30 or 90')
}
if (scope === 'repo' && !repo) {
console.error(
`${colors.bgRed(colors.white('Input error'))}: Please provide a repository name when using the repository scope.`
)
cli.showHelp()
}
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 runAnalytics(apiToken, {
scope,
time,
repo,
outputJson: cli.flags['json'],
file: cli.flags['file']
} as CommandContext)
}