-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a996638
commit f4be961
Showing
1 changed file
with
136 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,211 +2,186 @@ | |
|
||
set -eu | ||
|
||
# Declare some variables for the color codes | ||
NC='\033[0m' # No Color | ||
GREEN='\033[0;32m' | ||
YELLOW='\033[0;33m' | ||
BLUE='\033[0;34m' | ||
RED='\033[0;31m' | ||
|
||
npm i -g [email protected] | ||
echo "::group::Debug information" | ||
npm -g list --depth=1 | ||
echo "::endgroup::" | ||
|
||
declare -a FIND_CALL | ||
declare -a COMMAND_DIRS COMMAND_FILES | ||
declare -a COMMAND_FILES | ||
|
||
# Declare some variables for the options and arguments | ||
USE_QUIET_MODE="$1" | ||
USE_VERBOSE_MODE="$2" | ||
CONFIG_FILE="$3" | ||
FOLDER_PATH="$4" | ||
MAX_DEPTH="$5" | ||
CHECK_MODIFIED_FILES="$6" | ||
BASE_BRANCH="$7" | ||
if [ -z "$8" ]; then | ||
FILE_EXTENSION=".md" | ||
else | ||
FILE_EXTENSION="$8" | ||
fi | ||
FILE_EXTENSION="$8" | ||
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="" | ||
|
||
echo -e "${BLUE}USE_QUIET_MODE: $1${NC}" | ||
echo -e "${BLUE}USE_VERBOSE_MODE: $2${NC}" | ||
echo -e "${BLUE}FOLDER_PATH: $4${NC}" | ||
echo -e "${BLUE}MAX_DEPTH: $5${NC}" | ||
echo -e "${BLUE}CHECK_MODIFIED_FILES: $6${NC}" | ||
echo -e "${BLUE}FILE_EXTENSION: $8${NC}" | ||
echo -e "${BLUE}FILE_PATH: $9${NC}" | ||
# Declare some variables for the default values | ||
DEFAULT_FILE_EXTENSION=".md" | ||
DEFAULT_FOLDER_PATH="." | ||
DEFAULT_MAX_DEPTH="-1" | ||
DEFAULT_BASE_BRANCH="master" | ||
DEFAULT_ERROR_FILE="error.txt" | ||
DEFAULT_ERROR_CODE="113" | ||
|
||
handle_dirs () { | ||
# Declare some arrays to store the directories and files to check | ||
declare -a COMMAND_DIRS COMMAND_FILES | ||
|
||
IFS=', ' read -r -a DIRLIST <<< "$FOLDER_PATH" | ||
# Install markdown-link-check globally | ||
npm i -g [email protected] | ||
|
||
for index in "${!DIRLIST[@]}" | ||
do | ||
if [ ! -d "${DIRLIST[index]}" ]; then | ||
echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${DIRLIST[index]}${NC}" | ||
exit 2 | ||
fi | ||
COMMAND_DIRS+=("${DIRLIST[index]}") | ||
done | ||
FOLDERS="${COMMAND_DIRS[*]}" | ||
# Show the global npm packages installed | ||
echo "::group::Debug information" | ||
npm -g list --depth=1 | ||
echo "::endgroup::" | ||
|
||
# A function to print a message with a color | ||
print_message () { | ||
local COLOR="$1" # Get the first argument as the color | ||
local MESSAGE="$2" # Get the second argument as the message | ||
echo -e "${COLOR}${MESSAGE}${NC}" # Print the message with the color and no color | ||
} | ||
|
||
handle_files () { | ||
|
||
IFS=', ' read -r -a FILELIST <<< "$FILE_PATH" | ||
|
||
for index in "${!FILELIST[@]}" | ||
do | ||
if [ ! -f "${FILELIST[index]}" ]; then | ||
echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${FILELIST[index]}${NC}" | ||
exit 2 | ||
fi | ||
if [ "$index" == 0 ]; then | ||
COMMAND_FILES+=("-wholename ${FILELIST[index]}") | ||
else | ||
COMMAND_FILES+=("-o -wholename ${FILELIST[index]}") | ||
fi | ||
done | ||
FILES="${COMMAND_FILES[*]}" | ||
|
||
# A function to run the markdown-link-check command with the options | ||
run_markdown_link_check () { | ||
local FILE="$1" # Get the first argument as the file name | ||
local OPTIONS=() # Declare an array to store the options | ||
if [ -f "$CONFIG_FILE" ]; then # Check if the config file exists | ||
OPTIONS+=('--config' "${CONFIG_FILE}") # Add the --config option to the options array | ||
fi | ||
if [ "$USE_QUIET_MODE" = "yes" ]; then # Check if the quiet mode is enabled | ||
OPTIONS+=('-q') # Add the -q option to the options array | ||
fi | ||
if [ "$USE_VERBOSE_MODE" = "yes" ]; then # Check if the verbose mode is enabled | ||
OPTIONS+=('-v') # Add the -v option to the options array | ||
fi | ||
markdown-link-check "${OPTIONS[@]}" "$FILE" &>> "$DEFAULT_ERROR_FILE" || true # Run the markdown-link-check command with the options and the file name, and append the output to the error file, ignore the exit code | ||
} | ||
|
||
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 | ||
|
||
# A function to parse and validate the arguments | ||
parse_arguments () { | ||
# Set the default values for the arguments | ||
if [ -z "$FILE_EXTENSION" ]; then | ||
FILE_EXTENSION="$DEFAULT_FILE_EXTENSION" | ||
fi | ||
if [ -z "$FOLDER_PATH" ]; then | ||
FOLDER_PATH="$DEFAULT_FOLDER_PATH" | ||
fi | ||
if [ -z "$MAX_DEPTH" ]; then | ||
MAX_DEPTH="$DEFAULT_MAX_DEPTH" | ||
fi | ||
if [ -z "$BASE_BRANCH" ]; then | ||
BASE_BRANCH="$DEFAULT_BASE_BRANCH" | ||
fi | ||
# Print the arguments | ||
print_message "$BLUE" "USE_QUIET_MODE: $USE_QUIET_MODE" | ||
print_message "$BLUE" "USE_VERBOSE_MODE: $USE_VERBOSE_MODE" | ||
print_message "$BLUE" "CONFIG_FILE: $CONFIG_FILE" | ||
print_message "$BLUE" "FOLDER_PATH: $FOLDER_PATH" | ||
print_message "$BLUE" "MAX_DEPTH: $MAX_DEPTH" | ||
print_message "$BLUE" "CHECK_MODIFIED_FILES: $CHECK_MODIFIED_FILES" | ||
print_message "$BLUE" "FILE_EXTENSION: $FILE_EXTENSION" | ||
print_message "$BLUE" "FILE_PATH: $FILE_PATH" | ||
# Check if the config file exists | ||
if [ -f "$CONFIG_FILE" ]; then | ||
print_message "$BLUE" "Using markdown-link-check configuration file: $CONFIG_FILE" | ||
else | ||
print_message "$BLUE" "Cannot find $CONFIG_FILE" | ||
print_message "$YELLOW" "NOTE: See https://github.com/tcort/markdown-link-check#config-file-format to know more about customizing markdown-link-check by using a configuration file." | ||
fi | ||
} | ||
|
||
add_options () { | ||
|
||
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 | ||
|
||
# A function to handle the directories | ||
handle_dirs () { | ||
IFS=', ' read -r -a DIRLIST <<< "$FOLDER_PATH" # Split the folder path by comma and store it in an array | ||
for index in "${!DIRLIST[@]}" | ||
do | ||
if [ ! -d "${DIRLIST[index]}" ]; then # Check if the directory exists | ||
print_message "$RED" "ERROR [✖] Can't find the directory: ${DIRLIST[index]}" | ||
exit 2 # Exit with error code 2 | ||
fi | ||
COMMAND_DIRS+=("${DIRLIST[index]}") # Add the directory to the command dirs array | ||
done | ||
} | ||
|
||
check_additional_files () { | ||
|
||
if [ -n "$FILES" ]; then | ||
if [ "$MAX_DEPTH" -ne -1 ]; then | ||
FIND_CALL=('find' ${FOLDERS} '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}') | ||
else | ||
FIND_CALL=('find' ${FOLDERS} '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}') | ||
fi | ||
|
||
add_options | ||
|
||
FIND_CALL+=(';') | ||
|
||
set -x | ||
"${FIND_CALL[@]}" &>> error.txt | ||
set +x | ||
|
||
fi | ||
|
||
# A function to handle the files | ||
handle_files () { | ||
IFS=', ' read -r -a FILELIST <<< "$FILE_PATH" # Split the file path by comma and store it in an array | ||
for index in "${!FILELIST[@]}" | ||
do | ||
if [ ! -f "${FILELIST[index]}" ]; then # Check if the file exists | ||
print_message "$RED" "ERROR [✖] Can't find the file: ${FILELIST[index]}" | ||
exit 2 # Exit with error code 2 | ||
fi | ||
if [ "$index" == 0 ]; then | ||
COMMAND_FILES+=("-wholename ${FILELIST[index]}") # Add the file name with -wholename option to the command files array | ||
else | ||
COMMAND_FILES+=("-o -wholename ${FILELIST[index]}") # Add the file name with -o and -wholename options to the command files array | ||
fi | ||
done | ||
} | ||
|
||
if [ -z "$8" ]; then | ||
FOLDERS="." | ||
else | ||
handle_dirs | ||
fi | ||
|
||
if [ -n "$9" ]; then | ||
handle_files | ||
fi | ||
# A function to check and delete the error file | ||
check_error_file () { | ||
if [ -e "$DEFAULT_ERROR_FILE" ] ; then # Check if the error file exists | ||
if grep -q "ERROR:" "$DEFAULT_ERROR_FILE"; then # Check if the error file contains any ERROR: lines | ||
print_message "$YELLOW" "=========================> MARKDOWN LINK CHECK <=========================" | ||
cat "$DEFAULT_ERROR_FILE" # Print the error file | ||
printf "\n" | ||
print_message "$YELLOW" "=========================================================================" | ||
rm "$DEFAULT_ERROR_FILE" # Delete the error file | ||
exit "$DEFAULT_ERROR_CODE" # Exit with the default error code | ||
else | ||
print_message "$YELLOW" "=========================> MARKDOWN LINK CHECK <=========================" | ||
printf "\n" | ||
print_message "$GREEN" "[✔] All links are good!" | ||
printf "\n" | ||
print_message "$YELLOW" "=========================================================================" | ||
rm "$DEFAULT_ERROR_FILE" # Delete the error file | ||
fi | ||
else | ||
print_message "$GREEN" "All good!" | ||
fi | ||
} | ||
|
||
if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then | ||
# Parse and validate the arguments | ||
parse_arguments | ||
|
||
echo -e "${BLUE}BASE_BRANCH: $7${NC}" | ||
# Handle the directories | ||
handle_dirs | ||
|
||
git config --global --add safe.directory '*' | ||
# Handle the files | ||
handle_files | ||
|
||
git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null | ||
MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}") | ||
if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then # Check if the check modified files option is enabled | ||
|
||
if [ -z "$FOLDERS" ]; then | ||
FOLDERS="." | ||
fi | ||
print_message "$BLUE" "BASE_BRANCH: $BASE_BRANCH" # Print the base branch name | ||
|
||
FIND_CALL=('markdown-link-check') | ||
git config --global --add safe.directory '*' # Add a global git config to allow any directory name | ||
|
||
add_options | ||
git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null # Fetch the base branch from origin with depth 1 and discard the output | ||
MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}") # Get the hash of the base branch | ||
|
||
FOLDER_ARRAY=(${FOLDER_PATH//,/ }) | ||
mapfile -t FILE_ARRAY < <( git diff --name-only --diff-filter=AM "$MASTER_HASH" -- "${FOLDER_ARRAY[@]}") | ||
mapfile -t FILE_ARRAY < <( git diff --name-only --diff-filter=AM "$MASTER_HASH" -- "${COMMAND_DIRS[@]}") # Get the modified files from the git diff and store them in an array | ||
|
||
for i in "${FILE_ARRAY[@]}" | ||
do | ||
if [ "${i##*.}" == "${FILE_EXTENSION#.}" ]; then | ||
FIND_CALL+=("${i}") | ||
COMMAND="${FIND_CALL[*]}" | ||
$COMMAND &>> error.txt || true | ||
unset 'FIND_CALL[${#FIND_CALL[@]}-1]' | ||
fi | ||
done | ||
# Use xargs to run the markdown-link-check command on multiple files at once | ||
printf '%s\n' "${FILE_ARRAY[@]}" | parallel -j 10 run_markdown_link_check {} "$FILE_EXTENSION" | ||
|
||
check_additional_files | ||
|
||
check_errors | ||
# Check and delete the error file | ||
check_error_file | ||
|
||
else | ||
|
||
if [ "$5" -ne -1 ]; then | ||
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}') | ||
else | ||
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}') | ||
fi | ||
|
||
add_options | ||
|
||
FIND_CALL+=(';') | ||
|
||
set -x | ||
"${FIND_CALL[@]}" &>> error.txt | ||
set +x | ||
# Use xargs to run the markdown-link-check command on multiple files at once | ||
find "${COMMAND_DIRS[@]}" -iname "*${FILE_EXTENSION}" -not -path './node_modules/*' -maxdepth "${MAX_DEPTH}" -print0 | parallel -0 -j 10 run_markdown_link_check {} | ||
|
||
check_additional_files | ||
|
||
check_errors | ||
# Check and delete the error file | ||
check_error_file | ||
|
||
fi |