Writing Command Line Applications with iTwin.js

By Matt Gooding

iTwin.js
iTwin.js
2 min readJun 1, 2020

--

My favorite thing about working with iTwin.js is the speed and simplicity of writing small programs to understand and manipulate the data in design files. I tend to write and iterate on lots of tiny command line applications when debugging. I also think these applications are the easiest way to become acquainted with iTwin.js and TypeScript when coming from a traditional desktop software background.

This blog post contains a skeleton that can be used to quickly bootstrap a command-line iTwin.js application. The only prerequisite is the appropriate version of Node.js.

Start by creating a new directory for the application. In the root of the directory, create a new file named package.json. This file defines your dependencies and other information about the application.

package.json

In the same directory, create tsconfig.json and tslint.json. These files configure the TypeScript compiler to use the same settings as iTwin.js.

tsconfig.json

tslint.json

With these files in place, you can run npm install from the command line in your application's directory to install iModel.js and its dependencies.

Finally, you need to add code to execute. Create a src directory and a file named Main.ts under it. Your final directory structure should look like this:

The example program below will dump all the ID and name for all Category elements in the iModel. This program requires a Snapshot iModel. You can download a sample one here.

Main.ts

Enter npm run build from the command line in your application's directory to compile your application, then node lib\Main.js --input=demo.bim to run your application.

--

--