Bash script to dump a TypeScript project into flat directory

Here’s a Bash script to dump a project into a flat file structure in a single directory. It turns nested paths into flat filenames and prepends each file with its original source path. It also includes the current Git hash in each flat filename, so that LLM memory can distinguish different versions if necessary.

The script also writes a manifest file that maps each flattened filename back to its original path. For example, services__users__get-user__abc123.ts maps to src/services/users/get-user.ts. This lets the LLM see which other files exist in the project even if you don’t copy them into the prompt context. You can also prompt the LLM to ask to see other files it thinks are relevant in its task.

This is useful for putting multiple project files into an LLM prompt context at once without needing to navigate the project’s directory structure.

#!/usr/bin/env bash

set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/../.." && pwd)"

GIT_SHORT_HASH="$(git -C "${REPO_ROOT}" rev-parse --short HEAD)"

SRC_DIR="${REPO_ROOT}/src"
OUT_DIR="${REPO_ROOT}/tmp"
MANIFEST_FILE="${OUT_DIR}/000_manifest_${GIT_SHORT_HASH}.txt"

rm -rf "${OUT_DIR}"
mkdir -p "${OUT_DIR}"

find "${SRC_DIR}" \
  -type f \
  -name '*.ts' \
  ! -name 'index.ts' |
sort |
while read -r file; do
  rel_path="${file#"${SRC_DIR}"/}"
  flat_name="${rel_path//\//__}"
  flat_name="${flat_name%.ts}__${GIT_SHORT_HASH}.ts"

  {
    echo "// ============================================================="
    echo "// SOURCE: src/${rel_path}"
    echo "// ============================================================="
    echo
    cat "${file}"
  } > "${OUT_DIR}/${flat_name}"

  echo "${flat_name} -> src/${rel_path}"
done > "${MANIFEST_FILE}"

cp -f "${REPO_ROOT}/README.md" "${OUT_DIR}/000_README_${GIT_SHORT_HASH}.md"

echo "Dumped TypeScript files to ${OUT_DIR}"
echo "Wrote manifest to ${MANIFEST_FILE}"

I use this in projects like Yulin and Smartass when I want to use a separate LLM tool without navigating the project directory by hand.


View post: Bash script to dump a TypeScript project into flat directory