-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
183 lines (155 loc) · 4.67 KB
/
cli.ts
File metadata and controls
183 lines (155 loc) · 4.67 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import path from 'node:path'
import * as p from '@clack/prompts'
import pc from 'picocolors'
import type { Provider } from './analyze.js'
import { analyze } from './analyze.js'
import type { VoiceTemplate } from './constants.js'
import { VOICE_TEMPLATES } from './constants.js'
import { getTopCategories, printReport } from './report.js'
import { scanDirectory } from './scan.js'
import { openScoreCard, uploadScoreCard } from './scoreCard.js'
function getRepoName(dir: string): string {
return path.basename(dir)
}
function maskKey(key: string): string {
if (key.length <= 8) return '\u2022'.repeat(key.length)
return key.slice(0, 4) + '\u2022\u2022\u2022\u2022' + key.slice(-4)
}
function getEnvKey(provider: Provider): string | undefined {
if (provider === 'anthropic') return process.env.ANTHROPIC_API_KEY
return process.env.OPENAI_API_KEY
}
async function main() {
console.clear()
p.intro(pc.inverse(' Brandlint \u2014 Brand Voice Scanner '))
// 1. Choose voice template
const voice = await p.select({
message: 'Choose your brand voice',
options: Object.entries(VOICE_TEMPLATES).map(([key, val]) => ({
value: key as VoiceTemplate,
label: val.name,
hint: val.description,
})),
})
if (p.isCancel(voice)) {
p.cancel('Cancelled.')
process.exit(0)
}
// 2. Choose AI provider
const provider = await p.select({
message: 'AI provider',
options: [
{ value: 'anthropic' as Provider, label: 'Anthropic', hint: 'Claude' },
{ value: 'openai' as Provider, label: 'OpenAI', hint: 'GPT-4o' },
],
})
if (p.isCancel(provider)) {
p.cancel('Cancelled.')
process.exit(0)
}
// 3. Get API key (env var or prompt)
const envKey = getEnvKey(provider)
let apiKey: string
if (envKey) {
p.log.info(
`Using ${provider === 'anthropic' ? 'ANTHROPIC_API_KEY' : 'OPENAI_API_KEY'} ${pc.dim(maskKey(envKey))}`
)
apiKey = envKey
} else {
const prompted = await p.password({
message: `${provider === 'anthropic' ? 'Anthropic' : 'OpenAI'} API key`,
validate: (val) => {
if (!val || val.length < 10) return 'Please enter a valid API key'
},
})
if (p.isCancel(prompted)) {
p.cancel('Cancelled.')
process.exit(0)
}
apiKey = prompted
}
// 4. Scan
const dir = process.cwd()
const s = p.spinner()
s.start('Scanning for user-facing strings...')
const { strings, totalFiles, totalStrings } = await scanDirectory(dir)
if (strings.length === 0) {
s.stop('Scan complete')
p.log.warn('No user-facing strings found in this directory.')
p.outro(
`Want brand voice checks on every PR? ${pc.underline('https://brandlint.com/signin')}`
)
process.exit(0)
}
const capNote =
totalStrings > strings.length
? ` (analyzing ${strings.length} of ${totalStrings})`
: ''
s.stop(
`Found ${pc.bold(String(totalFiles))} files, ${pc.bold(String(totalStrings))} user-facing strings${capNote}`
)
// 5. Analyze
s.start(
`Analyzing against ${pc.bold(`"${VOICE_TEMPLATES[voice].name}"`)} voice...`
)
let suggestions
try {
suggestions = await analyze(strings, voice, provider, apiKey)
} catch (err: any) {
s.stop('Analysis failed')
const msg = err?.message || String(err)
if (
msg.includes('401') ||
msg.includes('invalid') ||
msg.includes('Unauthorized')
) {
p.log.error(
'Invalid API key. Please check your key and try again.'
)
} else {
p.log.error(`Analysis error: ${msg}`)
}
process.exit(1)
}
s.stop('Analysis complete')
console.log()
// 6. Report
const score = printReport(strings, suggestions, totalStrings)
// 7. Share score card
const repoName = getRepoName(dir)
const topCategories = getTopCategories(suggestions)
const shouldShare = await p.confirm({
message: 'Share your brand score?',
initialValue: true,
})
if (p.isCancel(shouldShare)) {
p.outro(
`Want this on every PR? ${pc.underline(`https://brandlint.com/signin?voice=${voice}`)}`
)
process.exit(0)
}
if (shouldShare) {
s.start('Creating score card...')
const result = await uploadScoreCard({
repoName,
score,
stringsAnalyzed: strings.length,
issuesFound: suggestions.length,
topCategories,
voiceTemplate: voice,
})
if (result) {
s.stop(`Score card: ${pc.underline(result.url)}`)
await openScoreCard(result.url)
} else {
s.stop('Could not create score card (are you online?)')
}
}
p.outro(
`Want this on every PR? ${pc.underline(`https://brandlint.com/signin?voice=${voice}`)}`
)
}
main().catch((err) => {
p.log.error(err.message || String(err))
process.exit(1)
})