From 92497ed4f12e69b6bd73440935d4c0875d57fb2d Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Tue, 22 Mar 2022 09:19:47 +0000 Subject: [PATCH] [FIX] web: fix tooling failing to run due to bash-specific syntax In #86163 we added a check on the branch name to avoid running the tooling on stable branches, however this check used bash-specific syntax. While the hashbang in the pre-commit hook specifies that the hook should be run using bash, we were using the npm module "husky" to manage git hooks, which would ignore this hashbang and always run the hook using sh, causing the hook to fail in all cases. After some consideration, we have decided to stop using husky, as its main purpose is to make hook management easier in npm-based projects. Since we already need a script to enable the tooling, we can do essentially the same thing that husky is doing but with more control over the process with no drawbacks. closes odoo/odoo#87436 X-original-commit: 3104ab16ecf88d4f7b1618db4d62e7f57df34e6c Signed-off-by: Simon Genin (ges@odoo) --- .gitignore | 1 - addons/web/tooling/_husky/.gitignore | 1 - addons/web/tooling/_package.json | 2 - addons/web/tooling/disable.sh | 33 ++++++------- addons/web/tooling/enable.sh | 47 +++++++++---------- .../web/tooling/{_husky => hooks}/pre-commit | 2 - addons/web/tooling/reload.sh | 8 ++-- 7 files changed, 40 insertions(+), 54 deletions(-) delete mode 100644 addons/web/tooling/_husky/.gitignore rename addons/web/tooling/{_husky => hooks}/pre-commit (85%) diff --git a/.gitignore b/.gitignore index acb779112d7..021964d773a 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,6 @@ setup/win32/static/postgresql*.exe node_modules package-lock.json package.json -.husky # various virtualenv /bin/ diff --git a/addons/web/tooling/_husky/.gitignore b/addons/web/tooling/_husky/.gitignore deleted file mode 100644 index 31354ec1389..00000000000 --- a/addons/web/tooling/_husky/.gitignore +++ /dev/null @@ -1 +0,0 @@ -_ diff --git a/addons/web/tooling/_package.json b/addons/web/tooling/_package.json index 4ab01819c5b..477c834cf8b 100644 --- a/addons/web/tooling/_package.json +++ b/addons/web/tooling/_package.json @@ -3,7 +3,6 @@ "version": "0.0.1", "description": "JS Config for better DX in javascript", "scripts": { - "prepare": "husky install", "format-web": "prettier-eslint --write 'addons/web/static/src/**/*.js' 'addons/web/static/tests/**/*.js' 'addons/web/doc/**/*.md'", "format-staged": "lint-staged", "lint-web": "prettier-eslint --list-different 'addons/web/static/src/**/*.js' 'addons/web/static/tests/**/*.js' 'addons/web/doc/**/*.md'", @@ -13,7 +12,6 @@ "eslint": "^7.25.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^3.4.0", - "husky": "^6.0.0", "lint-staged": "^10.5.4", "prettier": "2.2.1", "prettier-eslint-cli": "^5.0.1" diff --git a/addons/web/tooling/disable.sh b/addons/web/tooling/disable.sh index 1d98954864a..e62f2c3b42b 100755 --- a/addons/web/tooling/disable.sh +++ b/addons/web/tooling/disable.sh @@ -1,7 +1,18 @@ #!/bin/bash +community=$(cd -- "$(dirname "$0")" &> /dev/null && cd ../../.. && pwd) -script="$0" -basename="$(dirname "$script")" +disableInDir () { + cd "$1" + git config --unset core.hooksPath + rm .eslintignore + rm .prettierignore + rm .eslintrc.json + rm .prettierrc.json + rm package.json + rm package-lock.json + rm -r node_modules + cd - &> /dev/null +} read -p "Do you want to delete the tooling installed in enterprise too ? [y, n]" willingToDeleteToolingInEnterprise if [[ $willingToDeleteToolingInEnterprise != "n" ]] @@ -10,25 +21,11 @@ then pathToEnterprise=${pathToEnterprise:-../enterprise} fi -rm -rf "$basename/../../../.husky" -rm -rf "$basename/../../../.eslintignore" -rm -rf "$basename/../../../.prettierignore" -rm -rf "$basename/../../../.eslintrc.json" -rm -rf "$basename/../../../.prettierrc.json" -rm -rf "$basename/../../../package.json" -rm -rf "$basename/../../../package-lock.json" -rm -rf "$basename/../../../node_modules" +disableInDir "$community" if [[ $willingToDeleteToolingInEnterprise != "n" ]] then - rm -rf "$basename/../../../$pathToEnterprise/.husky" - rm -rf "$basename/../../../$pathToEnterprise/.eslintignore" - rm -rf "$basename/../../../$pathToEnterprise/.prettierignore" - rm -rf "$basename/../../../$pathToEnterprise/.eslintrc.json" - rm -rf "$basename/../../../$pathToEnterprise/.prettierrc.json" - rm -rf "$basename/../../../$pathToEnterprise/package.json" - rm -rf "$basename/../../../$pathToEnterprise/package-lock.json" - rm -rf "$basename/../../../$pathToEnterprise/node_modules" + disableInDir "$pathToEnterprise" fi diff --git a/addons/web/tooling/enable.sh b/addons/web/tooling/enable.sh index 6fce8780887..b20f0a33c2b 100755 --- a/addons/web/tooling/enable.sh +++ b/addons/web/tooling/enable.sh @@ -1,7 +1,25 @@ #!/bin/bash +community=$(cd -- "$(dirname "$0")" &> /dev/null && cd ../../.. && pwd) +tooling="$community/addons/web/tooling" -script="$0" -basename="$(dirname "$script")" +enableInDir () { + cd $1 + hooksPath="$(realpath --relative-to=. "$tooling/hooks")" + git config core.hooksPath "$hooksPath" + cp "$tooling/_eslintignore" .eslintignore + cp "$tooling/_prettierignore" .prettierignore + cp "$tooling/_eslintrc.json" .eslintrc.json + cp "$tooling/_prettierrc.json" .prettierrc.json + cp "$tooling/_package.json" package.json + if [[ $2 == "copy" ]]; then + # copy over node_modules and package-lock to avoid double "npm install" + cp "$community/package-lock.json" package-lock.json + cp -r "$community/node_modules" node_modules + else + npm install + fi + cd - &> /dev/null +} read -p "Do you want the tooling installed in enterprise too ? [y, n]" willingToInstallToolingInEnterprise if [[ $willingToInstallToolingInEnterprise != "n" ]] @@ -10,32 +28,11 @@ then pathToEnterprise=${pathToEnterprise:-../enterprise} fi -cp -r "$basename/_husky" "$basename/../../../.husky" -cp "$basename/_eslintignore" "$basename/../../../.eslintignore" -cp "$basename/_prettierignore" "$basename/../../../.prettierignore" -cp "$basename/_eslintrc.json" "$basename/../../../.eslintrc.json" -cp "$basename/_prettierrc.json" "$basename/../../../.prettierrc.json" -cp "$basename/_package.json" "$basename/../../../package.json" +enableInDir "$community" if [[ $willingToInstallToolingInEnterprise != "n" ]] then - cp -r "$basename/_husky" "$basename/../../../$pathToEnterprise/.husky" - cp "$basename/_eslintignore" "$basename/../../../$pathToEnterprise/.eslintignore" - cp "$basename/_prettierignore" "$basename/../../../$pathToEnterprise/.prettierignore" - cp "$basename/_eslintrc.json" "$basename/../../../$pathToEnterprise/.eslintrc.json" - cp "$basename/_prettierrc.json" "$basename/../../../$pathToEnterprise/.prettierrc.json" - cp "$basename/_package.json" "$basename/../../../$pathToEnterprise/package.json" -fi - -cd "$basename" -npm install -cd - - -if [[ $willingToInstallToolingInEnterprise != "n" ]] -then - cd "$basename/../../../$pathToEnterprise" - npm install - cd - + enableInDir "$pathToEnterprise" copy fi echo "" diff --git a/addons/web/tooling/_husky/pre-commit b/addons/web/tooling/hooks/pre-commit similarity index 85% rename from addons/web/tooling/_husky/pre-commit rename to addons/web/tooling/hooks/pre-commit index 74847d86c64..b6f58d2cbde 100755 --- a/addons/web/tooling/_husky/pre-commit +++ b/addons/web/tooling/hooks/pre-commit @@ -1,6 +1,4 @@ #!/bin/bash -. "$(dirname "$0")/_/husky.sh" - # run tooling only on branches that start with master to avoid linting noise in stable if [[ $(git branch --show-current) == master* ]]; then npm run format-staged diff --git a/addons/web/tooling/reload.sh b/addons/web/tooling/reload.sh index 0081880568b..fe7c1815488 100755 --- a/addons/web/tooling/reload.sh +++ b/addons/web/tooling/reload.sh @@ -1,7 +1,5 @@ #!/bin/bash +community=$(cd -- "$(dirname "$0")" &> /dev/null && cd ../../.. && pwd) -script="$0" -basename="$(dirname "$script")" - -"$basename/disable.sh" -"$basename/enable.sh" +"$community/addons/web/tooling/disable.sh" +"$community/addons/web/tooling/enable.sh"