Skip to content

devmentor416/dm-tools

Repository files navigation

Dev Mentor Project Creator Tool

Travis Version License Greenkeeper badge

Image Logo

  1. Dev Mentor Project Creator Tool
    1. Introduction
    2. No IDE: Build and Debug like a Pro
    3. Configuration: run, debug and build entry files
    4. Project Types
    5. Installing DM-Tools
    6. Benefits
    7. Creating a Project
      1. NPM Scripts
    8. Running a Node.js program
    9. Running Tests
      1. Developing and watching
        1. Watching Tests
    10. TypeScript development
      1. Project structure
      2. Building
      3. Warning
      4. Library code (Modules)
      5. Formatting the code
      6. Linting
      7. Testing
    11. Static Web development
      1. Browsersync Asset fetching
    12. Test coverage
    13. Create a Node.js JavaScript project
      1. TypeScript Node ES5
    14. Building C++ Testable Projects
      1. Basic Usage
        1. Project "hello_world" Creation
      2. Building Project
      3. Running hello_world program
      4. Running the Test Program
      5. Micro Test - Testing Your Project
    15. TypeScript Coding Guideline
    16. Debugging

Introduction

DM-Tools is a command-line utility for generating a project for the following programming languages.

  1. Basic JavaScript
  2. TypeScript
  3. JavaScript using Babel 7
  4. C++

Focus has been put into encouraging the use of best practices and the best tools.

Version of Node supported: Node v14.16.1+, for version earlier you will need to polyfill using "core-js".

No IDE: Build and Debug like a Pro

You can easily run and debug any TypeScript, JavaScript, Node.js program with ease. You do not need and IDE to debug! 💪🏽 All you need is a terminal and a browser and you can step through any code.

It is even possible to debug remotely, for more information, read the Node Debugging Guide.

To get started open a terminal and type:

npm run debug

This will start a debug build for TypeScript files then run it using Node.js and immediately break. JavaScript program will run immediately and break.

Next open a browser.

Browser Debug URL
Brave brave://inspect
Chrome chrome://inspect
Edge edge://inspect

Look for an "Open dedicated DevTools for Node" or "inspect" link.

Brave inspect

Clicking on either link will popup a browser debug window. You are good to go. ✅

Set breakpoints 🎯 and step through code like a pro. 😎

browser debugger

NOTE: TypeScript file(s) will appear in the debugger windows, not transpiled JavaScript files. ❤️

Configuration: run, debug and build entry files

The generated project haves two entries in package.json file that control:

  1. The build entry file.
  2. The run and debug entry file.

Here is sample package.json file:

{
  "name": "@dm-tools/demo",
  "version": "0.1.0",
  "description": "DM-Tools generated TypeScript Express.js demo Server with static Website",
  "main": "src/core/main.ts",
  "config": {
    "main": "build/core/main.js",
    "doc_folder": "docs/typedoc"
  },
...
}

The "main" property under the root, controls the entry file and location for the TypeScript built.

The property "config.main" is where the TypeScript code is compiled to, it points to the entry file to run.

Should you even have a need to rename the entry files, this is where you would do it. For JavaScript projects, you will see duplicate entries both pointing to the run file.

Project Types

The following basic project types that can be created using DM-Tools are:

Option Language Description
TS Default (TypeScript Node.js program)
js JS JavaScript Node.js server with static website
ts TS TypeScript Express.js Server (Zero compile with static Website)
web HTML Sass Static Website
node TS TypeScript Node.js Server
koa TS Koa Node.js API Server in TypeScript
gql JS Apollo GraphQL API Server using Babel 7
cpp CPP C++ with Micro Test using CMake

Installing DM-Tools

npm install -g dm-tools

Benefits

Here are the benefits you will enjoy right out of the gate:

  • ✅ Quick start
  • ✅ Best practices
  • ✅ Build system
  • ✅ Code in TypeScript
  • ✅ Easy no IDE debugging
  • ✅ HTML, CSS & Sass live edit and preview
  • ✅ Error logging
  • ✅ Code linting
  • ✅ Code formatting
  • ✅ Unit testing
  • ✅ Code coverage
  • ✅ Document generation
  • ✅ Git commit hooks

Creating a Project

Start playing with the demo starter project now, the generated source code is located in the project "src/" sub-folder.

dm new demo
cd demo

Note: If you are using Yarn over NPM, continue to work with Yarn, the DM-Tools generated project will use Yarn before NPM if it is available on your system.

NPM Scripts

The following NPM commands are provided, some are project type dependent.

NPM script Description
build Perform a release build
dev Run in watch mode.
doc Generate doc files (jsdocs).
format Format the source code.
lint Run TypeScript / ES linter
node:debug Start debugger, requires Chrome.
start Run the Node.js program.
test Run Unit test with coverage
test:e2e End to end test with Cypress
testwatch Run Unit testing in watch mode
web Run static web server

Running a Node.js program

To simply run a production build of the Node.js program, type:

npm start

This will perform a clean build and run the demo program from the "build/" folder. The demo application log will be produced in the "logs/" sub-folder under the project root.

Running Tests

To run tests with coverage for the TypeScript / Node.js based programs, type:

npm test

It might be a good idea to run all the tests with coverage before a production build.

Developing and watching

You can run and watch a Node.js based program during development. To do this, open a terminal and type the following.

npm run dev

This will kickoff the "dev build" and "dev watch" NPM scripts.

Watching Tests

To watch the unit tests while developing, type the following command in a new terminal.

npm run testwatch

TypeScript development

Project structure

Place all TypeScript source code under the folder, "src/", they will be picked up from here and compiled to the, "build/" folder under the project root.

You are free to create addition folders and sub-folders under, "src/", the compiler will recursively find and compile all TypeScript code.

All TypeScript code is compiled to ES2020 JavaScript. The target JavaScript code can be changed from the TypeScript configuration file, "tsconfig.json".

Some of the things you may want to configure:

  • Files to compile
  • Folders to include
  • Folders to exclude
  • Target compiled output
  • Source map (Needed for debugging)
  • Module system (Use commonjs for Node)
  • Output file

Supported compiled targets include:

ES3, ES5, ES6, ES2016, ES2017, ES2018, ES2019, ES2020

See compiler options for more details.

Building

To compile the TypeScript code, use the following command to start the build process:

npm run release

Warning

The "build/" folder and all sub-folders within it will be deleted to insure a clean build is performed each time. Do not place any files you will need later in the "build/" folder.

Library code (Modules)

Place any module or library source code that you write under the, "src/lib/", sub-folder. The compiled source code will be output to the, "build/lib/", sub-folder.

Formatting the code

The source code will be automatically formatted during development and production build. This will also happen before source code is committed to Git.

It is good practice to format the source code, so it conforms to a uniform structure. Avoid squabbles about style. Formatting in run by the NPM script, "format".

Linting

To validate the project TypeScript source code, use the following command:

npm run lint

Note: The TypeScript source code is run through a linter (ESLint) before a build and before it is committed to the Git repository. Any errors encountered must be fixed before the Git commit is allowed to proceed.

Important!: I have noticed, once in a while the git hooks will continue to fail when there is nothing really wrong. If you suspect this is the case, the easy fix is to delete the "node_modules" folder. Follow this with a "npm install" or simply type "yarn" and then try to commit or push the code again.

Testing

Testing is done using Ava, the test methods are simple and easy to learn.

Ava makes testing simple. Code is easy to read since it is just JavaScript, this avoids the need to context switch to BDD syntax. Plus anyone who knows JavaScript will be able to write test code immediately.

I firmly believe less time should be put into writing test code and having more time to write production code. Ava delivers on this by keeping the setup and test writing to a bare minimum. I believe Ava is the best option for writing unit test for JavaScript based code.

The test code should be co-located with the production source code. As a best practice, place tests under a sub-folder called "test/".

Pay attention to how the test source file is named: "test.<file>.ts". So if you have a file called, "filter.ts", the test file should be named, "test.filter.ts".

