#!/bin/bash

# This script generate files that analyse and report the structure and complexity of the source code.
# Usage: $ scripts/code-analysis/report.sh

THIS_DIR=$(dirname "$0")
ASSETS_DIR="${THIS_DIR}/assets"

mkdir -p "${ASSETS_DIR}"

echo "🔬  Generating ${THIS_DIR}/code-complexity.txt ..."
npx --yes code-complexity@v4.2.2 . \
  --filter 'app/**/*.js' \
  --sort name \
  --format \
  | perl -pe 's/\x1b\[[0-9;]*[mG]//g' \
  > ${THIS_DIR}/code-complexity.txt

echo "🔬  Generating ${ASSETS_DIR}/arkit.svg ..."
npx --yes arkit app/ -o ${ASSETS_DIR}/arkit.svg

echo "🔬  Generating ${ASSETS_DIR}/dependency-ddot.svg ..."
npx --yes dependency-cruiser app --config --exclude node_modules --output-type ddot | dot -T svg -Grankdir=TD > ${ASSETS_DIR}/dependency-ddot.svg      

echo "🔬  Generating ${ASSETS_DIR}/dependency-archi.svg ..."
npx --yes dependency-cruiser app --config --exclude node_modules --output-type archi | dot -T svg -Grankdir=TD > ${ASSETS_DIR}/dependency-archi.svg

echo "🔬  Generating ${THIS_DIR}/dependency-graph.md ..."
cat > ${THIS_DIR}/dependency-graph.md << CONTENT
# Dependency analysis generated by \`${THIS_DIR}/report.sh\`

$(npx dependency-cruiser app --config --exclude node_modules --output-type markdown)

## DDot graph

![](./assets/dependency-ddot.svg)

## Archi graph

![](./assets/dependency-archi.svg)

## Full dependency graph

\`\`\`mermaid
$(npx --yes dependency-cruiser app --config --exclude node_modules --output-type mermaid)
\`\`\`

## Full dependency graph by arkit

![](./assets/arkit.svg)
CONTENT
