Last active
January 8, 2025 02:22
-
-
Save degensean/b4935b441a7ca2ff4b70998f288e3eb1 to your computer and use it in GitHub Desktop.
Initialize a Typescript project using yarn with git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: ./init-ts-project.sh my-new-project | |
## Prerequisites: | |
# Install yarn: https://classic.yarnpkg.com/en/docs/install/#windows-stable | |
# Install jq: https://jqlang.github.io/jq/download/ | |
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 | |
# Check if jq is installed | |
if ! command -v jq &> /dev/null | |
then | |
echo "Error: jq is not installed. Please install it before running this script." | |
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 basic README.md file inside it | |
echo "# $PROJECT_NAME" > READNE.md | |
# 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. | |
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 | |
# TypeScript cache | |
/.tsbuildinfo | |
# dotenv environment variable files | |
.env | |
.env.local | |
.env.development.local | |
.env.test.local | |
.env.production.local | |
.env.example | |
# OS-generated files | |
.DS_Store | |
Thumbs.db | |
# Logs | |
logs | |
*.log | |
npm-debug.log* | |
yarn-debug.log* | |
yarn-error.log* | |
# IDE and editor settings | |
.vscode | |
.idea | |
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