Implement Generic Service TypeScript

Prerequisites

Node.js

Currently, only Node.js 20 from version 20.11.1 is supported.

Further information: Node.js

Download link: Node.js Version20.x

TypeScript

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

Further information: TypeScript

ESLint

A tool used to check the quality of the code.

Further information: ES Lint

EditorConfig

This is used to overwrite the settings of the user workspace in VS Code (IDE).

Further information: VS Code Marketplace

Running/building projects

Create a new project

Open Solution Designer and create a new project by clicking on the Create button and select Generic Service Projects (TypeScript). Provide additional information required and click on Create. Once this is created the project should appear in the Solution Designer. Optional: Open that project in Solution Designer and write a proper documentation.

Clone Git project to local machine

In order to build and run the project, the Git project should be cloned to the user's workspace. In order to check out a Generic Service Project open the project after creation, navigate to the General Information and copy the Git Repository URI.

  1. Alter the URL and add the Git user, so it looks like the following:

    • Before: https://auto-devops-url/managed-solutions/SOLTS.git
    • After: https://git@auto-devops-url/managed-solutions/SOLTS.git
  2. Open a terminal and clone the project. You will be prompted for a password. Please use your personal access token as password:

    git clone https://git@auto-devops-url/managed-solutions/SOLTS.git
Note:

In order to be able to interact with the Git repository you have to provide the credentials for your Git system in advance.

Install dependencies

In order to install all the needed dependencies to run a Generic Service Project (TypeScript) on a local machine this command needs to be run in the project directory:

npm install

Compile for Generic Service Projects (TypeScript)

In order to be able to run TypeScript projects in Node.js, the TypeScript source code is transpired to JavaScript. In the tsconfig.json file, dist is set as the output directory. This directory should not be changed, as it is transferred to the Docker image during project pipeline execution.

To run the compilation of the sources on a local computer, the following command can be executed in the project directory:

npm run compile

Build for Generic Service Projects (TypeScript)

During the execution of the project pipeline, the compilation step is performed automatically. By adding or changing the build script in the package.json file, this step can be customised. This can be used, for example, for the build commands of frontend frameworks or to add additional files to the dist directory.

To run a build on a local computer, the following command can be executed in the project directory:

npm run build

If you also need a build script for local use, we recommend creating a script with the name build:local in the package.json file. This could then be called with the following command:

npm run build:local

Unit tests for Generic Service Projects (TypeScript)

It's highly recommended writing unit tests for your TypeScript projects. The unit tests will be executed during the project pipeline execution.

By default, the unit test step Enable Unit Test Execution is disabled

But once you enable Enable Unit Test Execution, the script (mentioned below) is ran. This script is configurable from package.json file in the repository.

npm run test:unit
Note:

We are using mocha and chai as test framework but in case you need to use different module you have to add those modules in package.json and use it in your test files.

Lint for Generic Service Projects (TypeScript)

To execute the linking of the sources on a local computer, the following command can be executed in the project directory:

npm run lint

If the linter should also try to fix problems automatically:

npm run lint:fix

Reset for Generic Service Projects (TypeScript)

Sometimes it can be useful to reset a Generic Service Project(TypeScript). This involves deleting the node_modules and the dist directory and running an npm install. To execute the reset, the following command can be executed in the project directory:

npm run reset

Health Check with Readiness

To verify that a container in a pod is healthy and ready to handle traffic, there are several mechanisms. If it is determined that the Pod is not healthy, it is restarted. Currently, the health status check is done by checking if the file /dist/src/index.js exists. By default, this file is created in the build process from the src/index.ts file. If you deviate from this case, you must ensure that this file is present after a build process, otherwise the Pod cannot start.

Start for Generic Service Projects (TypeScript)

There are several ways to start a Generic Service Project (TypeScript) in the cluster.

First, it is checked whether a start script is specified in the package.json file. If this exists, it is called. All commands required for the start can be stored in the start script.

To start, call out the following command:

npm run start

If you also need a start script for local use, we recommend creating a script with the name start:local in the package.json file. This could then be called with the following command:

npm run start:local

If there is no start script in the package.json file, it is checked whether there is a file with the name start-server.sh in the root directory. If this file exists, it will be used for the start.

If there is neither a start script in the package.json file nor a start-server.sh file, the command * node dist/src/index.js* is used.