Skip to content

Instantly share code, notes, and snippets.

@nrctkno
Last active May 22, 2022 23:39
Show Gist options
  • Save nrctkno/1564a88d1038d3a5173ee56a871f4760 to your computer and use it in GitHub Desktop.
Save nrctkno/1564a88d1038d3a5173ee56a871f4760 to your computer and use it in GitHub Desktop.
Checks whether all imported modules belong to domain objects in a typescript project
# Checks whether all imported modules belong to domain objects inside domain
# Imported files must
# 1. have a relative path,
# 2. have the module's name enclosed by single or double quotes and
# 3. not propagate outside the domain
filename=check_domain_imports.txt
basedir="src/Domain/"
grep -r -n 'import ' src/Domain/ > $filename
n=0
while read line; do
IFS=':' read -r -a parts <<< "$line"
path=${parts[0]}
line=${parts[1]}
import=${parts[2]}
separators="${path//[^\/]}"
max_escape=$(( "${#separators}" - 1 ))
if [[ $path == "$basedir"* ]] && \
! [[ $import =~ \ from\ [\'\"]((./)|(../){1,${max_escape}})[a-zA-Z] ]]; then
echo "$path:$line: $import"
n=$((n+1))
fi
done < $filename
echo "$n illegal imports found."
rm $filename
exit $n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment