2017-06-07 2 views
0

こんにちはみんな私はgit repoその家3のフォルダ(個々のjsプロジェクト)を持っています。私はプリコミットフックにlintingを追加するにはどうすればいいですか?

ここで私のpre-commitフック

#!/bin/bash 
echo -e "\033[1;92m Linting Staged Files . . . " 

files=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(js|jsx)$') 
path=(${files[0]//// }) 


if [[ $files = "" ]] ; then 
    echo -e "\033[1;31m You have no staged file, exiting . . " 
    exit 1 
fi 

for file in $files 
do 
git show ":$file" | $("./$path/node_modules/.bin/eslint --stdin --stdin-filename $file") 
if [ $? -ne 0 ]; then 
    echo -e "\033[1;31m ESLint failed on staged file '$file'. \n Code will not be committed, Please fix your code and try again." 
exit 1 
fi 
done 

BRANCH=`git rev-parse --abbrev-ref HEAD` 

if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then 
echo "\033[1;31m commiting to $BRANCH is illegal." 
echo "\033[1;31m If you really know what you're doing, then add the argument '-n' to skip this git hook." 
exit 1 
fi 

exit 0 

だが、それは私が間違ってやっている見当がつかないエラー

.git/hooks/pre-commit: line 17: ./node_modules/.bin/eslint --stdin --stdin-filename sfa/src/SFAApp.js: No such file or directory

で、このラインでgit show ":$file" | $("./$path/node_modules/.bin/eslint --stdin --stdin-filename $file")

を失敗してください保持します助けて。

答えて

0

ネヴァーマインドは、私が

$("./$path/node_modules/.bin/eslint --stdin --stdin-filename $file")

を変更することで問題を修正

./$path/node_modules/.bin/eslint --stdin --stdin-filename $file

TODO:$()は本当にbashで何を意味するのか調査。あなたは文字通りという名前のコマンドを実行しようとしている

0

./node_modules/.bin/eslint --stdin --stdin-filename sfa/src/SFAApp.js 

という名前のコマンドを実行しようとしているより:引数を持つ

./node_modules/.bin/eslint 

--stdin 

とを:

--stdin-filename 

と:

sfa/src/SFAApp.js 

治療法は、あなたの二重引用符の配置を修正することです。あなたが完了したら、おそらく第二の問題があります:

git show ":$file" | $(./$path/node_modules/.bin/eslint --stdin --stdin-filename "$file") 

は、引数を指定してコマンドを実行しますが、それは$(...)内部にあるので、その後、コマンドとしてその出力がかかります。私はeslintの何も知らないが、それは、これは読むべきと思わ:

git show ":$file" | ./$path/node_modules/.bin/eslint --stdin --stdin-filename "$file" 

(私は全くも$pathと愚かさが何であるかわからないんだけど、最初のファイルpath-にpathを設定path=(${files[0]//// })ラインが。名前出力がa/b/c.jsであれば、git diff、例えばによって記載されているファイルのコンポーネント、pathaに設定されている。)

+0

私はGitのレポを持って言ったように、それらの異なるプロジェクトと実際のプロジェクトが、その他のフォルダを保持していません、したがって、私は変更されたファイルのフォルダ名を取得して、そのフォルダのconfigsと共に**その**フォルダのeslintを実行しなければなりません。 –

+0

Aha。まあ、 'a/dir1/file1.js' *と*' b/dir2/file2.js'に変更や新しいファイルがあるとうまくいかないでしょう。ファイルごとに正しいパス*を選ぶ必要があります(または複数のサブプロジェクトに影響を与えるコミットを禁止するかもしれません)。 – torek

+0

おそらく最初のファイルを取得するのではなく、ファイルごとにフォルダパスを取得する必要があります。どうもありがとう。 –

関連する問題