feat: add good looking logs

This commit is contained in:
Allyedge
2022-05-31 20:48:17 +02:00
parent d45f3e04e2
commit 34c27bd910
5 changed files with 104 additions and 43 deletions

View File

@@ -1,54 +1,56 @@
package initialize
import (
"fmt"
"errors"
"os"
"os/exec"
"strings"
"github.com/go-git/go-git/v5"
"github.com/gookit/color"
"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
}
func cloneRepository(name string, language string) error {
_, 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.")
color.Error.Prompt("Couldn't install the template.")
return
return err
}
err = os.Rename("templates/templates/"+strings.ToLower(language), name)
if err != nil {
fmt.Println("Couldn't install the template, exiting.")
color.Error.Prompt("Couldn't rename the template to the project's name.")
color.Warn.Prompt("The project was generated, but it wasn't renamed.\n\nYou can still use the project, but you will have to rename it manually.")
return err
}
err = os.RemoveAll("templates")
if err != nil {
return
color.Error.Prompt("Couldn't remove the templates folder.")
return err
}
return nil
}
func renameFolders(name string, main string, commands string) {
func renameFolders(name string, main string, commands string) error {
if main != "src" {
err := os.Rename(name+"/src", name+"/"+main)
if err != nil {
fmt.Println("Couldn't rename the main folder, exiting.")
color.Warn.Prompt("Couldn't rename the main folder.")
return
return err
}
}
@@ -56,20 +58,22 @@ func renameFolders(name string, main string, commands string) {
err := os.Rename(name+"/"+main+"/commands", name+"/"+main+"/"+commands)
if err != nil {
fmt.Println("Couldn't rename the commands folder, exiting.")
color.Warn.Prompt("Couldn't rename the commands folder.")
return
return err
}
}
return nil
}
func installDependencies(name string, packageManager string) {
func installDependencies(name string, packageManager string) error {
err := os.Chdir(name)
if err != nil {
fmt.Println("Couldn't change to the project's directory, exiting.")
color.Error.Prompt("Couldn't change to the project's directory.")
return
return err
}
packageManagers := util.CheckPackageManagers()
@@ -78,35 +82,43 @@ func installDependencies(name string, packageManager string) {
err := exec.Command("npm", "install").Run()
if err != nil {
fmt.Println("Couldn't install the dependencies, exiting.")
color.Error.Prompt("Couldn't install the dependencies.")
return
return err
}
fmt.Println("Successfully installed the dependencies.")
color.Info.Prompt("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.")
color.Error.Prompt("Couldn't install the dependencies.")
return
return err
}
fmt.Println("Successfully installed the dependencies.")
color.Info.Prompt("Successfully installed the dependencies.")
}
if packageManager == "skip" {
fmt.Println("Skipping the installation of the dependencies.")
color.Warn.Prompt("Skipping the installation of the dependencies.")
}
if !packageManagers.NPM && !packageManagers.Yarn {
color.Error.Prompt("Couldn't find any package managers.")
return errors.New("couldn't find any package managers")
}
err = os.Chdir("..")
if err != nil {
fmt.Println("Couldn't change to the project's directory, exiting.")
color.Error.Prompt("Couldn't change to the starting directory.")
return
return err
}
return nil
}

View File

@@ -2,8 +2,10 @@ package initialize
import (
"fmt"
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/gookit/color"
)
func Initialize() {
@@ -21,12 +23,56 @@ func Initialize() {
if err != nil {
fmt.Println("Project initialization failed, exiting.")
return
os.Exit(1)
}
cloneRepository(answers.Name, answers.Language)
color.Info.Prompt("Initializing the project...")
renameFolders(answers.Name, answers.Main, answers.Commands)
err = cloneRepository(answers.Name, answers.Language)
installDependencies(answers.Name, answers.Package)
if err != nil {
color.Error.Prompt("Couldn't generate the project from the templates, exiting.")
err = os.RemoveAll("templates")
if err != nil {
color.Error.Prompt("Couldn't remove the templates folder.")
}
os.Exit(1)
}
err = os.RemoveAll("templates")
if err != nil {
color.Error.Prompt("Couldn't remove the templates folder.")
}
color.Info.Prompt("Successfully generated the project from the templates.")
color.Info.Prompt("Renaming the project's folders...")
err = renameFolders(answers.Name, answers.Main, answers.Commands)
if err != nil {
color.Error.Prompt("Couldn't rename the folders, exiting.")
color.Warn.Prompt("The project was generated, but the folders weren't renamed.\n\nYou can still use the project, but you will have to rename the folders manually.")
os.Exit(1)
}
color.Info.Prompt("Successfully renamed the project's folders.")
color.Info.Prompt("Installing the dependencies...")
err = installDependencies(answers.Name, answers.Package)
if err != nil {
color.Error.Prompt("Couldn't install the dependencies, exiting.")
color.Warn.Prompt("The project was generated, but the dependencies weren't installed.\n\nYou can still use the project, but you will have to install the dependencies manually.")
os.Exit(1)
}
color.Success.Prompt("Project successfully initialized.")
}

View File

@@ -38,12 +38,6 @@ var questions = []*survey.Question{
Default: "!",
},
},
{
Name: "git",
Prompt: &survey.Confirm{
Message: "Do you want to initialize a git repository?",
},
},
{
Name: "package",
Prompt: &survey.Select{