-
Notifications
You must be signed in to change notification settings - Fork 52
feat: offer automatic Prisma ORM 8 CLI installation #2004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
StevenMcClankerton
wants to merge
2
commits into
main
Choose a base branch
from
feat/prisma-orm-8-cli-install
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/vscode/src/plugins/prisma-language-server/installPrismaCli.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { randomUUID } from 'node:crypto' | ||
| import { detect } from 'package-manager-detector/detect' | ||
| import { resolveCommand } from 'package-manager-detector/commands' | ||
| import { commands, tasks, Task, ShellExecution, TaskRevealKind, window, workspace, type WorkspaceFolder } from 'vscode' | ||
|
|
||
| const supportedAgents = ['npm', 'pnpm', 'pnpm@6', 'yarn', 'yarn@berry', 'bun'] as const | ||
|
|
||
| type SupportedAgent = (typeof supportedAgents)[number] | ||
|
|
||
| function isSupportedAgent(agent: string): agent is SupportedAgent { | ||
| return supportedAgents.some((supported) => supported === agent) | ||
| } | ||
|
|
||
| async function runInstallTask(folder: WorkspaceFolder, command: string, args: string[]): Promise<number | undefined> { | ||
| const installId = randomUUID() | ||
| const task = new Task( | ||
| { type: 'prisma-cli-install', installId }, | ||
| folder, | ||
| 'Install Prisma ORM 8 CLI', | ||
| 'Prisma', | ||
| new ShellExecution(command, args, { cwd: folder.uri.fsPath }), | ||
| [], | ||
| ) | ||
| task.presentationOptions = { reveal: TaskRevealKind.Always, focus: true } | ||
|
|
||
| let complete!: (exitCode: number | undefined) => void | ||
| const completion = new Promise<number | undefined>((resolve) => { | ||
| complete = resolve | ||
| }) | ||
| // Subscribe before launching: a short-lived process may exit before executeTask resolves. | ||
| const processEnd = tasks.onDidEndTaskProcess((event) => { | ||
| if (event.execution.task.definition.installId === installId) complete(event.exitCode) | ||
| }) | ||
| const taskEnd = tasks.onDidEndTask((event) => { | ||
| // Covers cancellation or a task that never started a process. A process-end event, when | ||
| // present, precedes this event and has already settled completion with the exit code. | ||
| if (event.execution.task.definition.installId === installId) complete(undefined) | ||
| }) | ||
| try { | ||
| await tasks.executeTask(task) | ||
| return await completion | ||
| } finally { | ||
| processEnd.dispose() | ||
| taskEnd.dispose() | ||
| } | ||
| } | ||
|
|
||
| export async function installPrismaCli(folder: WorkspaceFolder, isDisposed: () => boolean): Promise<void> { | ||
| const canInstall = () => !isDisposed() && workspace.isTrusted && folder.uri.scheme === 'file' | ||
| if (!canInstall()) return | ||
|
|
||
| try { | ||
| const detected = await detect({ | ||
| cwd: folder.uri.fsPath, | ||
| stopDir: folder.uri.fsPath, | ||
| strategies: ['packageManager-field', 'install-metadata', 'lockfile'], | ||
| }) | ||
| if (!canInstall()) return | ||
|
|
||
| let agent = detected?.agent | ||
| if (!agent || !isSupportedAgent(agent)) { | ||
| const selected = await window.showQuickPick( | ||
| [ | ||
| { label: 'npm', agent: 'npm' as const }, | ||
| { label: 'pnpm', agent: 'pnpm' as const }, | ||
| { label: 'Yarn Classic', agent: 'yarn' as const }, | ||
| { label: 'Yarn (2 or later)', agent: 'yarn@berry' as const }, | ||
| { label: 'Bun', agent: 'bun' as const }, | ||
| ], | ||
| { placeHolder: `Choose a package manager to install prisma@latest in "${folder.name}"` }, | ||
| ) | ||
| if (!selected) return | ||
| agent = selected.agent | ||
| } | ||
|
|
||
| const args = ['-D', 'prisma@latest'] | ||
| // Installation targets the open root, including monorepo roots protected by these managers. | ||
| if (agent === 'pnpm' || agent === 'pnpm@6' || agent === 'yarn') { | ||
| args.push('--ignore-workspace-root-check') | ||
| } | ||
| const command = resolveCommand(agent, 'add', args) | ||
| if (!command) throw new Error(`No add command available for ${agent}`) | ||
|
|
||
| if (!canInstall()) return | ||
| const exitCode = await runInstallTask(folder, command.command, command.args) | ||
| if (exitCode !== 0) { | ||
| throw new Error(`Prisma CLI install task did not succeed (exit code: ${exitCode ?? 'unknown'})`) | ||
| } | ||
| } catch (error) { | ||
| console.error('Automatic Prisma ORM 8 CLI installation failed', error) | ||
| if (canInstall()) { | ||
| void window.showErrorMessage( | ||
| `Automatic Prisma ORM 8 CLI installation failed in workspace "${folder.name}". Install "prisma@latest" manually.`, | ||
| ) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if (!canInstall()) return | ||
| try { | ||
| await commands.executeCommand('prisma.restartLanguageServer') | ||
| } catch (error) { | ||
| console.error('Prisma ORM 8 language server restart failed', error) | ||
| if (canInstall()) { | ||
| void window.showErrorMessage( | ||
| `Prisma ORM 8 CLI was installed, but the language server could not restart.`, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.