MCPcopy Create free account
hub / github.com/Melkeydev/go-blueprint / CreateViteReactProject

Method CreateViteReactProject

cmd/program/program.go:811–933  ·  view source on GitHub ↗
(projectPath string)

Source from the content-addressed store, hash-verified

809}
810
811func (p *Project) CreateViteReactProject(projectPath string) error {
812 if err := checkNpmInstalled(); err != nil {
813 return err
814 }
815
816 originalDir, err := os.Getwd()
817 if err != nil {
818 return fmt.Errorf("failed to get current directory: %w", err)
819 }
820 defer func() {
821 if err := os.Chdir(originalDir); err != nil {
822 fmt.Fprintf(os.Stderr, "failed to change back to original directory: %v\n", err)
823 }
824 }()
825
826 // change into the project directory to run vite command
827 err = os.Chdir(projectPath)
828 if err != nil {
829 fmt.Println("failed to change into project directory: %w", err)
830 }
831
832 // the interactive vite command will not work as we can't interact with it
833 fmt.Println("Installing create-vite (using cache if available)...")
834 cmd := exec.Command("npm", "create", "vite@latest", "frontend", "--",
835 "--template", "react-ts",
836 "--prefer-offline",
837 "--no-fund")
838 cmd.Stdout = os.Stdout
839 cmd.Stderr = os.Stderr
840 if err := cmd.Run(); err != nil {
841 return fmt.Errorf("failed to use create-vite: %w", err)
842 }
843
844 frontendPath := filepath.Join(projectPath, "frontend")
845 if err := os.MkdirAll(frontendPath, 0755); err != nil {
846 return fmt.Errorf("failed to create frontend directory: %w", err)
847 }
848
849 if err := os.Chdir(frontendPath); err != nil {
850 return fmt.Errorf("failed to change to frontend directory: %w", err)
851 }
852
853 srcDir := filepath.Join(frontendPath, "src")
854 if err := os.MkdirAll(srcDir, 0755); err != nil {
855 return fmt.Errorf("failed to create src directory: %w", err)
856 }
857
858 if err := os.WriteFile(filepath.Join(srcDir, "App.tsx"), advanced.ReactAppfile(), 0644); err != nil {
859 return fmt.Errorf("failed to write App.tsx template: %w", err)
860 }
861
862 // Create the global `.env` file from the template
863 err = p.CreateFileWithInjection("", projectPath, ".env", "env")
864 if err != nil {
865 return fmt.Errorf("failed to create global .env file: %w", err)
866 }
867
868 // Read from the global `.env` file and create the frontend-specific `.env`

Callers 1

CreateMainFileMethod · 0.95

Calls 6

ReactAppfileFunction · 0.92
ViteTailwindConfigFileFunction · 0.92
InputCssTemplateReactFunction · 0.92
ReactTailwindAppfileFunction · 0.92
checkNpmInstalledFunction · 0.85

Tested by

no test coverage detected