Skip to content

Instantly share code, notes, and snippets.

@robertjdominguez
Created January 22, 2024 20:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertjdominguez/34cba81402e4884e222a33baf2942425 to your computer and use it in GitHub Desktop.
Save robertjdominguez/34cba81402e4884e222a33baf2942425 to your computer and use it in GitHub Desktop.
For quickly scaffolding out a new TDD TS project.
#!/bin/bash
# Check if a project name is provided
if [ -z "$1" ]; then
echo "Please provide a project name."
exit 1
fi
PROJECT_NAME=$1
# Step 1: Create the project
mkdir $PROJECT_NAME
cd $PROJECT_NAME
# Step 2: Init npm and tsc
npm init -y
tsc --init
# Step 3: Scaffold out the project
mkdir src dist
cd src && touch index.ts
# Populate index.ts with a simple Hello World program
echo 'async function main() {
console.log("Hello World!");
}
main();' > index.ts
cd ..
# Step 4: Configure the tsconfig.json
echo '{
"compilerOptions": {
"target": "es2022",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"exclude": ["node_modules", "__tests__"]
}' > tsconfig.json
# Step 5: Set up jest for testing
npm i --save-dev babel-jest @babel/core @babel/preset-env nodemon jest
echo 'module.exports = {
presets: [["@babel/preset-env", {targets: {node: "current"}}]],
};' > babel.config.js
npm i --save-dev @babel/preset-typescript
npm i --save-dev @types/jest
# Update babel.config.js
echo 'module.exports = {
presets: [
["@babel/preset-env", {targets: {node: "current"}}],
"@babel/preset-typescript",
],
};' > babel.config.js
mkdir __tests__
cd __tests__
mkdir sample
cd sample
touch sample.spec.ts
# Add a sample test
echo '// in __tests__/sample/sample.spec.ts
describe("Sample test", () => {
it("should be true", () => {
expect(true).toBe(true);
});
});' > sample.spec.ts
cd ../..
# Step 6: Add scripts to package.json
sed -i '' '/"scripts": {/,/}/c\
"scripts": {\
"build": "tsc --outDir dist",\
"watch": "tsc -w & nodemon --no-deprecation dist/index.js",\
"test": "jest --watchAll --verbose --silent"\
},' package.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment