Skip to content

Instantly share code, notes, and snippets.

@bsnux
Last active February 17, 2023 05:59
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 bsnux/deb7d6b159150041f7caf21d3a30aa72 to your computer and use it in GitHub Desktop.
Save bsnux/deb7d6b159150041f7caf21d3a30aa72 to your computer and use it in GitHub Desktop.
Creates a new TypeScript project ready to use with Node.js
#!/bin/bash
#
# Creates a new TypeScript project ready to use with Node.js
set -euo pipefail
if [[ ! -f $(which npm) ]] || [[ ! -f $(which yarn) ]]; then
echo "Please, install npm and yarn before continuing"
echo "See https://docs.npmjs.com/downloading-and-installing-node-js-and-npm"
echo "See https://yarnpkg.com/"
exit
fi
yarn init -y
yarn add typescript ts-node ts-loader webpack webpack-cli @types/node -D
./node_modules/.bin/tsc --init
mkdir src && mkdir build
sed -i.bak 's/\/\/ "outDir": "\.\/"/"outDir": "\.\/build\/"/' tsconfig.json
jq '. += {"scripts": {"start": "npx ts-node src","build": "npx tsc", "dist": "webpack --mode production"}}' < package.json > package.json.tmp
mv package.json.tmp package.json
cat<<EOF>>webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.ts',
module: {
rules: [
{
test: /\.ts?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
}
};
EOF
echo "=== Done! ==="
echo "Now you can add TS files to src/"
echo "Then, you can run:"
# Running the code
echo "$ yarn run start"
# Building a JS file
echo "$ yarn run build"
# Building a bundle JS file
echo "$ yarn run dist"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment