Skip to content

Instantly share code, notes, and snippets.

@degensean
Last active April 28, 2024 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save degensean/b4935b441a7ca2ff4b70998f288e3eb1 to your computer and use it in GitHub Desktop.
Save degensean/b4935b441a7ca2ff4b70998f288e3eb1 to your computer and use it in GitHub Desktop.
Initialize a Typescript project using yarn with git
#!/bin/bash
# Usage: ./init-ts-project.sh my-new-project
PROJECT_NAME=$1
# Check if a project name was provided
if [ -z "$PROJECT_NAME" ]; then
echo "Error: No project name provided."
echo "Usage: ./init-ts-project.sh my-new-project"
exit 1
fi
# Create a new directory for the project and enter it
mkdir "$PROJECT_NAME"
cd "$PROJECT_NAME" || exit
# Initialize a new Yarn project
yarn init -y
# Add TypeScript, ts-node, and nodemon as dev dependencies
yarn add typescript ts-node --dev
# Initialize TypeScript with a default tsconfig.json
npx tsc --init
# Create a src directory and a basic index.ts file inside it
mkdir src
echo "console.log('Hello, TypeScript');" > src/index.ts
# Add the dev scripts to package.json. Install jp beforehand.
jq '.scripts += {"dev": "ts-node src/index.ts"}' package.json > temp.json && mv temp.json package.json
# Initialize a new Git repository (optional)
git init
# Set the default branch name to 'main'
git branch -M main
# Create a .gitignore file
cat > .gitignore << EOF
# See https://www.toptal.com/developers/gitignore/api/node,yarn
# for more about what to include here
# Dependency directories
node_modules/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional nyc test coverage
.nyc_output
# Optional Jest test results
/coverage
# TypeScript build output
/dist
/build
# dotenv environment variable files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
.env.example
# Misc
.DS_Store
*.log
EOF
# Done
echo "TypeScript project $PROJECT_NAME initialized with main branch and .gitignore."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment