Skip to content

Ready to script Node with Typescript

Notifications You must be signed in to change notification settings

towertop/ts-node-ready

Repository files navigation

Ready for scripting Node with Typescript

Introduction

This is a project template with various adjusted configurations. You can start scripting Node on the base with confidence. It targets to the last NodeJS version still under maintenance, meaning v12 and ES2019.

Setup and Usage

You can download the project without git index content:

curl -L https://github.com/towertop/ts-node-ready/tarball/master | tar xf -

After install dependencies, it is ready to open editor and start programing from src/main.ts.

If you're going to commit or publish the project, you need to modify package.json fields like name, version, author and etc.

You can use the simple workflow with:

npm run <command> 

Commands include:

startrun production version, depends on build
buildbuild production version, output to dist/
cleanremove dist/
devrun developement version main.ts directly
watchautomatically restart main.ts
lintcheck all source code for bad practices
lintFixmanage to correct the bad practices
checkTypinguse tsc check the typing of scripts
testexecute test cases
testWatchautomatically re-execute test cases
releaserelease current version as a git tag
buildDockerdeliver the app as a docker image

After all, this is a template, you can modify anything on demand.

Advices

  1. Make your editor support TS type check and code lint.
  2. Use new ES2019 syntax as more as you can.
  3. Update @types/node if targeting to a fresher NodeJS than v12.

Details

  1. Defines a consistent indent style in .editorconfig.
  2. Declares target node version range by engines field in package.json.
  3. Uses officially recommended TSConfig base from tsconfig/bases project, which specified type check for ES2019.
  4. Installs @types/node@12 to specify type check for Node API of that version.
  5. Tunes TSConfig to transpile ESModules to CommonJS for Node. Current Node has't been ready for native ESModules without flag and .mjs file extension.
  6. Includes TSLint and its autofix function. In tslint.json turn on the tslint:recommended presets and additional rules from tslint-eslint-rules. I added some personal adjustments after a blank line at the rules section in that file.
  7. Adds basic Unit Test methods with Mocha and Chai. I prefer BDD style interfaces and assertions with expect() function.
  8. Has double TS configuration tsconfig.app.json and tsconfig.spec.json like Angular projects. The TS lang server reference all @types/* module's declarations when you coding in supporting editors. But the compiler need to select some of them for strict type checking.
  9. Has a simple development workflow by scripts section in package.json. Use nodemon to have a live restart or retest when you coding.
  10. Utilizes husky and lint-staged to enforce code style before every commits.
  11. Has a simple relase method within the workflow based on npm-version command. It would lint and test source code, build production version, and release them as a git tag. I feel it is handy for private projects within a team internally. What you should do before use release command is to bump the version in package.json, else would get stuck by tag name collision.
  12. Use rollup.js to bundle and emit main.js while keeping readable javascript code and sourcemap for debugging.
  13. Use Dockerfile to build and deliver the app as an image.

Roadmap

  1. Collect common Node program patterns as references.
  2. Utilize code generators like Yeoman, Schematics and etc.
  3. Introduce zeit/pkg.
  4. Introduce node-config.
  5. Replace tslint with typescript-eslint.
  6. Upgrade all deps with builtin compatibility checks
  7. Introduce schematic-release?
  8. Bring in npm-check-updates tool to confidentially bump dependencies.