1.2.0-c9e6aafe • Published 7 years ago

ts-creator v1.2.0-c9e6aafe

Weekly downloads
9
License
MIT
Repository
-
Last release
7 years ago

Build Status NPM version

👉 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

optiondescriptiontypedefault
--colorcolorful printbooleanfalse
--output, -ooutput to filepathstringundefined
--version, -vshow ts-creator versionbooleanfalse
--help, -hshow helpbooleanfalse

TODO:

  • JSDoc
1.2.5-f4d886b1

7 years ago

1.2.5-add0acdf

7 years ago

1.2.5-3c37a4e4

7 years ago

1.2.5-c7538302

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.4-10a53083

7 years ago

1.2.3

7 years ago

1.2.2-56ba5445

7 years ago

1.2.2-f11a2fcd

7 years ago

1.2.2-9fa5bcf3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.1-b8ea51d5

7 years ago

1.2.0-30689c25

7 years ago

1.2.0-e4c967ca

7 years ago

1.2.0-9abe862a

7 years ago

1.2.0-f44096c2

7 years ago

1.2.0-8a3b6645

7 years ago

1.2.0-cd075c66

7 years ago

1.2.0-bb3529f6

7 years ago

1.2.0-397de79a

7 years ago

1.2.0-173bb449

7 years ago

1.2.0-236b2d3a

7 years ago

1.2.0-d45f071b

7 years ago

1.2.0-d68f208c

7 years ago

1.2.0-67926fd3

7 years ago

1.2.0-c9e6aafe

7 years ago

1.2.0-6d8a15bd

7 years ago

1.2.0-20ca5be3

7 years ago

1.2.0-402e3359

7 years ago

1.2.0

7 years ago

1.1.10-be9c1788

7 years ago

1.1.10-55a4fbb5

7 years ago

1.1.10-9b22d5ac

7 years ago

1.1.10-f02673ef

7 years ago

1.1.10-c41a68d5

7 years ago

1.1.10-69ce820f

7 years ago

1.1.10-873cc816

7 years ago

1.1.10-10f5e592

7 years ago

1.1.10-874750a7

7 years ago

1.1.10

7 years ago

1.1.8

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.12

7 years ago

1.0.10

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago