From f97044447fe357e1e64d272e26841c405213f24c Mon Sep 17 00:00:00 2001 From: Ujstor Date: Mon, 16 Dec 2024 20:17:41 +0100 Subject: [PATCH 1/2] fix cache --- cmd/program/program.go | 28 +++++++++++++++++++++------ docs/docs/advanced-flag/react-vite.md | 21 +++++++++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/cmd/program/program.go b/cmd/program/program.go index 95a735fb..e85fac32 100644 --- a/cmd/program/program.go +++ b/cmd/program/program.go @@ -828,9 +828,19 @@ func (p *Project) CreateViteReactProject(projectPath string) error { fmt.Println("failed to change into project directory: %w", err) } + // Configure npm to prefer offline and use cache + configCmd := exec.Command("npm", "config", "set", "prefer-offline", "true") + if err := configCmd.Run(); err != nil { + fmt.Println("Warning: Failed to set npm offline preference:", err) + } + // the interactive vite command will not work as we can't interact with it - fmt.Println("Installing create-vite...") - cmd := exec.Command("npm", "create", "vite@latest", "frontend", "--", "--template", "react-ts") + fmt.Println("Running create-vite (using cache if available)...") + cmd := exec.Command("npm", "create", "vite@latest", "frontend", "--", + "--template", "react-ts", + "--prefer-offline", + "--no-audit", + "--no-fund") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { @@ -881,16 +891,23 @@ func (p *Project) CreateViteReactProject(projectPath string) error { if err := os.WriteFile(filepath.Join(frontendPath, ".env"), []byte(frontendEnvContent), 0644); err != nil { return fmt.Errorf("failed to create frontend .env file: %w", err) } + // Handle Tailwind configuration if selected if p.AdvancedOptions[string(flags.Tailwind)] { - fmt.Println("Tailwind selected. Configuring with React...") - cmd := exec.Command("npm", "install", "-D", "tailwindcss", "postcss", "autoprefixer") + fmt.Println("Installing Tailwind dependencies (using cache if available)...") + cmd := exec.Command("npm", "install", "-D", + "--prefer-offline", + "--no-audit", + "--no-fund", + "tailwindcss", "postcss", "autoprefixer") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { return fmt.Errorf("failed to install Tailwind: %w", err) } - cmd = exec.Command("npx", "tailwindcss", "init", "-p") + + fmt.Println("Initializing Tailwind...") + cmd = exec.Command("npx", "--prefer-offline", "tailwindcss", "init", "-p") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { @@ -930,7 +947,6 @@ func (p *Project) CreateViteReactProject(projectPath string) error { return nil } - func (p *Project) CreateHtmxTemplates() { routesPlaceHolder := "" importsPlaceHolder := "" diff --git a/docs/docs/advanced-flag/react-vite.md b/docs/docs/advanced-flag/react-vite.md index e1f59485..01de8afe 100644 --- a/docs/docs/advanced-flag/react-vite.md +++ b/docs/docs/advanced-flag/react-vite.md @@ -213,4 +213,23 @@ networks: ## Environment Variables -The `VITE_PORT` in .env refers `PORT` from .env in project root ( for backend ). If value of `PORT` is changed than `VITE_PORT` must also be changed so that requests to backend work fine and have no conflicts. \ No newline at end of file +The `VITE_PORT` in .env refers `PORT` from .env in project root ( for backend ). If value of `PORT` is changed than `VITE_PORT` must also be changed so that requests to backend work fine and have no conflicts. + +## Notes + +- First time running the project creation with Tailwind can take longer (~10 mins) as npm needs to download and cache all packages + +- Subsequent runs will be faster as they utilize npm's cache, which we enforce during project creation. + +- If encountering issues with package installation, try these npm commands: + +```bash +# Check cache status +npm cache verify + +# View cache contents +npm cache ls + +# Clean cache if needed +npm cache clean --force +``` From adac587453fce98ad979f3be1e1d2eeec80b902b Mon Sep 17 00:00:00 2001 From: Ujstor Date: Mon, 16 Dec 2024 20:39:33 +0100 Subject: [PATCH 2/2] makefile update, remove dev flags --- cmd/program/program.go | 12 ++---------- cmd/template/framework/files/makefile.tmpl | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/cmd/program/program.go b/cmd/program/program.go index e85fac32..b5a8465b 100644 --- a/cmd/program/program.go +++ b/cmd/program/program.go @@ -828,18 +828,11 @@ func (p *Project) CreateViteReactProject(projectPath string) error { fmt.Println("failed to change into project directory: %w", err) } - // Configure npm to prefer offline and use cache - configCmd := exec.Command("npm", "config", "set", "prefer-offline", "true") - if err := configCmd.Run(); err != nil { - fmt.Println("Warning: Failed to set npm offline preference:", err) - } - // the interactive vite command will not work as we can't interact with it - fmt.Println("Running create-vite (using cache if available)...") + fmt.Println("Installing create-vite (using cache if available)...") cmd := exec.Command("npm", "create", "vite@latest", "frontend", "--", "--template", "react-ts", "--prefer-offline", - "--no-audit", "--no-fund") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr @@ -895,9 +888,8 @@ func (p *Project) CreateViteReactProject(projectPath string) error { // Handle Tailwind configuration if selected if p.AdvancedOptions[string(flags.Tailwind)] { fmt.Println("Installing Tailwind dependencies (using cache if available)...") - cmd := exec.Command("npm", "install", "-D", + cmd := exec.Command("npm", "install", "--prefer-offline", - "--no-audit", "--no-fund", "tailwindcss", "postcss", "autoprefixer") cmd.Stdout = os.Stdout diff --git a/cmd/template/framework/files/makefile.tmpl b/cmd/template/framework/files/makefile.tmpl index 16205704..7e10b254 100644 --- a/cmd/template/framework/files/makefile.tmpl +++ b/cmd/template/framework/files/makefile.tmpl @@ -56,7 +56,7 @@ build:{{- if and .AdvancedOptions.tailwind (not .AdvancedOptions.react) }} tailw # Run the application run: @go run cmd/api/main.go{{- if .AdvancedOptions.react }} & - @npm install --prefix ./frontend + @npm install --prefer-offline --no-fund --prefix ./frontend @npm run dev --prefix ./frontend {{- end }}