diff --git a/go.mod b/go.mod index e067dce..04b7b23 100644 --- a/go.mod +++ b/go.mod @@ -15,21 +15,23 @@ require ( github.com/emirpasic/gods v1.12.0 // indirect github.com/go-git/gcfg v1.5.0 // indirect github.com/go-git/go-billy/v5 v5.3.1 // indirect + github.com/gookit/color v1.5.0 // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect github.com/mattn/go-colorable v0.1.2 // indirect - github.com/mattn/go-isatty v0.0.8 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/sergi/go-diff v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/xanzy/ssh-agent v0.3.0 // indirect + github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect golang.org/x/net v0.0.0-20210326060303-6b1517762897 // indirect - golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect golang.org/x/text v0.3.3 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect diff --git a/go.sum b/go.sum index b39f8e3..748a38c 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,8 @@ github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti4ihgckDc= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/gookit/color v1.5.0 h1:1Opow3+BWDwqor78DcJkJCIwnkviFi+rrOANki9BUFw= +github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= @@ -61,8 +63,9 @@ github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= @@ -90,6 +93,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= @@ -105,8 +110,10 @@ golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 h1:RX8C8PRZc2hTIod4ds8ij+/4RQX3AqhYj3uOHmyaz4E= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w= golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= diff --git a/pkg/initialize/generate.go b/pkg/initialize/generate.go index 1bea462..aad2f95 100644 --- a/pkg/initialize/generate.go +++ b/pkg/initialize/generate.go @@ -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 } diff --git a/pkg/initialize/initialize.go b/pkg/initialize/initialize.go index 3122274..e96bfb8 100644 --- a/pkg/initialize/initialize.go +++ b/pkg/initialize/initialize.go @@ -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.") } diff --git a/pkg/initialize/questions.go b/pkg/initialize/questions.go index 55be767..8a35227 100644 --- a/pkg/initialize/questions.go +++ b/pkg/initialize/questions.go @@ -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{