From d45f3e04e24a0eefd0c78345b3b841da416daac1 Mon Sep 17 00:00:00 2001 From: Allyedge Date: Tue, 31 May 2022 19:05:45 +0200 Subject: [PATCH] feat: finish initialize, needs better logs --- pkg/initialize/generate.go | 112 +++++++++++++++++++++++++++++++++++ pkg/initialize/initialize.go | 5 +- pkg/initialize/template.go | 34 ----------- pkg/util/util.go | 29 +++++++++ 4 files changed, 145 insertions(+), 35 deletions(-) create mode 100644 pkg/initialize/generate.go delete mode 100644 pkg/initialize/template.go create mode 100644 pkg/util/util.go diff --git a/pkg/initialize/generate.go b/pkg/initialize/generate.go new file mode 100644 index 0000000..1bea462 --- /dev/null +++ b/pkg/initialize/generate.go @@ -0,0 +1,112 @@ +package initialize + +import ( + "fmt" + "os" + "os/exec" + "strings" + + "github.com/go-git/go-git/v5" + "github.com/sern-handler/cli/pkg/util" +) + +func cloneRepository(name string, language string) { + if _, err := os.Stat(name); os.IsExist(err) { + fmt.Println(name + " already exists, can't initialize a new project.") + + return + } + + _, err := git.PlainClone("templates", false, &git.CloneOptions{ + URL: "https://github.com/sern-handler/templates", + Progress: os.Stdout, + }) + + if err != nil { + fmt.Println("Couldn't install the template, exiting.") + + return + } + + err = os.Rename("templates/templates/"+strings.ToLower(language), name) + + if err != nil { + fmt.Println("Couldn't install the template, exiting.") + } + + err = os.RemoveAll("templates") + + if err != nil { + return + } +} + +func renameFolders(name string, main string, commands string) { + if main != "src" { + err := os.Rename(name+"/src", name+"/"+main) + + if err != nil { + fmt.Println("Couldn't rename the main folder, exiting.") + + return + } + } + + if commands != "commands" { + err := os.Rename(name+"/"+main+"/commands", name+"/"+main+"/"+commands) + + if err != nil { + fmt.Println("Couldn't rename the commands folder, exiting.") + + return + } + } +} + +func installDependencies(name string, packageManager string) { + err := os.Chdir(name) + + if err != nil { + fmt.Println("Couldn't change to the project's directory, exiting.") + + return + } + + packageManagers := util.CheckPackageManagers() + + if packageManager == "npm" && packageManagers.NPM { + err := exec.Command("npm", "install").Run() + + if err != nil { + fmt.Println("Couldn't install the dependencies, exiting.") + + return + } + + fmt.Println("Successfully installed the dependencies.") + } + + if packageManager == "yarn" && packageManagers.Yarn { + err := exec.Command("yarn", "install").Run() + + if err != nil { + fmt.Println("Couldn't install the dependencies, exiting.") + + return + } + + fmt.Println("Successfully installed the dependencies.") + } + + if packageManager == "skip" { + fmt.Println("Skipping the installation of the dependencies.") + } + + err = os.Chdir("..") + + if err != nil { + fmt.Println("Couldn't change to the project's directory, exiting.") + + return + } +} diff --git a/pkg/initialize/initialize.go b/pkg/initialize/initialize.go index 132d122..3122274 100644 --- a/pkg/initialize/initialize.go +++ b/pkg/initialize/initialize.go @@ -13,7 +13,6 @@ func Initialize() { Main string Commands string Prefix string - Git bool Package string }{} @@ -26,4 +25,8 @@ func Initialize() { } cloneRepository(answers.Name, answers.Language) + + renameFolders(answers.Name, answers.Main, answers.Commands) + + installDependencies(answers.Name, answers.Package) } diff --git a/pkg/initialize/template.go b/pkg/initialize/template.go deleted file mode 100644 index c489d1f..0000000 --- a/pkg/initialize/template.go +++ /dev/null @@ -1,34 +0,0 @@ -package initialize - -import ( - "fmt" - "os" - "strings" - - "github.com/go-git/go-git/v5" -) - -func cloneRepository(name string, language string) { - _, err := git.PlainClone("templates", false, &git.CloneOptions{ - URL: "https://github.com/sern-handler/templates", - Progress: os.Stdout, - }) - - if err != nil { - fmt.Println("Couldn't install the template, exiting.") - - return - } - - err = os.Rename("templates/templates/"+strings.ToLower(language), name) - - if err != nil { - fmt.Println("Couldn't install the template, exiting.") - } - - err = os.RemoveAll("templates") - - if err != nil { - return - } -} diff --git a/pkg/util/util.go b/pkg/util/util.go new file mode 100644 index 0000000..f66b4d0 --- /dev/null +++ b/pkg/util/util.go @@ -0,0 +1,29 @@ +package util + +import "os/exec" + +type PackageManagers struct { + NPM bool + Yarn bool +} + +func CheckPackageManagers() PackageManagers { + packageManagers := PackageManagers{ + NPM: false, + Yarn: false, + } + + _, err := exec.LookPath("npm") + + if err == nil { + packageManagers.NPM = true + } + + _, err = exec.LookPath("yarn") + + if err == nil { + packageManagers.Yarn = true + } + + return packageManagers +}