A powerful and flexible Node.js script to automate the setup of a TypeScript project. This script initializes a new or existing directory as a TypeScript project, adds all required configuration files, and installs dependencies. It also intelligently assigns the author field in the package.json using the connected GitHub username or the system username as a fallback.
- Initializes
package.jsonwith preconfigured values. - Automatically installs dependencies and devDependencies (fixed versions or latest stable versions based on the configuration).
- Creates a
tsconfig.jsonwith predefined compiler options. - Dynamically sets the project author using:
- GitHub username if available (retrieved from Git configuration).
- System username as a fallback.
- Adds a basic
srcdirectory with a starterindex.tsfile (only if the folder doesn't already exist). - Works directly in the current directory without creating additional folders.
- Node.js and npm installed on your machine.
- Git installed if you want to retrieve the author name from your GitHub configuration.
- Optionally, configure your GitHub username using:
git config --global user.name "YourGitHubUsername"
project-directory/
├── package.json
├── tsconfig.json
├── node_modules/
├── src/
│ └── index.ts
├── .gitignore
├── .env
The script uses three JSON configuration files located in the configs directory:
Defines the package.json template, including dependencies, scripts, and metadata.
{
"version": "1.0.0",
"description": "A TypeScript project",
"main": "dist/index.js",
"scripts": {
"start": "node dist/index.js",
"build": "tsc"
},
"dependencies": {
"ts-node": "", // Latest stable version
"dotenv": "" // Latest stable version
},
"devDependencies": {
"typescript": "4.9.5", // Specific version
"@types/node": "" // Latest stable version
}
}Specifies the TypeScript compiler options.
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}Defines the .gitignore template used by the project. This configuration file allows for easy customization of ignored files and directories.
{
"ignore": [
"# Node Modules",
"node_modules/",
"dist/",
"coverage/",
"temp/",
"*.log",
"",
"# TypeScript",
"*.tsbuildinfo",
"",
"# Environment variables",
".env",
".env.local",
".env.development",
".env.production",
".env.test",
"",
"# IDE-Specific Files",
".vscode/",
".idea/",
".DS_Store"
]
}A .gitignore file is automatically created based on the configs/gitConfig.json. It prevents unwanted files such as node_modules, build artifacts, and environment variables from being tracked by Git.
Example .gitignore:
# Node Modules
node_modules/
dist/
coverage/
temp/
*.log
# TypeScript
*.tsbuildinfo
# Environment variables
.env
.env.local
.env.development
.env.production
.env.test
# IDE-Specific Files
.vscode/
.idea/
.DS_StoreA blank .env file is created to store environment variables. You can add your sensitive API keys and configuration settings here.
Example .env:
# Add your environment variables here
API_KEY=
DATABASE_URL= If GitHub is configured:
• The author field in package.json will use the username from git config --get user.name.
If GitHub is not configured:
• The system username will be used as a fallback.
The script is designed to prevent overwriting existing projects. If you want to reinitialize, manually delete the package.json and other generated files before running the script.
Ensure Git is installed and your username is set with:
git config --global user.name "YourGitHubUsername"