To run the test, type:

npm test

Note: Running the test will cause a fresh build to be kicked-off. Once the build finishes, all the unit tests will be run.

Static Web development

If you want to hack around with HTML, Sass and try things out quick. Start the project in web mode using the following command:

npm run web

This will run the build first and then open a web browser on port 3000, and load the HTML page, "index.html" located in the "src/" sub-folder.

Any changes made to "index.html" will automatically update and browser on save. You do not need to keep hitting refresh on the browser.

The website uses lite-server, which is based on Browsersync to run a local development web-server and keeps all browsers listening to it in sync. This means it is possible to have multiple browsers listening to the server.

On how to configure the setup, read the Browsersync options.

Basic configurations setting you may be interested in are:

  • files
  • server
  • proxy
  • logLevel
  • port

The default Browsersync UI web address is: http://localhost:3001/.

Browsersync Asset fetching

With Browsersync, having to serve addition CSS and JavaScript files, make sure to add their path in routes. Something similar to like this:

  "server": {
    baseDir: "src",
    routes: {
      "/node_modules/tachyons/css":"node_modules/tachyons/css"
    }
  },

This will allow including "<script>" assets from the index.html file like this:

<head>
  <link rel="stylesheet" href="./node_modules/tachyons/css/tachyons.min.css">
</head>

Test coverage

Test coverage is done when test is run using "nyc". The test coverage result is displayed to the console after the results of the unit tests. A folder called "coverage/" will be created under the project root. It will hold the results of the code coverage from the test run. Of interest to you will be the HTML report. It is a nice way to see what code was covered and what code was not by the unit tests.

To configure the test coverage, make changes to the "nyc" settings found in the file "package.json".

Create a Node.js JavaScript project

If you want to develop in plain JavaScript, or develop a ES6 Node.js based project, this is now supported. It is also good for quickly testing out code and not getting slowed down by the compile step.

You will need the latest version of Node.js for ES6 and beyond support, otherwise plain JavaScript will continue to work.

dm new demo --type js
npm install

Note: You may also use "-t" which is the short-form for "--type".

The plain JavaScript generated file has a development mode. It will run the Entry file ("main.js") using Node.js each time the source code is updated. You can develop and see the output from the terminal to test out code quickly.

npm run dev

TypeScript Node ES5

If you need to use ES5 Node.js support with TypeScript here are the following change you need to make.

Make sure you're using Node v14.16.1 or higher.

Add the following two lines under compilerOptions to "tsconfig.json" and "tsconfig.test.json".

"compilerOptions": {
  "target": "es2020",
  "lib": [
    "es2020",
    "es2019",
    "es2018",
    "es2017",
    "dom"
    ]
}

Building C++ Testable Projects

DM-Tools can be used to create a C++ project with Unit Testing setup using Micro Test.

The C++ project uses CMake to generate cross-platform Makefiles for:

  1. Linux
  2. MacOS
  3. Windows

Basic Usage

Let us go through the steps of creating a simple "hello_world" project.

Project "hello_world" Creation

The created project will be found under the hello_world folder.

cd /tmp
dm new hello_world --cpp

Building Project

The Makefile is located under sub-folder build.

cd hello_world/build
make

Running hello_world program

The executable hello_world will be found in the build folder.

./hello_world

Running the Test Program

The test program is located under build/test/ sub-folder. Initially one failing test is created for you to follow.

./test/test.hello_world

Micro Test - Testing Your Project

DM-Tools creates a test sub-folder under src/ and uses the latest Micro Test header file, it basically pulls it from the Micro Test Git repository.

To learn more about how to write tests using Micro Test check out the project site. You will be amazed how simple and fast it is to write test code.

TypeScript Coding Guideline

Read the coding guideline found in the wiki.

Debugging

If you get a post or address is use, you might have a background process that didn't shutdown correctly. To shut it down, use the following command to find its process ID and then kill it.

lsof -i :PORT
lsof -i :3000

kill <PIDP>
kill -9 <PIDP>

Happy Hacking =)