Skip to content

Commit

Permalink
adapt for npm
Browse files Browse the repository at this point in the history
  • Loading branch information
A.A.Abroskin committed Feb 15, 2019
1 parent b91f38a commit 340d6bc
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 28 deletions.
18 changes: 12 additions & 6 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
10 changes: 0 additions & 10 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
26 changes: 20 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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())

Expand Down Expand Up @@ -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)
}
}
11 changes: 9 additions & 2 deletions spec/fixtures/pre-commit
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
#!/bin/sh
exec hookah run pre-commit
#!/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
11 changes: 9 additions & 2 deletions spec/fixtures/pre-push
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
#!/bin/sh
exec hookah run pre-push
#!/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
4 changes: 2 additions & 2 deletions spec/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 340d6bc

Please sign in to comment.