Giter Club home page Giter Club logo

ts-creator's Introduction

ts-creator

A code generator to generate TypeScript code generator from TypeScript code

Build Status NPM version codecov

๐Ÿ‘‰ Try It! ๐Ÿ‘ˆ

How to use it:

npm install ts-creator

1. generate from code

import creator from 'ts-creator'

const generatedFactoryCode = creator(`const foo = "your code here"`)

2. transform source file

import { transformSourceFile } from 'ts-creator'

declare const file: ts.SourceFile
const factoryFile = transformSourceFile(file)

3. transform node

import { transformNode } from 'ts-creator'

declare const node: ts.Expression
const factoryNode = transformNode(node)

How does it work?

If you want to write a TypeScript codegen.

You got TypeScript code:

function foo(bar: number): number {
  return bar + 1
}

ts-creator generate TypeScript factory from given code to:

ts.createFunctionDeclaration(
  undefined,
  undefined,
  undefined,
  ts.createIdentifier('foo'),
  undefined,
  [
    ts.createParameter(
      undefined,
      undefined,
      undefined,
      ts.createIdentifier('bar'),
      undefined,
      ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
      undefined
    )
  ],
  ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
  ts.createBlock(
    [
      ts.createReturn(
        ts.createBinary(
          ts.createIdentifier('bar'),
          ts.createToken(ts.SyntaxKind.PlusToken),
          ts.createNumericLiteral('1')
        )
      )
    ],
    true
  )
)

Result after run the generated factory code:

function foo(bar: number): number {
    return bar + 1;
}

Cli usage

Use ts-creator cli to generate code:

ts-creator <input> [options]

Simple usage:

# print generate code
ts-creator foo.ts

# or read data from pipeline
echo 42 | ts-creator
cat foo.ts | ts-creator
ts-creator < foo.ts

# write to file
ts-creator foo.ts -o foo.js
ts-creator foo.ts > foo.js

Installation

You can install ts-creator globally.

npm i -g ts-creator
# or yarn
yarn global add ts-creator

If you install locally, may prepend npx or yarn you need.

# use npm
npm i ts-creator
npx ts-creator -h

# use yarn
yarn add ts-creator
yarn ts-creator -h

Cli options

option description type default
--target, -t generate targets Enum { expression, runnable, esmodule, commonjs } expression
--output, -o output to filepath String undefined
--tsx tsx support Boolean false
--color colorful print Boolean true
--semi print semicolons at the ends of statements Boolean false
--single-quote use single quotes instead of double quotes Boolean true
--jsx-single-quote use single quotes in JSX Boolean false
--bracket-spacing print spaces between brackets in object literals Boolean true
--tab-width pecify the number of spaces per indentation-level Number 2
--use-tabs indent lines with tabs instead of spaces Boolean false
--trailing-comma print trailing commas wherever possible Enum { none, es5, all } none
--prose-wrap how to wrap output Enum { always, never, preserve } preserve
--version, -v show ts-creator versions Boolean false
--help, -h show helper Boolean false

TODO:

  • JSDoc

ts-creator's People

Contributors

kingwl avatar hairyrabbit avatar angrypowman avatar blainehansen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.