diff --git a/cmd/add.go b/cmd/add.go index 951c56b3..ef10f893 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -20,15 +20,15 @@ hookah add pre-commit This command will try to build the foolowing structure in repository: ├───.git -│ └───hooks -│ └───pre-commit // this executable will be added. Existed file with -│ // same name will be renamed to pre-commit.old +│ └───hooks +│ └───pre-commit // this executable will be added. Existed file with +│ // same name will be renamed to pre-commit.old ... │ ├───.hookah // directory for project level hooks -│ └───pre-commit // directory with hooks executables +│ └───pre-commit // directory with hooks executables ├───.hookah-local // directory for personal hooks add it in .gitignore -│ └───pre-commit +│ └───pre-commit `, Run: func(cmd *cobra.Command, args []string) { addCmdExecutor(args, appFs) @@ -46,7 +46,13 @@ func addCmdExecutor(args []string, fs afero.Fs) { } func addHook(hookName string, fs afero.Fs) { - template := "#!/bin/sh\nexec hookah run " + hookName + // TODO: text/template + template := `#!/bin/bash +# If can't find hookah in global scope +# we suppose it in local npm package +if ! type hookah 2>/dev/null +then + exec npx hookah run ` + hookName + "\nelse\n exec hookah run " + hookName + "\nfi" pathToFile := filepath.Join(getGitHooksDir(), hookName) diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index e231c4e7..1948a687 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -89,16 +89,6 @@ func TestAddCmdExecutor(t *testing.T) { assert.Equal(t, expectedDirs, actualDirs, "Haven`t renamed file with .old extension") } -// TODO: little tricky to call exec.Command with virtual file system -// func TestRunCmdExecutor(t *testing.T) { -// fs := afero.NewMemMapFs() -// presetConfig(fs) -// presetExecutable("fail_script", "pre-commit", "1", fs) - -// err := RunCmdExecutor([]string{"pre-commit"}, fs) -// assert.Error(t, err) -// } - func presetConfig(fs afero.Fs) { viper.SetDefault(configSourceDirKey, ".hookah") diff --git a/cmd/run.go b/cmd/run.go index c93b2b6d..efe6d91c 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -57,7 +57,11 @@ func RunCmdExecutor(args []string, fs afero.Fs) error { executables, err := afero.ReadDir(fs, sourcePath) check(err) - log.Println(aurora.Bold("Running " + getHooksGroup() + " hooks")) + if len(executables) == 0 { + log.Println(aurora.Cyan("RUNNING HOOKS GROUP:"), aurora.Bold(getHooksGroup()), aurora.Brown("(SKIP EMPTY)")) + } else { + log.Println(aurora.Cyan("RUNNING HOOKS GROUP:"), aurora.Bold(getHooksGroup())) + } for _, executable := range executables { execute(sourcePath, executable) @@ -66,7 +70,11 @@ func RunCmdExecutor(args []string, fs afero.Fs) error { sourcePath = filepath.Join(getLocalSourceDir(), getHooksGroup()) executables, err = afero.ReadDir(fs, sourcePath) if err == nil { - log.Println(aurora.Bold("Running local " + getHooksGroup() + " hooks")) + if len(executables) == 0 { + log.Println(aurora.Cyan("RUNNING LOCAL HOOKS GROUP:"), aurora.Bold(getHooksGroup()), aurora.Brown("(SKIP EMPTY)")) + } else { + log.Println(aurora.Cyan("RUNNING LOCAL HOOKS GROUP:"), aurora.Bold(getHooksGroup())) + } for _, executable := range executables { execute(sourcePath, executable) @@ -84,7 +92,7 @@ func RunCmdExecutor(args []string, fs afero.Fs) error { func execute(source string, executable os.FileInfo) { setExecutableName(executable.Name()) - log.Println(aurora.Bold("Execute " + getExecutableName())) + log.Println(aurora.Cyan(" EXECUTE >"), aurora.Bold(getExecutableName())) pathToExecutable := filepath.Join(source, getExecutableName()) @@ -146,11 +154,17 @@ func getExecutableName() string { } func printSummary() { - for _, fileName := range failList { - log.Printf("[ %s ] %s\n", aurora.Red("FAIL"), fileName) + if len(okList) == 0 && len(failList) == 0 { + log.Println(aurora.Cyan("\nSUMMARY:"), aurora.Brown("(SKIP EMPTY)")) + } else { + log.Println(aurora.Cyan("\nSUMMARY:")) } for _, fileName := range okList { - log.Printf("[ %s ] %s\n", aurora.Green("OK"), fileName) + log.Printf("[ %s ] %s\n", aurora.Green("OK"), fileName) + } + + for _, fileName := range failList { + log.Printf("[ %s ] %s\n", aurora.Red("FAIL"), fileName) } } diff --git a/spec/fixtures/pre-commit b/spec/fixtures/pre-commit index 15a0f423..981b9a13 100644 --- a/spec/fixtures/pre-commit +++ b/spec/fixtures/pre-commit @@ -1,2 +1,9 @@ -#!/bin/sh -exec hookah run pre-commit \ No newline at end of file +#!/bin/bash +# If can't find hookah in global scope +# we suppose it in local npm package +if ! type hookah 2>/dev/null +then + exec npx hookah run pre-commit +else + exec hookah run pre-commit +fi \ No newline at end of file diff --git a/spec/fixtures/pre-push b/spec/fixtures/pre-push index e678fa75..236a4396 100644 --- a/spec/fixtures/pre-push +++ b/spec/fixtures/pre-push @@ -1,2 +1,9 @@ -#!/bin/sh -exec hookah run pre-push \ No newline at end of file +#!/bin/bash +# If can't find hookah in global scope +# we suppose it in local npm package +if ! type hookah 2>/dev/null +then + exec npx hookah run pre-push +else + exec hookah run pre-push +fi \ No newline at end of file diff --git a/spec/run_spec.rb b/spec/run_spec.rb index 0ff9f7c4..5a7ba6cb 100644 --- a/spec/run_spec.rb +++ b/spec/run_spec.rb @@ -13,7 +13,7 @@ describe 'fail chain' do let(:command) { 'hookah run pre-commit' } let(:expected_output) do - "[ \e[31mFAIL\e[0m ] fail_script\n[ \e[32mOK\e[0m ] ok_script\n" + "\n[ \e[32mOK\e[0m ] ok_script\n[ \e[31mFAIL\e[0m ] fail_script\n" end it 'exit with 1 status' do @@ -27,7 +27,7 @@ describe 'ok chain' do let(:command) { 'hookah run pre-push' } - let(:expected_output) { "[ \e[32mOK\e[0m ] ok_script\n" } + let(:expected_output) { "[ \e[32mOK\e[0m ] ok_script\n" } it 'exit with 0 status' do expect(@status.success?).to be_truthy