Skip to content

Commit

Permalink
enhance: improve error messages for better user and developer clarity
Browse files Browse the repository at this point in the history
- Enhanced error messages to be more descriptive for users.
- Improved error messages to aid in easier debugging.
  • Loading branch information
RaulCatalinas committed Jul 22, 2024
1 parent 463bf29 commit 6dbbc7b
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 77 deletions.
43 changes: 43 additions & 0 deletions internal/constants/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package constants

var ERROR_MESSAGES = map[string]string{
"NotFound": "The {fileName} file wasn't found in the current directory.",

"Default": "Something went wrong, please try again later, if the error persists please report it on " + ISSUES + ".",

"Dependencies": "An error occurred while installing dependencies, please try again later, if the error persists please report it on " + ISSUES + ".",

"Husky": "An error has occurred during the Husky configuration process, please try again later, if the error persists please report it on " + ISSUES + ".",

"CommitLintConfig": "An error has occurred during the commitlint configuration process, please try again later, if the error persists please report it on " + ISSUES + ".",

"AddCommitLint": "An error occurred while asking if you wanna add commitlint, please try again later, if the error persists please report it on " + ISSUES + ".",

"PackageManagerSelection": "An error occurred while selecting the package manager, please try again later, if the error persists, please report it on " + ISSUES + ".",

"CreateEmptyFile": "An error occurred while creating the empty file. Please try again later, if the error persists please report it on " + ISSUES + ".",

"CreateFolder": "An error occurred while creating the folder: {folderName}, please try again later, if the error persists, please report it on " + ISSUES + ".",

"ReadFile": "An error has occurred while reading the {fileName} file, please try again later, if the error persists, please report it on " + ISSUES + ".",

"WriteFile": "An error has occurred while writing to the {fileName} file, please try again later, if the error persists, please report it on " + ISSUES + ".",

"ConfigFilesCreate": "An error occurred while creating configuration files. Please try again later, if the error persists please report it on " + ISSUES + ".",

"PublishConfirmation": "An error occurred while confirming npm publication. Please try again later, if the error persists, please report it on" + ISSUES + ".",

"NpmIgnoreWrite": "An error occurred while writing to the '.npmignore' file. Please try again later, if the error persists please report it on " + ISSUES + ".",

"GitHubRepoOpen": "An error occurred while opening the GitHub repository in a new browser tab. Please try again later, if the error persists please report it on" + ISSUES + ".",

"JsonUnmarshal": "Replace this with an error message that's descriptive and easily understandable by any developer.",

"JsonMarshal": "Replace this with an error message that's descriptive and easily understandable by any developer.",

"GetWorkingDirectory": "Replace this with an error message that's descriptive and easily understandable by any developer.",

"InvalidTypeForFilesToAdd": "Replace this with an error message that's descriptive and easily understandable by any developer.",

"CheckingFolderOrFile": "Replace this with an error message that's descriptive and easily understandable by any developer.",
}
22 changes: 17 additions & 5 deletions internal/handlers/handlers_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ import (
)

func HandlerOptionCollaborate() {
utils.WriteMessage("info", "Opening the GitHub repository...")
utils.WriteMessage(utils.WriteMessageProps{
Type: "info",
Message: "Opening the GitHub repository...",
})

time.Sleep(5 * time.Millisecond)

err := webbrowser.Open(constants.REPOSITORY)

if err != nil {
utils.WriteMessage("error", utils.GetErrorMessage("Open webbrowser"))
utils.WriteMessage(utils.WriteMessageProps{
Type: "error",
Message: utils.GetErrorMessage("GitHubRepoOpen"),
})

os.Exit(1)
}
}
Expand All @@ -36,16 +43,21 @@ func HandlerOptionBuild() {
PackageJsonPath: constants.PATH_PACKAGE_JSON,
UseCommitlint: useCommitlint,
ShouldPublishToNpm: shouldPublishToNpm,
})
},
)

