Skip to content

Instantly share code, notes, and snippets.

@markcallen
Last active April 13, 2025 18:07
Show Gist options
  • Select an option

  • Save markcallen/bfa844c2e48949f4935aefaf8c1b4876 to your computer and use it in GitHub Desktop.

Select an option

Save markcallen/bfa844c2e48949f4935aefaf8c1b4876 to your computer and use it in GitHub Desktop.
This script initializes a TypeScript project with a customized tsconfig.json.
#!/bin/bash
# ------------------------------------------------------------------------------
# TypeScript Config Generator Script
#
# This script initializes a TypeScript project with a customized tsconfig.json.
# It checks for an existing tsconfig.json and exits if one is found.
# It also ensures that strip-json-comments is available to format the output.
#
# Learn more at: https://www.markcallen.com/tsconfig/
# GitHub: https://github.com/markcallen/
# Contact: everydaydevops@markcallen.com
# ------------------------------------------------------------------------------
# Exit if tsconfig.json already exists
if [ -f "tsconfig.json" ]; then
echo "tsconfig.json already exists. Exiting script."
exit 0
fi
# Install strip-json-comments-cli if not found
if ! command -v strip-json-comments &> /dev/null; then
echo "strip-json-comments not found, installing..."
npm install -g strip-json-comments-cli
fi
# Create tsconfig.json
npx tsc --init --rootDir src --outDir dist \
--esModuleInterop --resolveJsonModule --target es2022 \
--module node16 --moduleResolution node16 --allowJs true --noImplicitAny true
# Clean up and format tsconfig.json
cat tsconfig.json | strip-json-comments --no-whitespace | jq -r . > tsconfig.pretty.json && mv tsconfig.pretty.json tsconfig.json
# Add directories to include and exclude
jq '. + {include: ["src/**/*"], exclude: ["node_modules"]}' tsconfig.json > tsconfig.tmp.json && mv tsconfig.tmp.json tsconfig.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment