From 6925a90175a3198b666345cf3cbe3f64ffef56df Mon Sep 17 00:00:00 2001 From: Raul Catalinas <105791463+RaulCatalinas@users.noreply.github.com> Date: Sun, 8 Dec 2024 13:19:43 +0100 Subject: [PATCH 1/2] perf: optimize HandlerOptionBuild by generating Husky and Commitlint configs in parallel - Improved performance by executing Husky and Commitlint configuration generation concurrently. --- internal/handlers/handlers_options.go | 57 +++++++++++++++++++++------ internal/utils/commitlint.go | 9 ----- internal/utils/husky_library.go | 5 --- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/internal/handlers/handlers_options.go b/internal/handlers/handlers_options.go index 0dcfe20..5afb69e 100644 --- a/internal/handlers/handlers_options.go +++ b/internal/handlers/handlers_options.go @@ -2,6 +2,7 @@ package handlers import ( "os" + "sync" "github.com/RaulCatalinas/HuskyBC/internal/constants" "github.com/RaulCatalinas/HuskyBC/internal/enums" @@ -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", diff --git a/internal/utils/commitlint.go b/internal/utils/commitlint.go index ae8e4b5..56aaaac 100644 --- a/internal/utils/commitlint.go +++ b/internal/utils/commitlint.go @@ -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{ diff --git a/internal/utils/husky_library.go b/internal/utils/husky_library.go index 9592fbf..72e9f81 100644 --- a/internal/utils/husky_library.go +++ b/internal/utils/husky_library.go @@ -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( From bcf5e8bbb17f54e900c5012183b5cc8b11e8edab Mon Sep 17 00:00:00 2001 From: Raul Catalinas <105791463+RaulCatalinas@users.noreply.github.com> Date: Sun, 8 Dec 2024 13:20:40 +0100 Subject: [PATCH 2/2] perf: Optimised "ConfigureOptions" function --- internal/config/options.go | 52 ++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/internal/config/options.go b/internal/config/options.go index 4c4ce99..ed4e787 100644 --- a/internal/config/options.go +++ b/internal/config/options.go @@ -7,26 +7,32 @@ 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", @@ -34,5 +40,29 @@ func ConfigureOptions() { 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) }