Skip to content

Instantly share code, notes, and snippets.

@ygurin
Created July 2, 2024 00:38
Show Gist options
  • Save ygurin/af1748f7fd2c3814ddd6293c62bbeba5 to your computer and use it in GitHub Desktop.
Save ygurin/af1748f7fd2c3814ddd6293c62bbeba5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Function to print usage
print_usage() {
echo "Usage: $0 [-d directory]"
}
# Parse the command line arguments
while getopts 'd:' flag; do
case "${flag}" in
d) directory="${OPTARG}" ;;
*) print_usage
exit 1 ;;
esac
done
# If directory is not provided, prompt the user
if [ -z "$directory" ]; then
read -p "Enter the directory name: " directory
fi
# Create project directory
mkdir -p "$directory"
cd "$directory"
# Initialize Node.js project
npm init -y
# Install TypeScript
npm install typescript --save-dev
# Initialize TypeScript configuration
npx tsc --init
sed -i '/"compilerOptions": {/a \
"outDir": "dist", \
"rootDir": "src",' tsconfig.json
# Install ESLint and Prettier, resolving peer dependency conflicts
npm install [email protected] @typescript-eslint/[email protected] @typescript-eslint/[email protected] prettier eslint-config-prettier eslint-plugin-prettier --save-dev
# Create ESLint configuration
cat > .eslintrc.json <<EOL
{
"env": {
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"prettier"
],
"rules": {
"prettier/prettier": "error",
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
}
EOL
# Create Prettier configuration
cat > .prettierrc <<EOL
{
"singleQuote": true,
"semi": true,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80
}
EOL
# Create source directory and sample TypeScript file
mkdir src
cat > src/index.ts <<EOL
const greet = (name: string): string => {
return \`Hello, \${name}!\`;
};
const user = 'World';
console.log(greet(user));
EOL
# Add npm scripts to package.json
jq '.scripts += {
"build": "tsc",
"start": "node dist/index.js",
"lint": "eslint src/**/*.ts --fix",
"format": "prettier --write src/**/*.ts",
"dev": "tsc --watch"
}' package.json > temp.json && mv temp.json package.json
# Build the project
npx tsc
# Run the project
node dist/index.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment