5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-06 03:49:41 +08:00
wails/v3/internal/commands/init.go

56 lines
1.3 KiB
Go

package commands
import (
"fmt"
"github.com/wailsapp/wails/v3/internal/flags"
"github.com/wailsapp/wails/v3/internal/templates"
"path/filepath"
"github.com/pterm/pterm"
)
func Init(options *flags.Init) error {
if options.List {
return printTemplates()
}
if options.Quiet {
pterm.DisableOutput()
}
if options.ProjectName == "" {
return fmt.Errorf("please use the -n flag to specify a project name")
}
if !templates.ValidTemplateName(options.TemplateName) {
return fmt.Errorf("invalid template name: %s. Use -l flag to list valid templates", options.TemplateName)
}
err := templates.Install(options)
if err != nil {
return err
}
// Generate build assets
buildAssetsOptions := &BuildAssetsOptions{
Name: options.ProjectName,
Dir: filepath.Join(options.ProjectDir, "build"),
Silent: true,
}
return GenerateBuildAssets(buildAssetsOptions)
}
func printTemplates() error {
defaultTemplates := templates.GetDefaultTemplates()
pterm.DefaultSection.Println("Available templates")
table := pterm.TableData{{"Name", "Description"}}
for _, template := range defaultTemplates {
table = append(table, []string{template.Name, template.Description})
}
err := pterm.DefaultTable.WithHasHeader(true).WithBoxed(true).WithData(table).Render()
pterm.Println()
return err
}