Skip to content

Instantly share code, notes, and snippets.

@maxwellb
Last active January 23, 2019 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxwellb/b80d29c8b89fe275dd7b8509eb20c52c to your computer and use it in GitHub Desktop.
Save maxwellb/b80d29c8b89fe275dd7b8509eb20c52c to your computer and use it in GitHub Desktop.
NodeJS/bash starter scripts

NodeJS/bash starter scripts

My collection of "templates" from around the web.

TypeScript-Node starter (minimal server)

Script will

  • Install packages to packages.json
  • Add npm run scripts to package.json
  • Download several files from TypeScript-Node-Starter
  • Make some modifications to the downloaded files

After running, add your own app.ts file in src/ and build with npm run build.

Run with:

curl https://gist.githubusercontent.com/maxwellb/\
b80d29c8b89fe275dd7b8509eb20c52c/raw/\
66d7e6ef4d7732a960a0feb2f6028f4a2944dbe0/typescript-node-starter_minimal-server.sh \
| bash
#!/usr/bin/env sh
# https://github.com/Microsoft/TypeScript-Node-Starter
if [ ! -f ./package.json ]; then
echo "Please run 'npm init' first."
exit 1
fi
DL=
[ -z "$DL" ] && [ -x `which curl` ] && DL="`which curl` -sLO"
[ -z "$DL" ] && [ -x `which wget` ] && DL="`which wget` -q"
[ -z "$DL" ] && echo "Did not find curl or wget. Exiting." && exit 1
NODE=
[ -z "$NODE" ] && [ -x `which node` ] && NODE="`which node`"
[ -z "$NODE" ] && [ -x `which nodejs` ] && NODE="`which nodejs`"
[ -z "$NODE" ] && echo "Did not find NodeJS. Exiting." && exit 1
NPM=
[ -z "$NPM" ] && [ -x `which npm` ] && NPM="`which npm`"
[ -z "$NPM" ] && echo "Did not find NPM. Exiting." && exit 1
$NPM i --save compression dotenv errorhandler express winston
$NPM i --save-dev concurrently node-sass nodemon shelljs ts-node tslint typescript
$NPM i --save-dev @types/compression @types/dotenv @types/errorhandler @types/express \
@types/node @types/shelljs
echo "
'use strict';
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('./package.json'));
pkg.main = 'dist/server.js';
pkg.scripts = pkg.scripts || {};
pkg.scripts['start'] = 'npm run serve';
pkg.scripts['build'] = 'npm run build-sass && npm run build-ts && npm run tslint && npm run copy-static-assets';
pkg.scripts['serve'] = 'node dist/server.js';
pkg.scripts['watch-node'] = 'nodemon dist/server.js';
pkg.scripts['watch'] = 'concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run watch-node\"';
pkg.scripts['build-ts'] = 'tsc';
pkg.scripts['watch-ts'] = 'tsc -w';
pkg.scripts['build-sass'] = 'node-sass src/public/css/main.scss dist/public/css/main.css';
pkg.scripts['watch-sass'] = 'node-sass -w src/public/css/main.scss dist/public/css/main.css';
pkg.scripts['tslint'] = 'tslint -c tslint.json -p tsconfig.json';
pkg.scripts['copy-static-assets'] = 'ts-node copyStaticAssets.ts';
pkg.scripts['debug'] = 'npm run build && npm run watch-debug';
pkg.scripts['serve-debug'] = 'nodemon --inspect dist/server.js';
pkg.scripts['watch-debug'] = 'concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve-debug\"';
fs.writeFileSync('./package.json', JSON.stringify(pkg, undefined, 2));
console.log('package.json updated');
" | $NODE
REF="4779cb7e182cf41d5c62289bb80d2850e0265b71"
FORK="Microsoft/TypeScript-Node-Starter"
for f in \
"copyStaticAssets.ts" \
"tsconfig.json" \
"tslint.json"
do
$DL "https://raw.githubusercontent.com/$FORK/$REF/$f"
done
sed -e 's;^shell;// shell;' -i copyStaticAssets.ts
DIR=src
mkdir -p $DIR && pushd $DIR
for f in \
"server.ts"
do
$DL "https://raw.githubusercontent.com/$FORK/$REF/$DIR/$f"
done
popd
DIR=src/util
mkdir -p $DIR && pushd $DIR
for f in \
"logger.ts" \
"secrets.ts"
do
$DL "https://raw.githubusercontent.com/$FORK/$REF/$DIR/$f"
done
grep -v "import { Logger }" logger.ts | tee logger.ts~ && mv logger.ts~ logger.ts
sed -e 's;new (Logger);winston.createLogger;' logger.ts | tee logger.ts~ && mv logger.ts~ logger.ts
head -n `grep -n "const prod" secrets.ts | cut -d: -f1` secrets.ts | tee secrets.ts~ && mv secrets.ts~ secrets.ts
popd
DIR=src/public/css
mkdir -p $DIR && pushd dir
touch main.scss
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment