This is an automated email from the ASF dual-hosted git repository.

kamilbregula pushed a commit to branch aip-11
in repository https://gitbox.apache.org/repos/asf/airflow-site.git


The following commit(s) were added to refs/heads/aip-11 by this push:
     new 1838e4c  Add support for running a linters for a single file (#111)
1838e4c is described below

commit 1838e4c34d1023f98e960c036f7e15bcefa8ef96
Author: Kamil BreguĊ‚a <[email protected]>
AuthorDate: Mon Nov 4 17:49:29 2019 +0100

    Add support for running a linters for a single file (#111)
---
 .pre-commit-config.yaml |  3 +--
 site.sh                 | 67 +++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 2a69477..1da206a 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -104,13 +104,12 @@ repos:
         entry: ./site.sh lint-css
         language: system
         files: \.scss$
-        pass_filenames: false
       - id: lint-js
         name: Lint JS files
         entry: ./site.sh lint-js
         language: system
         files: \.js$
-        pass_filenames: false
+        exclude: "webpack.common.js|webpack.dev.js|create-index.js"
   -   repo: https://github.com/pre-commit/pre-commit-hooks
       rev: v2.3.0
       hooks:
diff --git a/site.sh b/site.sh
index 98e5916..b838ecb 100755
--- a/site.sh
+++ b/site.sh
@@ -19,6 +19,7 @@
 
 set -euox pipefail
 
+WORKING_DIR="$(pwd)"
 MY_DIR="$(cd "$(dirname "$0")" && pwd)"
 pushd "${MY_DIR}" &>/dev/null || exit 1
 
@@ -51,6 +52,9 @@ can execute the following command:
 The following command can also be performed from the Docker environment:
 install-node-deps, preview, build-site, lint-css, lint-js.
 
+The lint-css and lint-js accept paths in arguments. If no path is given, the 
script
+will be executed for all supported files
+
 EOF
 }
 
@@ -126,6 +130,57 @@ function prevent_docker {
     fi
 }
 
+function relativepath {
+    source=$1
+    target=$2
+
+    common_part=$source # for now
+    result="" # for now
+
+    while [[ "${target#$common_part}" == "${target}" ]]; do
+        # no match, means that candidate common part is not correct
+        # go up one level (reduce common part)
+        common_part="$(dirname "$common_part")"
+        # and record that we went back, with correct / handling
+        if [[ -z $result ]]; then
+            result=".."
+        else
+            result="../$result"
+        fi
+    done
+
+    if [[ $common_part == "/" ]]; then
+        # special case for root (no common path)
+        result="$result/"
+    fi
+
+    # since we now have identified the common part,
+    # compute the non-common part
+    forward_part="${target#$common_part}"
+
+    # and now stick all parts together
+    if [[ -n $result ]] && [[ -n $forward_part ]]; then
+        result="$result$forward_part"
+    elif [[ -n $forward_part ]]; then
+        # extra slash removal
+        result="${forward_part:1}"
+    fi
+    echo "$result"
+}
+
+function run_lint {
+    script_working_directory=$1
+    command=$2
+    shift 2
+
+    DOCKER_PATHS=()
+    for E in "${@}"; do
+        ABS_PATH=$(cd "${WORKING_DIR}" && realpath "${E}")
+        DOCKER_PATHS+=("/opt/site/$(relativepath "$(pwd)" "${ABS_PATH}")")
+    done
+    run_command "${script_working_directory}" "${command}" "${DOCKER_PATHS[@]}"
+}
+
 if [[ "$#" -eq 0 ]]; then
     echo "You must provide at least one command."
     echo
@@ -164,10 +219,18 @@ elif [[ "${CMD}" == "build-site" ]]; then
     run_command "/opt/site/landing-pages/" npm run build
 elif [[ "${CMD}" == "lint-js" ]]; then
     ensure_node_module_exists
-    run_command "/opt/site/landing-pages/" npm run lint:js
+    if [[ "$#" -eq 0 ]]; then
+        run_command "/opt/site/landing-pages/" npm run lint:js
+    else
+        run_lint "/opt/site/landing-pages/" ./node_modules/.bin/eslint "$@"
+    fi
 elif [[ "${CMD}" == "lint-css" ]]; then
     ensure_node_module_exists
-    run_command "/opt/site/landing-pages/" npm run lint:css
+    if [[ "$#" -eq 0 ]]; then
+        run_command "/opt/site/landing-pages/" npm run lint:css
+    else
+        run_lint "/opt/site/landing-pages/" ./node_modules/.bin/stylelint "$@"
+    fi
 elif [[ "${CMD}" == "shell" ]]; then
     prevent_docker
     docker exec -ti "${CONTAINER_NAME}" bash

Reply via email to