Skip to content

Commit a0e78c1

Browse files
authored
Merge pull request #244 from JohT/feature/scan-each-package-when-repo-scan-failed
Scan each contained Typescript package when the scan of the whole source repository failed
2 parents 2554c2b + dbd8566 commit a0e78c1

File tree

1 file changed

+81
-17
lines changed

1 file changed

+81
-17
lines changed

scripts/scanTypescript.sh

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")"
2424
echo "scanTypescript: SCRIPTS_DIR=${SCRIPTS_DIR}" >&2
2525

2626
# Dry run for internal testing (not intended to be accessible from the outside)
27-
TYPESCRIPT_SCAN_DRY_RUN=false
27+
TYPESCRIPT_SCAN_DRY_RUN=false
2828
if [ "${TYPESCRIPT_SCAN_DRY_RUN}" = true ] ; then
2929
echo "scanTypescript: -> DRY RUN <- Scanning will only be logged, not executed." >&2
3030
fi
@@ -39,6 +39,66 @@ if ! command -v "npx" &> /dev/null ; then
3939
exit 1
4040
fi
4141

42+
# Takes one parameter containing the directory to search.
43+
# Returns all directories (multi-line) that contain a "package.json" file within the given base directory.
44+
find_directories_with_package_json_file() {
45+
find -L "${1}" \
46+
-type d -name "node_modules" -prune -o \
47+
-type d -name "dist" -prune -o \
48+
-type d -name ".yalc" -prune -o \
49+
-type d -name "lib" -prune -o \
50+
-type d -name "libs" -prune -o \
51+
-type d -name "*test" -prune -o \
52+
-type d -name "*tests" -prune -o \
53+
-name "package.json" \
54+
-print0 \
55+
| xargs -0 -r -I {} dirname {}
56+
}
57+
58+
# Takes one parameter containing the directory to scan for Typescript projects and the second one containing progress information.
59+
# Executes the Typescript scan for the given base directory including subdirectories.
60+
# Skips the scan in case of a dry run
61+
scan_directory() {
62+
local source_directory_name; source_directory_name=$(basename "${1}");
63+
local progress_information; progress_information="${2}"
64+
65+
echo "" >&2 # Output an empty line to have a clearer separation between each scan
66+
67+
if [ "${TYPESCRIPT_SCAN_DRY_RUN}" = false ] ; then
68+
echo "scanTypescript: $(date +'%Y-%m-%dT%H:%M:%S%z') Scanning ${source_directory_name} (${progress_information}) -----------------" >&2
69+
# Note: For later troubleshooting, the output is also copied to a dedicated log file using "tee".
70+
# Note: Don't worry about the hardcoded version number. It will be updated by Renovate using a custom Manager.
71+
# Note: NODE_OPTIONS --max-old-space-size=4096 increases the memory for scanning larger projects
72+
NODE_OPTIONS="${NODE_OPTIONS} --max-old-space-size=${TYPESCRIPT_SCAN_HEAP_MEMORY}" npx --yes @jqassistant/[email protected] "${1}" --extension React 2>&1 | tee "${LOG_DIRECTORY}/jqassistant-typescript-scan-${directory_name}.log" >&2
73+
else
74+
echo "scanTypescript: Skipping scan of ${source_directory_name} (${progress_information}) -----------------" >&2
75+
fi
76+
}
77+
78+
# Takes one parameter containing the directory to scan for Typescript projects.
79+
# Returns true (=0) when the given directory contains a valid (existing and reasonable size) scan result file.
80+
# Otherwise returns false
81+
is_valid_scan_result() {
82+
if [ "${TYPESCRIPT_SCAN_DRY_RUN}" = true ] ; then
83+
echo "scanTypescript: Info: No scan result expected in dry run mode." >&2
84+
return 1 # (false) Since dry run mode won't produce a scan result. Additionally, its intended to also dry-run the individual package scans.
85+
fi
86+
87+
local scan_result_file="${1}/.reports/jqa/ts-output.json"
88+
if [ ! -f "${scan_result_file}" ] ; then
89+
echo "scanTypescript: Info: The scanned file ${scan_result_file} doesn't exist" >&2
90+
return 1 # (false) Since the file doesn't exist it is considered empty.
91+
fi
92+
93+
local scan_file_size; scan_file_size=$(wc -c "${scan_result_file}" | awk '{print $1}')
94+
if [ "${scan_file_size}" -le "600" ]; then
95+
echo "scanTypescript: Info: The scanned file ${scan_result_file} is too small: ${scan_file_size} < 600" >&2
96+
false
97+
else
98+
true
99+
fi
100+
}
101+
42102
# Scan and analyze Artifacts when they were changed
43103
changeDetectionHashFilePath="./${SOURCE_DIRECTORY}/typescriptFileChangeDetectionHashFile.txt"
44104
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --readonly --hashfile "${changeDetectionHashFilePath}" --paths "./${SOURCE_DIRECTORY}")
@@ -54,23 +114,27 @@ if [ "${changeDetectionReturnCode}" != "0" ] || [ "${TYPESCRIPT_SCAN_DRY_RUN}" =
54114
LOG_DIRECTORY="$(pwd)/runtime/logs"
55115
echo "scanTypescript: LOG_DIRECTORY=${LOG_DIRECTORY}" >&2
56116

57-
source_directories=$( find -L "./${SOURCE_DIRECTORY}" -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 -r -I {} echo {} )
58-
59-
total_directories=$(echo "${source_directories}" | wc -l | awk '{print $1}')
60-
processed_directories=0
61-
62-
for directory in ${source_directories}; do
63-
directory_name=$(basename "${directory}");
64-
processed_directories=$((processed_directories + 1))
65-
echo "" >&2
66-
echo "scanTypescript: $(date +'%Y-%m-%dT%H:%M:%S%z') Scanning ${directory_name} (${processed_directories}/${total_directories}) -----------------" >&2
67-
68-
if [ "${TYPESCRIPT_SCAN_DRY_RUN}" = false ] ; then
69-
# Note: For later troubleshooting, the output is also copied to a dedicated log file using "tee".
70-
# Note: Don't worry about the hardcoded version number. It will be updated by Renovate using a custom Manager.
71-
# Note: NODE_OPTIONS --max-old-space-size=4096 increases the memory for scanning larger projects
72-
NODE_OPTIONS="${NODE_OPTIONS} --max-old-space-size=${TYPESCRIPT_SCAN_HEAP_MEMORY}" npx --yes @jqassistant/[email protected] "${directory}" --extension React 2>&1 | tee "${LOG_DIRECTORY}/jqassistant-typescript-scan-${directory_name}.log" >&2
117+
source_directories=$( find -L "./${SOURCE_DIRECTORY}" -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 -r -I {} echo {} )
118+
total_source_directories=$(echo "${source_directories}" | wc -l | awk '{print $1}')
119+
processed_source_directories=0
120+
121+
for source_directory in ${source_directories}; do
122+
processed_source_directories=$((processed_source_directories + 1))
123+
progress_info_source_dirs="${processed_source_directories}/${total_source_directories}"
124+
if scan_directory "${source_directory}" "${progress_info_source_dirs}" && is_valid_scan_result "${source_directory}"; then
125+
continue # successful scan, proceed to next one.
73126
fi
127+
128+
echo "scanTypescript: Info: Unsuccessful source directory scan. Trying to scan all contained packages individually." >&2
129+
contained_package_directories=$( find_directories_with_package_json_file "${source_directory}" )
130+
total_package_directories=$(echo "${contained_package_directories}" | wc -l | awk '{print $1}')
131+
processed_package_directories=0
132+
133+
for contained_package_directory in ${contained_package_directories}; do
134+
processed_package_directories=$((processed_package_directories + 1))
135+
progress_info_package_dirs="${progress_info_source_dirs}: ${processed_package_directories}/${total_package_directories}"
136+
scan_directory "${contained_package_directory}" "${progress_info_package_dirs}"
137+
done
74138
done
75139

76140
if [ "${TYPESCRIPT_SCAN_DRY_RUN}" = false ] ; then

0 commit comments

Comments
 (0)