MCPcopy
hub / github.com/prisma/prisma / parse

Method parse

packages/cli/src/postgres/link/Link.ts:158–212  ·  view source on GitHub ↗
(argv: string[], _config: PrismaConfigInternal, baseDir: string)

Source from the content-addressed store, hash-verified

156`)
157
158 public async parse(argv: string[], _config: PrismaConfigInternal, baseDir: string): Promise<string | Error> {
159 const args = arg(argv, {
160 '--api-key': String,
161 '--database': String,
162 '--force': Boolean,
163 '--help': Boolean,
164 '-h': '--help',
165 '--telemetry-information': String,
166 })
167
168 if (isError(args)) {
169 return this.help(args.message)
170 }
171
172 if (args['--help']) {
173 return this.help()
174 }
175
176 if (!args['--force'] && isAlreadyLinked(baseDir)) {
177 return `\nThis project is already linked to Prisma Postgres.\nRun with ${bold('--force')} to re-link.\n`
178 }
179
180 const explicitApiKey = args['--api-key']
181 const databaseId = args['--database']
182 const apiKey = explicitApiKey ?? (databaseId ? process.env.PRISMA_API_KEY : undefined)
183
184 if (explicitApiKey && !databaseId) {
185 return new HelpError(
186 `\n${bold(red('!'))} Missing ${bold('--database')} flag.\n\nWhen using ${bold('--api-key')}, you must also provide ${bold('--database')}.\n${Link.help}`,
187 )
188 }
189
190 if (databaseId && !databaseId.startsWith('db_')) {
191 return new HelpError(
192 `\n${bold(red('!'))} Invalid database ID "${databaseId}" — expected format: ${bold('db_<id>')}\n${Link.help}`,
193 )
194 }
195
196 try {
197 const result = await this.executeLinkFlow(apiKey, databaseId, baseDir)
198 return formatCompletionOutput(result)
199 } catch (err) {
200 if (!apiKey && isExpiredSessionError(err)) {
201 console.log(`Session expired. Re-authenticating via browser...`)
202 await login({ utmMedium: 'command-postgres-link' })
203 try {
204 const result = await this.executeLinkFlow(apiKey, databaseId, baseDir)
205 return formatCompletionOutput(result)
206 } catch (retryErr) {
207 return Link.formatError(retryErr)
208 }
209 }
210 return Link.formatError(err)
211 }
212 }
213
214 async link(
215 apiKey: string | undefined,

Callers

nothing calls this directly

Calls 10

helpMethod · 0.95
executeLinkFlowMethod · 0.95
argFunction · 0.90
isErrorFunction · 0.90
isAlreadyLinkedFunction · 0.90
formatCompletionOutputFunction · 0.90
loginFunction · 0.90
isExpiredSessionErrorFunction · 0.85
logMethod · 0.80
formatErrorMethod · 0.80

Tested by

no test coverage detected