2019-04-03 08:16:17 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2019-10-09 03:53:02 +03:00
|
|
|
NC='\033[0m' # No Color
|
|
|
|
GREEN='\033[0;32m'
|
|
|
|
YELLOW='\033[0;33m'
|
|
|
|
BLUE='\033[0;34m'
|
2020-08-19 07:44:02 +03:00
|
|
|
RED='\033[0;31m'
|
2019-10-09 03:53:02 +03:00
|
|
|
|
2020-12-09 17:26:36 +03:00
|
|
|
npm i -g markdown-link-check@3.8.5
|
2019-04-03 08:16:17 +03:00
|
|
|
|
2020-04-05 13:20:35 +03:00
|
|
|
declare -a FIND_CALL
|
2020-08-09 15:01:29 +03:00
|
|
|
declare -a COMMAND_DIRS COMMAND_FILES
|
|
|
|
declare -a COMMAND_FILES
|
2020-04-05 13:20:35 +03:00
|
|
|
|
2020-02-12 04:56:20 +03:00
|
|
|
USE_QUIET_MODE="$1"
|
2020-02-10 04:43:16 +03:00
|
|
|
USE_VERBOSE_MODE="$2"
|
|
|
|
CONFIG_FILE="$3"
|
2020-02-12 04:56:20 +03:00
|
|
|
FOLDER_PATH="$4"
|
2020-04-01 13:12:32 +03:00
|
|
|
MAX_DEPTH="$5"
|
2020-04-05 13:20:35 +03:00
|
|
|
CHECK_MODIFIED_FILES="$6"
|
|
|
|
BASE_BRANCH="$7"
|
2020-06-16 02:19:01 +03:00
|
|
|
if [ -z "$8" ]; then
|
|
|
|
FILE_EXTENSION=".md"
|
2020-06-16 11:36:25 +03:00
|
|
|
else
|
|
|
|
FILE_EXTENSION="$8"
|
2020-06-06 14:21:20 +03:00
|
|
|
fi
|
2020-08-09 15:01:29 +03:00
|
|
|
FILE_PATH="$9"
|
|
|
|
|
|
|
|
if [ -f "$CONFIG_FILE" ]; then
|
|
|
|
echo -e "${BLUE}Using markdown-link-check configuration file: ${YELLOW}$CONFIG_FILE${NC}"
|
|
|
|
else
|
|
|
|
echo -e "${BLUE}Cannot find ${YELLOW}$CONFIG_FILE${NC}"
|
|
|
|
echo -e "${YELLOW}NOTE: See https://github.com/tcort/markdown-link-check#config-file-format to know more about"
|
|
|
|
echo -e "customizing markdown-link-check by using a configuration file.${NC}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
FOLDERS=""
|
|
|
|
FILES=""
|
2020-02-10 04:43:16 +03:00
|
|
|
|
2020-02-12 04:56:20 +03:00
|
|
|
echo -e "${BLUE}USE_QUIET_MODE: $1${NC}"
|
|
|
|
echo -e "${BLUE}USE_VERBOSE_MODE: $2${NC}"
|
|
|
|
echo -e "${BLUE}FOLDER_PATH: $4${NC}"
|
2020-04-01 13:12:32 +03:00
|
|
|
echo -e "${BLUE}MAX_DEPTH: $5${NC}"
|
2020-04-05 13:20:35 +03:00
|
|
|
echo -e "${BLUE}CHECK_MODIFIED_FILES: $6${NC}"
|
2020-04-14 05:21:58 +03:00
|
|
|
echo -e "${BLUE}FILE_EXTENSION: $8${NC}"
|
2020-08-09 15:01:29 +03:00
|
|
|
echo -e "${BLUE}FILE_PATH: $9${NC}"
|
|
|
|
|
|
|
|
handle_dirs () {
|
|
|
|
|
|
|
|
IFS=', ' read -r -a DIRLIST <<< "$FOLDER_PATH"
|
|
|
|
|
|
|
|
for index in "${!DIRLIST[@]}"
|
|
|
|
do
|
2020-08-19 07:44:02 +03:00
|
|
|
if [ ! -d "${DIRLIST[index]}" ]; then
|
|
|
|
echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${DIRLIST[index]}${NC}"
|
|
|
|
exit 2
|
|
|
|
fi
|
2020-08-09 15:01:29 +03:00
|
|
|
COMMAND_DIRS+=("${DIRLIST[index]}")
|
|
|
|
done
|
|
|
|
FOLDERS="${COMMAND_DIRS[*]}"
|
2020-02-10 04:43:16 +03:00
|
|
|
|
2020-04-05 13:20:35 +03:00
|
|
|
}
|
2020-04-03 05:26:57 +03:00
|
|
|
|
2020-08-09 15:01:29 +03:00
|
|
|
handle_files () {
|
2020-02-10 04:43:16 +03:00
|
|
|
|
2020-08-09 15:01:29 +03:00
|
|
|
IFS=', ' read -r -a FILELIST <<< "$FILE_PATH"
|
2020-02-10 04:43:16 +03:00
|
|
|
|
2020-08-09 15:01:29 +03:00
|
|
|
for index in "${!FILELIST[@]}"
|
|
|
|
do
|
2020-08-19 07:44:02 +03:00
|
|
|
if [ ! -f "${FILELIST[index]}" ]; then
|
|
|
|
echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${FILELIST[index]}${NC}"
|
|
|
|
exit 2
|
|
|
|
fi
|
2020-12-09 16:33:00 +03:00
|
|
|
if [ "$index" == 0 ]; then
|
2020-08-09 15:01:29 +03:00
|
|
|
COMMAND_FILES+=("-wholename ${FILELIST[index]}")
|
|
|
|
else
|
|
|
|
COMMAND_FILES+=("-o -wholename ${FILELIST[index]}")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
FILES="${COMMAND_FILES[*]}"
|
2020-02-10 04:43:16 +03:00
|
|
|
|
2020-08-09 15:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
check_errors () {
|
|
|
|
|
|
|
|
if [ -e error.txt ] ; then
|
|
|
|
if grep -q "ERROR:" error.txt; then
|
|
|
|
echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}"
|
|
|
|
cat error.txt
|
|
|
|
printf "\n"
|
|
|
|
echo -e "${YELLOW}=========================================================================${NC}"
|
|
|
|
exit 113
|
|
|
|
else
|
|
|
|
echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}"
|
|
|
|
printf "\n"
|
|
|
|
echo -e "${GREEN}[✔] All links are good!${NC}"
|
|
|
|
printf "\n"
|
|
|
|
echo -e "${YELLOW}=========================================================================${NC}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo -e "${GREEN}All good!${NC}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
add_options () {
|
2020-04-05 13:20:35 +03:00
|
|
|
|
|
|
|
if [ -f "$CONFIG_FILE" ]; then
|
|
|
|
FIND_CALL+=('--config' "${CONFIG_FILE}")
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$USE_QUIET_MODE" = "yes" ]; then
|
|
|
|
FIND_CALL+=('-q')
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$USE_VERBOSE_MODE" = "yes" ]; then
|
|
|
|
FIND_CALL+=('-v')
|
|
|
|
fi
|
|
|
|
|
2020-08-09 15:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
check_additional_files () {
|
|
|
|
|
|
|
|
if [ -n "$FILES" ]; then
|
|
|
|
if [ "$MAX_DEPTH" -ne -1 ]; then
|
2020-12-11 19:18:55 +03:00
|
|
|
FIND_CALL=('find' '.' '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
|
2020-08-09 15:01:29 +03:00
|
|
|
else
|
2020-12-11 19:18:55 +03:00
|
|
|
FIND_CALL=('find' '.' '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}')
|
2020-08-09 15:01:29 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
add_options
|
|
|
|
|
|
|
|
FIND_CALL+=(';')
|
|
|
|
|
|
|
|
set -x
|
|
|
|
"${FIND_CALL[@]}" &>> error.txt
|
|
|
|
set +x
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ -z "$8" ]; then
|
|
|
|
FOLDERS="."
|
|
|
|
else
|
|
|
|
handle_dirs
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$9" ]; then
|
|
|
|
handle_files
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then
|
|
|
|
|
|
|
|
echo -e "${BLUE}BASE_BRANCH: $7${NC}"
|
|
|
|
|
|
|
|
git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null
|
|
|
|
MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}")
|
|
|
|
|
|
|
|
FIND_CALL=('markdown-link-check')
|
|
|
|
|
|
|
|
add_options
|
|
|
|
|
2020-08-20 12:32:05 +03:00
|
|
|
mapfile -t FILE_ARRAY < <( git diff --name-only --diff-filter=AM "$MASTER_HASH" )
|
2020-04-05 13:20:35 +03:00
|
|
|
|
|
|
|
for i in "${FILE_ARRAY[@]}"
|
|
|
|
do
|
2020-08-07 05:21:48 +03:00
|
|
|
if [ "${i##*.}" == "${FILE_EXTENSION#.}" ]; then
|
2020-04-05 13:20:35 +03:00
|
|
|
FIND_CALL+=("${i}")
|
2020-06-09 10:57:51 +03:00
|
|
|
COMMAND="${FIND_CALL[*]}"
|
2020-04-05 13:20:35 +03:00
|
|
|
$COMMAND &>> error.txt || true
|
|
|
|
unset 'FIND_CALL[${#FIND_CALL[@]}-1]'
|
|
|
|
fi
|
|
|
|
done
|
2020-08-09 15:01:29 +03:00
|
|
|
|
|
|
|
check_additional_files
|
2020-04-05 13:20:35 +03:00
|
|
|
|
|
|
|
check_errors
|
2020-02-10 04:43:16 +03:00
|
|
|
|
2019-10-09 03:53:02 +03:00
|
|
|
else
|
2020-04-05 13:20:35 +03:00
|
|
|
|
|
|
|
if [ "$5" -ne -1 ]; then
|
2020-12-11 00:25:30 +03:00
|
|
|
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
|
2020-04-05 13:20:35 +03:00
|
|
|
else
|
2020-12-11 00:25:30 +03:00
|
|
|
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}')
|
2020-04-05 13:20:35 +03:00
|
|
|
fi
|
|
|
|
|
2020-08-09 15:01:29 +03:00
|
|
|
add_options
|
2020-04-05 13:20:35 +03:00
|
|
|
|
|
|
|
FIND_CALL+=(';')
|
|
|
|
|
|
|
|
set -x
|
|
|
|
"${FIND_CALL[@]}" &>> error.txt
|
|
|
|
set +x
|
2020-08-09 15:01:29 +03:00
|
|
|
|
|
|
|
check_additional_files
|
|
|
|
|
2020-04-05 13:20:35 +03:00
|
|
|
check_errors
|
|
|
|
|
2019-04-03 08:16:17 +03:00
|
|
|
fi
|