Skip to content

kevinbai0/tscgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tscgen

A (WIP) collection of tools for Typescript code generation.

Note: The docs for this project are still incomplete and likely to change

Why?

  • Code generation is an extremely powerful tool but it's hard to leverage unless there's an off-the-shelf solution readily available.
  • It's really hard to create your own code generation pipeline that isn't a jumble of string concatenation and messy logic.
  • It takes a lot of time to create and maintain code generation projects

Goals

Simplify Typescript code generation by defining interfaces, types, and variables declaratively.

  • Generate type definitions and Javascript objects based on JSON/YAML files or any other data source.
  • Make it easy to use custom code generation in any project.
  • Make maintaining, updating, and understanding code generation projects easy.

Sample Usage

import tscgen from 'tscgen';

const ISampleInterface = tscgen
  .interfaceBuilder('ISampleInterface')
  .markExport()
  .addBody({
    type: tscgen.stringType(),
    data: tscgen.numberType(1, 2, 3, 4)
    inputs: tscgen.booleanTuple(true, false)
  });

console.log(ISampleInterface.toString());

// output (after linted)

export interface ISampleInterface {
  type: string;
  data: 1 | 2 | 3 | 4;
  inputs: [true, false]
}

Projects

The tscgen monorepo contains a number of different projects and packages which are located under the packages folder.

Project Description
tscgen The core package for constructing type definitions and variables declaratively
tscgen-framework A small framework for building & maintaining code-generation projects.
tscgen-cli CLI-tool for tscgen-framework to build code-generation projects.
tscgen-bootstrap A create-react-app-like tool for bootstrapping a tscgen-framework project
framework-example An example project that parses OpenAPI3 spec and generates FE code.

Getting Started

The tscgen-framework is by far the best & easiest way to create useful code generation projects. If you only want to generate a few interfaces or types, refer to the core tscgen package.

There are 2 primary types of projects for code generation:

  1. Standalone project to be published as a package (other projects consume this package).
  2. As a subfolder inside of a single-project directory

For the Getting Started Guide, we'll assume that you're creating a standalone project.

Creating your project

With npx (recommended)

npx tscgen-bootstrap -p my-project

This configures everything you need for your project including typescript, eslint, npm scripts, tscgen dependencies, and your initial project setup.

Build your project

Generate code into the ./out folder

npm run generate

Compile your ./out folder to ./dist with typescript

npm run build

Documentation

This section outlines detailed usage of both tscgen and tscgen-framework. A complete overview of the API can be found at the API Reference.

tscgen-framework

Docs

tscgen

Docs

About

A (WIP) collection of tools for Typescript code generation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published