Skip to content

Instantly share code, notes, and snippets.

@sajadghawami
Last active July 9, 2024 13:12
Show Gist options
  • Save sajadghawami/377126415c6125ca1219d57063e05cfc to your computer and use it in GitHub Desktop.
Save sajadghawami/377126415c6125ca1219d57063e05cfc to your computer and use it in GitHub Desktop.
Generate a typescript project with nodemon
#!/bin/bash
# Ask for folder name
read -p "Enter your project folder name: " folder_name
# Create the project folder and navigate into it
mkdir "$folder_name"
cd "$folder_name"
# Initialize Project
yarn init -y
# Install TypeScript, ts-node, and nodemon
yarn add typescript ts-node --dev
yarn add nodemon --dev
# Create src folder and index.ts
mkdir src
touch src/index.ts
# Initialize TypeScript config
npx tsc --init
# Configure tsconfig.json
cat <<EOT > tsconfig.json
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
EOT
# Create nodemon.json configuration
cat <<EOT > nodemon.json
{
"watch": ["src"],
"ext": "ts",
"exec": "ts-node ./src/index.ts"
}
EOT
# Create .gitignore
cat <<EOT > .gitignore
# Node modules
node_modules/
# TypeScript compiled output
dist/
*.tsbuildinfo
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Environment variables
.env
EOT
# Update package.json using a temporary node script
node -e "
const fs = require('fs');
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
packageJson.main = 'dist/index.js';
packageJson.types = 'dist/index.d.ts';
packageJson.files = ['dist', 'src'];
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.build = 'tsc';
packageJson.scripts.dev = 'nodemon';
// Reorder package.json
const orderedPackageJson = {
name: packageJson.name,
version: packageJson.version,
description: packageJson.description,
main: packageJson.main,
types: packageJson.types,
files: packageJson.files,
scripts: packageJson.scripts,
keywords: packageJson.keywords,
author: packageJson.author,
license: packageJson.license,
dependencies: packageJson.dependencies,
devDependencies: packageJson.devDependencies
};
fs.writeFileSync('package.json', JSON.stringify(orderedPackageJson, null, 2));
"
# Build the project
yarn build
echo "Project setup is complete. You can now publish your package using 'yarn publish'."
echo "To start watching your TypeScript files, use 'yarn dev'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment