Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulCatalinas committed Dec 8, 2024
2 parents 07e36cb + bcf5e8b commit fb2bb0e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 37 deletions.
52 changes: 41 additions & 11 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,62 @@ import (
"github.com/RaulCatalinas/HuskyBC/internal/cli"
"github.com/RaulCatalinas/HuskyBC/internal/enums"
"github.com/RaulCatalinas/HuskyBC/internal/options"
"github.com/RaulCatalinas/HuskyBC/internal/types"
"github.com/RaulCatalinas/HuskyBC/internal/utils"
)

func ConfigureOptions() {
options := options.GetOptions()

if len(os.Args) != 2 {
cli.ShowHelp(options)
func buildOptionMap(optionsList []types.Option) map[string]func() {
optionMap := make(map[string]func())

os.Exit(0)
for _, option := range optionsList {
optionMap[option.Name] = option.Handler
optionMap[option.Alias] = option.Handler
}

for _, option := range options {
if os.Args[1] == option.Name || os.Args[1] == option.Alias {
option.Handler()
return optionMap
}

func getOptionHandler(optionName string, availableOptions []types.Option) func() {
optionMap := buildOptionMap(availableOptions)

return
}
if handler, exists := optionMap[optionName]; exists {
return handler
}

return nil
}

func handleInvalidOption(availableOptions []types.Option) {
utils.WriteMessage(utils.WriteMessageProps{
Type: enums.MessageTypeError,
Message: "The option you've tried to execute doesn't exist",
})

fmt.Println()

showHelpAndExit(availableOptions, 1)
}

func showHelpAndExit(options []types.Option, exitCode int) {
cli.ShowHelp(options)

os.Exit(exitCode)
}

func ConfigureOptions() {
availableOptions := options.GetOptions()

if len(os.Args) != 2 {
showHelpAndExit(availableOptions, 0)
}

optionName := os.Args[1]

if handler := getOptionHandler(optionName, availableOptions); handler != nil {
handler()

return
}

handleInvalidOption(availableOptions)
}
57 changes: 45 additions & 12 deletions internal/handlers/handlers_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"os"
"sync"

"github.com/RaulCatalinas/HuskyBC/internal/constants"
"github.com/RaulCatalinas/HuskyBC/internal/enums"
Expand All @@ -28,31 +29,63 @@ func HandlerOptionCollaborate() {
}

func HandlerOptionBuild() {
utils.CreateEmptyJsonFileIfNotExists(constants.PATH_PACKAGE_JSON)

packageManagerToUse := userInput.GetPackageManager()
useCommitlint := userInput.AddCommitlint()
shouldPublishToNpm := userInput.ShouldPublishToNpm()

utils.GenerateHuskyConfig(
utils.Props{
PackageManagerToUse: packageManagerToUse,
PackageJsonPath: constants.PATH_PACKAGE_JSON,
UseCommitlint: useCommitlint,
ShouldPublishToNpm: shouldPublishToNpm,
},
)
utils.CreateEmptyJsonFileIfNotExists(constants.PATH_PACKAGE_JSON)

dependenciesToInstall := []string{"husky"}

if useCommitlint {
utils.GenerateCommitlintConfig(
utils.CommitlintProps{
dependenciesToInstall = append(
dependenciesToInstall,
"lint-staged",
"@commitlint/cli",
"@commitlint/config-conventional",
)
}

utils.InstallDependencies(utils.InstallProps{
PackageManagerToUse: packageManagerToUse,
PackagesToInstall: dependenciesToInstall,
})

var wg sync.WaitGroup

wg.Add(1)

go func() {
defer wg.Done()

utils.GenerateHuskyConfig(
utils.Props{
PackageManagerToUse: packageManagerToUse,
PackageJsonPath: constants.PATH_PACKAGE_JSON,
UseCommitlint: useCommitlint,
ShouldPublishToNpm: shouldPublishToNpm,
},
)
}()

if useCommitlint {
wg.Add(1)

go func() {
defer wg.Done()

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

wg.Wait()

utils.WriteMessage(utils.WriteMessageProps{
Type: enums.MessageTypeSuccess,
Message: "All tasks were completed",
Expand Down
9 changes: 0 additions & 9 deletions internal/utils/commitlint.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ func GenerateCommitlintConfig(commitlintProps CommitlintProps) {
Message: "Configuring commitlint...",
})

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

addScript(addScriptProps{
PackageJsonPath: commitlintProps.PackageJsonPath,
ScriptsToAdd: []packageJsonScript{
Expand Down
5 changes: 0 additions & 5 deletions internal/utils/husky_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ func GenerateHuskyConfig(props Props) {
Message: "Generating Husky's Configuration...",
})

InstallDependencies(InstallProps{
PackageManagerToUse: props.PackageManagerToUse,
PackagesToInstall: []string{"husky"},
})

createHuskyConfigFiles(props.PackageManagerToUse, props.UseCommitlint)

addNecessaryScriptsToPackageJson(
Expand Down

0 comments on commit fb2bb0e

Please sign in to comment.