TypeScriptPP
Example project showing how to embed the TypeScript scripting language in a C++ project with automatically generated declarations.
Motivation
Lua is a great embedded scripting language for C++ projects. Unfortunately, Lua code becomes unmaintainable as the project grows, due to the absence of reliable static type checkers. In the JavaScript world this was solved a long time ago with through the TypeScript language. Thanks to the great work of TypeScriptToLua project, it is now possible to use TypeScript for Lua projects. This project demonstrates how to use CMake to bring TypeScript, Lua and C++ together and can be used as inspiration or a starting point for further projects.
Usage
git clone https://github.com/TheLartians/TypeScriptPP.git
cd TypeScriptPP
cmake -H. -Bbuild
cmake --build build -j4 # compiles C++, updates declarations and transpiles TypeScript
./build/TypeScriptPP -d # print TypeScript declarations
./build/TypeScriptPP -s lua # runs the transpiled lua script lua/index.lua
How it works
The C++ project is contained in source/main.cpp which contains the C++ library and a Lua interface. It also defines a simple CLI for generating and printing the declarations and executing a lua script.
The CMake script uses CPM and npm to download and configure all dependencies in the current directory. After building the C++ code, CMake uses the executable to update the TypeScript declarations and then transpiles the TypeScript code to Lua with TypeScriptToLua.
The script also adds package.json
and the TypeScript sources to the dependency to automatically re-invoke TypeScriptToLua on changes.
The TypeScript sources are collected using GLOB
, so be sure to re-run cmake
after adding new source files.
Additional commands
Recompile TypeScript without invoking CMake
npm run build
Manually update the declarations
cmake --build build --target update-declarations