forked from stackblitz/bolt.new
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #589 from wonderwhy-er/Add-command-detection-to-gi…
…t-import-flow Add command detection to git import flow
- Loading branch information
Showing
7 changed files
with
172 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,80 @@ | ||
import type { Message } from 'ai'; | ||
import { generateId } from './fileUtils'; | ||
|
||
export interface ProjectCommands { | ||
type: string; | ||
setupCommand: string; | ||
followupMessage: string; | ||
} | ||
|
||
interface FileContent { | ||
content: string; | ||
path: string; | ||
} | ||
|
||
export async function detectProjectCommands(files: FileContent[]): Promise<ProjectCommands> { | ||
const hasFile = (name: string) => files.some((f) => f.path.endsWith(name)); | ||
|
||
if (hasFile('package.json')) { | ||
const packageJsonFile = files.find((f) => f.path.endsWith('package.json')); | ||
|
||
if (!packageJsonFile) { | ||
return { type: '', setupCommand: '', followupMessage: '' }; | ||
} | ||
|
||
try { | ||
const packageJson = JSON.parse(packageJsonFile.content); | ||
const scripts = packageJson?.scripts || {}; | ||
|
||
// Check for preferred commands in priority order | ||
const preferredCommands = ['dev', 'start', 'preview']; | ||
const availableCommand = preferredCommands.find((cmd) => scripts[cmd]); | ||
|
||
if (availableCommand) { | ||
return { | ||
type: 'Node.js', | ||
setupCommand: `npm install && npm run ${availableCommand}`, | ||
followupMessage: `Found "${availableCommand}" script in package.json. Running "npm run ${availableCommand}" after installation.`, | ||
}; | ||
} | ||
|
||
return { | ||
type: 'Node.js', | ||
setupCommand: 'npm install', | ||
followupMessage: | ||
'Would you like me to inspect package.json to determine the available scripts for running this project?', | ||
}; | ||
} catch (error) { | ||
console.error('Error parsing package.json:', error); | ||
return { type: '', setupCommand: '', followupMessage: '' }; | ||
} | ||
} | ||
|
||
if (hasFile('index.html')) { | ||
return { | ||
type: 'Static', | ||
setupCommand: 'npx --yes serve', | ||
followupMessage: '', | ||
}; | ||
} | ||
|
||
return { type: '', setupCommand: '', followupMessage: '' }; | ||
} | ||
|
||
export function createCommandsMessage(commands: ProjectCommands): Message | null { | ||
if (!commands.setupCommand) { | ||
return null; | ||
} | ||
|
||
return { | ||
role: 'assistant', | ||
content: ` | ||
<boltArtifact id="project-setup" title="Project Setup"> | ||
<boltAction type="shell"> | ||
${commands.setupCommand} | ||
</boltAction> | ||
</boltArtifact>${commands.followupMessage ? `\n\n${commands.followupMessage}` : ''}`, | ||
id: generateId(), | ||
createdAt: new Date(), | ||
}; | ||
} |