if useCommitlint {
utils.GenerateCommitlintConfig(
utils.CommitlintProps{
PackageManagerToUse: packageManagerToUse,
PackageJsonPath: constants.PATH_PACKAGE_JSON,
ShouldPublishToNpm: shouldPublishToNpm,
})
},
)
}

utils.WriteMessage("success", "All tasks were completed")
utils.WriteMessage(utils.WriteMessageProps{
Type: "success",
Message: "All tasks were completed",
})
}
5 changes: 4 additions & 1 deletion internal/user-input/add-commitlint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func AddCommitlint() bool {
err := survey.Ask(questions, &answers)

if err != nil {
utils.WriteMessage("error", utils.GetErrorMessage("HuskyConfigCleanOrder"))
utils.WriteMessage(utils.WriteMessageProps{
Type: "error",
Message: utils.GetErrorMessage("AddCommitLint"),
})

os.Exit(1)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/user-input/package-managers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func GetPackageManager() types.PackageManager {
err := survey.Ask(questions, &answers)

if err != nil {
utils.WriteMessage("error", utils.GetErrorMessage("PackageManagerSelection"))
utils.WriteMessage(utils.WriteMessageProps{
Type: "error",
Message: utils.GetErrorMessage("PackageManagerSelection"),
})

os.Exit(1)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/user-input/publishToNpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func ShouldPublishToNpm() bool {
err := survey.Ask(questions, &answers)

if err != nil {
utils.WriteMessage("error", utils.GetErrorMessage("ShouldPublishToNpm"))
utils.WriteMessage(utils.WriteMessageProps{
Type: "error",
Message: utils.GetErrorMessage("ShouldPublishToNpm"),
})

os.Exit(1)
}
Expand Down
71 changes: 52 additions & 19 deletions internal/utils/commitlint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,99 @@ type CommitlintProps struct {
func GenerateCommitlintConfig(commitlintProps CommitlintProps) {
defer func() {
if r := recover(); r != nil {
WriteMessage("error", GetErrorMessage("Commitlint"))
WriteMessage(WriteMessageProps{
Type: "error",
Message: GetErrorMessage("Commitlint"),
})

os.Exit(1)
}
}()

WriteMessage("config", "Configuring commitlint...")

InstallDependencies(InstallProps{PackageManagerToUse: commitlintProps.PackageManagerToUse, PackagesToInstall: []string{
"lint-staged",
"@commitlint/cli",
"@commitlint/config-conventional",
}})

addScript(addScriptProps{PackageJsonPath: commitlintProps.PackageJsonPath, ScriptsToAdd: []packageJsonScript{
{Key: "lint", Value: "eslint src"},
{Key: "lint:fix", Value: "eslint src --fix"},
{Key: "format", Value: "prettier src --check"},
{Key: "format:write", Value: "prettier src --write"},
}})
WriteMessage(WriteMessageProps{
Type: "config",
Message: "Configuring commitlint...",
})

InstallDependencies(InstallProps{
PackageManagerToUse: commitlintProps.PackageManagerToUse,
PackagesToInstall: []string{
"lint-staged",
"@commitlint/cli",
"@commitlint/config-conventional",
},
})

addScript(addScriptProps{
PackageJsonPath: commitlintProps.PackageJsonPath,
ScriptsToAdd: []packageJsonScript{
{Key: "lint", Value: "eslint src"},
{Key: "lint:fix", Value: "eslint src --fix"},
{Key: "format", Value: "prettier src --check"},
{Key: "format:write", Value: "prettier src --write"},
},
})

createCommitlintConfigFiles(commitlintProps.PackageManagerToUse)

if commitlintProps.ShouldPublishToNpm {
modifyNpmIgnore([]string{".lintstagedrc", "commitlint.config.js"})
}

WriteMessage("success", "commitlint's configuration generated successfully")
WriteMessage(WriteMessageProps{
Type: "success",
Message: "commitlint's configuration generated successfully",
})
}

func createCommitlintConfigFiles(packageManagerToUse types.PackageManager) {
WriteMessage("info", "Creating configuration files...")
WriteMessage(WriteMessageProps{
Type: "info",
Message: "Creating configuration files...",
})

var wg sync.WaitGroup

wg.Add(1)

go func() {
defer wg.Done()
writeFile(".husky/commit-msg", []byte(constants.COMMITLINT_CONFIG[string(packageManagerToUse)]))

writeFile(
".husky/commit-msg",
[]byte(constants.COMMITLINT_CONFIG[string(packageManagerToUse)]),
)
}()

wg.Add(1)

go func() {
defer wg.Done()

content := "export default { extends: ['@commitlint/config-conventional'] }"

writeFile("commitlint.config.js", []byte(content))
}()

wg.Add(1)

go func() {
defer wg.Done()

content := `{
"src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}": [
"prettier src --check",
"eslint src --max-warnings 0"
]
}`

writeFile(".lintstagedrc", []byte(content))
}()

wg.Wait()

WriteMessage("success", "Configuration files (commit-msg, commitlint.config.js and .lintstagedrc) created successfully")
WriteMessage(WriteMessageProps{
Type: "success",
Message: "Configuration files (commit-msg, commitlint.config.js and .lintstagedrc) created successfully",
})
}
28 changes: 28 additions & 0 deletions internal/utils/console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"fmt"

"github.com/RaulCatalinas/HuskyBC/internal/types"
)

type WriteMessageProps struct {
Type types.MessageType
Message string
}

func WriteMessage(props WriteMessageProps) {
switch props.Type {
case "success":
fmt.Println("\033[32m" + props.Message + "\033[0m")

case "info":
fmt.Println("\033[36m" + props.Message + "\033[0m")

case "error":
fmt.Println("\033[31m" + props.Message + "\033[0m")

case "config":
fmt.Println("\033[37m" + props.Message + "\033[0m")
}
}
34 changes: 24 additions & 10 deletions internal/utils/dependencies.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"fmt"
"os"
"os/exec"

Expand All @@ -26,18 +25,21 @@ func promiseSpawn(command string, args []string) error {
func InstallDependencies(props InstallProps) {
defer func() {
if r := recover(); r != nil {
WriteMessage("error", GetErrorMessage("Dependencies"))
WriteMessage(WriteMessageProps{
Type: "error",
Message: GetErrorMessage("Dependencies"),
})

os.Exit(1)
}
}()

installationCommand, exists := constants.INSTALLATION_COMMANDS[types.PackageManager(props.PackageManagerToUse)]
if !exists {
WriteMessage("error", "Invalid package manager")
os.Exit(1)
}
message := "Installing dependencies using: " + props.PackageManagerToUse + "..."

WriteMessage("info", fmt.Sprintf("Installing dependencies using: %s...", props.PackageManagerToUse))
WriteMessage(WriteMessageProps{
Type: "info",
Message: string(message),
})

/* FIXME: Problemas al ejecutan bun, dice esto:
error: Module not found "...\HuskyBC\node_modules\husky\bin.mjs"
Expand All @@ -48,12 +50,24 @@ func InstallDependencies(props InstallProps) {
hice una prueba haciendo "bun add husky" directamente y es el mismo caso, lo que quiere decir que HuskyBC no presenta problemas
el problema viene desde el mismo bun
*/

installationCommand := constants.INSTALLATION_COMMANDS[props.PackageManagerToUse]

args := append([]string{installationCommand}, append(props.PackagesToInstall, "-D")...)

err := promiseSpawn(string(props.PackageManagerToUse), args)

if err != nil {
WriteMessage("error", GetErrorMessage(err.Error()))
WriteMessage(WriteMessageProps{
Type: "error",
Message: GetErrorMessage("Dependencies"),
})

os.Exit(1)
}

WriteMessage("success", "Dependencies installed successfully")
WriteMessage(WriteMessageProps{
Type: "success",
Message: "Dependencies installed successfully",
})
}
7 changes: 7 additions & 0 deletions internal/utils/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils

import "github.com/RaulCatalinas/HuskyBC/internal/constants"

func GetErrorMessage(error string) string {
return constants.ERROR_MESSAGES[error]
}
Loading

0 comments on commit 6dbbc7b

Please sign in to comment.