2017-06-07 45 views
0

嗨,大家好我有一個git倉庫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 show ":$file" | $("./$path/node_modules/.bin/eslint --stdin --stdin-filename $file")

一個錯誤

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

我不知道我做錯了什麼故障,請幫幫我。

回答

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=(${files[0]//// })設置path第一個文件路徑 - 行。通過git diff列出的文件,例如名字組成部分,如果輸出爲a/b/c.jspath設置爲a。)

+0

就像我說的混帳回購協議具有不成立一個實際的項目,但與他們不同的項目其他文件夾,因此我必須獲取修改文件的文件夾名稱並執行** that **文件夾的eslint,以及該文件夾的配置。 –

+0

啊哈。那麼,如果'a/dir1/file1.js' *和*'b/dir2/file2.js'中有更改或新文件,那麼這不起作用。你需要爲每個文件選擇正確的路徑*(或者可能禁止影響多個子項目的提交)。 – torek

+0

我可能需要獲取每個文件的文件夾路徑,而不是僅獲取第一個文件。非常感謝。 –