diff --git a/Taskfile.yml b/Taskfile.yml index 4730dfa..6ae7927 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -11,7 +11,7 @@ tasks: test: desc: Test project cmds: - - go test -timeout 600s -cover ./... + - go test -timeout 60s -cover ./... security: desc: Run gosec for project diff --git a/pkg/cgapp/actions.go b/pkg/cgapp/actions.go index 3793e15..675a6bb 100644 --- a/pkg/cgapp/actions.go +++ b/pkg/cgapp/actions.go @@ -106,7 +106,7 @@ func CreateCLIAction(c *cli.Context) error { stopTimer := time.Since(startTimer).String() // END message - SendMsg(true, "FINISH", "Completed in "+stopTimer+".", "green", false) + SendMsg(true, "FINISH", "Completed in "+stopTimer+"!", "green", false) SendMsg(true, "DOCS", "A helpful documentation here → https://create-go.app", "yellow", false) SendMsg(false, "!", "Go to the `"+appPath+"` folder and make something beautiful! :)", "yellow", true) diff --git a/pkg/cgapp/create.go b/pkg/cgapp/create.go index 84d0db1..29e6708 100644 --- a/pkg/cgapp/create.go +++ b/pkg/cgapp/create.go @@ -20,7 +20,7 @@ func CreateProjectFromRegistry(project *Project, registry map[string]*Registry) // Checking for nil if project == nil || registry == nil { - return ThrowError("Registry not found!") + return ThrowError("Project template or registry not found!") } // Create path in project root folder @@ -71,6 +71,12 @@ func CreateProjectFromRegistry(project *Project, registry map[string]*Registry) SendMsg(false, "OK", strings.Title(project.Type)+": created with user template `"+project.Name+"`!", "", false) } + // Cleanup project + foldersToRemove := []string{".git", ".github"} + if err := RemoveFolders(folder, foldersToRemove); err != nil { + return ThrowError(err.Error()) + } + return nil } @@ -79,15 +85,18 @@ func CreateProjectFromCMD(p *Project, cmd map[string]*Command) error { // Define vars var options []string + // Checking for nil + if p == nil || cmd == nil { + return ThrowError("Project template or commands not found!") + } + // Create path in project root folder folder := filepath.Join(p.RootFolder, p.Type) // Split framework name and template - project := StringSplit(":", p.Name) - - // Error, when empty - if len(project) == 0 { - return ThrowError("Frontend template not set!") + project, err := StringSplit(":", p.Name) + if err != nil { + return ThrowError(err.Error()) } // Re-define vars for more beauty view @@ -106,19 +115,32 @@ func CreateProjectFromCMD(p *Project, cmd map[string]*Command) error { break case "preact": // preact create [template] [dest] [args...] - options = []string{create, folder} + options = []string{create, "default", p.Type, args["cwd"], p.RootFolder, args["name"], "cgapp"} if len(project) > 1 { options = []string{create, project[1], p.Type, args["cwd"], p.RootFolder, args["name"], "cgapp"} } break + case "svelte": + // npx degit [template] [dest] + options = []string{create, args["template"], folder} + break default: return ThrowError("Frontend template" + p.Name + " not found!") } - // + // Run execution command if err := ExecCommand(runner, options); err != nil { return ThrowError(err.Error()) } + // Cleanup project + folderToRemove := []string{".git"} + if err := RemoveFolders(folder, folderToRemove); err != nil { + return ThrowError(err.Error()) + } + + // Show success report + SendMsg(false, "OK", "Frontend: created with template `"+p.Name+"`!", "", false) + return nil } diff --git a/pkg/cgapp/create_test.go b/pkg/cgapp/create_test.go index 5b34941..ae93996 100644 --- a/pkg/cgapp/create_test.go +++ b/pkg/cgapp/create_test.go @@ -75,6 +75,18 @@ func TestCreateProjectFromRegistry(t *testing.T) { }, false, }, + { + "failed wrong template name", + args{ + project: &Project{ + Type: "backend", + Name: "...wrong...", + RootFolder: "../../tmp", + }, + registry: registry, + }, + true, + }, { "failed create (empty Project struct)", args{ @@ -114,3 +126,65 @@ func TestCreateProjectFromRegistry(t *testing.T) { } } } + +func TestCreateProjectFromCMD(t *testing.T) { + type args struct { + p *Project + cmd map[string]*Command + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + "successfully create svelte", + args{ + p: &Project{ + Type: "frontend", + Name: "svelte", + RootFolder: "../../tmp", + }, + cmd: cmds, + }, + false, + }, + { + "failed create (empty Project struct)", + args{ + p: nil, + cmd: cmds, + }, + true, + }, + { + "failed create (empty Command struct)", + args{ + p: &Project{ + Type: "frontend", + Name: "svelte", + RootFolder: "../../tmp", + }, + cmd: nil, + }, + true, + }, + { + "failed create (empty Project and Command struct)", + args{}, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateProjectFromCMD(tt.args.p, tt.args.cmd); (err != nil) != tt.wantErr { + t.Errorf("CreateProjectFromCMD() error = %v, wantErr %v", err, tt.wantErr) + } + }) + + // Clean + if tt.args.p != nil { + os.RemoveAll(tt.args.p.RootFolder) + } + } +} diff --git a/pkg/cgapp/defaults.go b/pkg/cgapp/defaults.go index 15ba9ac..f954672 100644 --- a/pkg/cgapp/defaults.go +++ b/pkg/cgapp/defaults.go @@ -67,10 +67,15 @@ var ( Runner: "preact", Create: "create", Args: map[string]string{ - "cwd": "--cwd", - "name": "--name", - "skip-git": "--git", - "skip-install": "--install", + "cwd": "--cwd", + "name": "--name", + }, + }, + "svelte": { + Runner: "npx", + Create: "degit", + Args: map[string]string{ + "template": "sveltejs/template", }, }, } diff --git a/pkg/cgapp/utils.go b/pkg/cgapp/utils.go index b4450a1..595a1e7 100644 --- a/pkg/cgapp/utils.go +++ b/pkg/cgapp/utils.go @@ -112,7 +112,12 @@ func ExecCommand(command string, options []string) error { } // StringSplit ... -func StringSplit(pattern, match string) []string { +func StringSplit(pattern, match string) ([]string, error) { + // Error, when empty or nil + if pattern == "" || match == "" { + return nil, ThrowError("Frontend template not set!") + } + // Define empty []string{} for splitted strings splittedStrings := []string{} @@ -126,7 +131,7 @@ func StringSplit(pattern, match string) []string { splittedStrings = append(splittedStrings, split[str]) } - return splittedStrings + return splittedStrings, nil } // MakeFiles function for massively create folders @@ -185,11 +190,5 @@ func GitClone(rootFolder, templateName string) error { return ThrowError("Repository was not cloned!") } - // Cleanup project - foldersToRemove := []string{".git", ".github"} - if err := RemoveFolders(rootFolder, foldersToRemove); err != nil { - return ThrowError(err.Error()) - } - return nil } diff --git a/pkg/cgapp/utils_test.go b/pkg/cgapp/utils_test.go index 3aa4d05..0ed792f 100644 --- a/pkg/cgapp/utils_test.go +++ b/pkg/cgapp/utils_test.go @@ -296,9 +296,10 @@ func TestStringSplit(t *testing.T) { match string } tests := []struct { - name string - args args - want []string + name string + args args + want []string + wantErr bool }{ { "successfully matched", @@ -307,19 +308,32 @@ func TestStringSplit(t *testing.T) { match: "react:redux", }, []string{"react", "redux"}, + false, }, { - "failed match", + "successfully not matched", args{ pattern: "=", match: "react:redux", }, []string{"react:redux"}, + false, + }, + { + "failed wrong pattern and match", + args{}, + nil, + true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := StringSplit(tt.args.pattern, tt.args.match); !reflect.DeepEqual(got, tt.want) { + got, err := StringSplit(tt.args.pattern, tt.args.match) + if (err != nil) != tt.wantErr { + t.Errorf("StringSplit() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { t.Errorf("StringSplit() = %v, want %v", got, tt.want) } })