# ts-creator

> A code generator to generate TypeScript code generator from TypeScript code

Latest version **1.2.5** (published 2019-03-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-creator
pnpm add ts-creator
yarn add ts-creator
bun add ts-creator
```

Provides the command `ts-creator`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; large bundle.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.5 |
| Published | 2019-03-19 |
| First published | 2019-01-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 21.3 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | kingwl |
| Maintainers | kingwl |

## Links

- npm: https://www.npmjs.com/package/ts-creator
- npm.io page: https://npm.io/package/ts-creator

## Dependencies (5)

- [yargs](https://npm.io/package/yargs.md) ^12.0.5
- [cardinal](https://npm.io/package/cardinal.md) ^2.1.1
- [prettier](https://npm.io/package/prettier.md) ^1.16.0
- [get-stdin](https://npm.io/package/get-stdin.md) ^6.0.0
- [typescript](https://npm.io/package/typescript.md) ^3.4.0-dev.20190313

## Recent versions

- 1.2.5 (latest) — 2019-03-19
- 1.2.5-f4d886b1 (next) — 2019-04-10
- 1.2.5-add0acdf — 2019-03-21
- 1.2.5-3c37a4e4 — 2019-03-21
- 1.2.5-c7538302 — 2019-03-19
- 1.2.4 — 2019-03-13
- 1.2.4-10a53083 — 2019-03-13
- 1.2.3 — 2019-03-13
- 1.2.2-56ba5445 — 2019-03-13
- 1.2.2-f11a2fcd — 2019-02-22
- 1.2.2-9fa5bcf3 — 2019-02-15
- 1.2.2 — 2019-02-15
- 1.2.1 — 2019-02-14
- 1.2.1-b8ea51d5 — 2019-02-14
- 1.2.0-30689c25 — 2019-02-14
- … 41 more at https://npm.io/package/ts-creator/versions

## README

<div align="center">
  <img width="520" src="./media/Logo.svg">
  
  # ts-creator 
  
  A code generator to generate TypeScript code generator from TypeScript code

  [![Build Status](https://travis-ci.com/HearTao/ts-creator.svg?branch=master)](https://travis-ci.com/HearTao/ts-creator)  [![NPM version](https://img.shields.io/npm/v/ts-creator.svg)](https://www.npmjs.com/package/ts-creator)   [![codecov](https://codecov.io/gh/HearTao/ts-creator/branch/master/graph/badge.svg)](https://codecov.io/gh/HearTao/ts-creator)

  👉 [Try It!](https://ts-creator.js.org/) 👈
</div>


## How to use it:

```shell
npm install ts-creator
```


### 1. generate from code

```ts
import creator from 'ts-creator'

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

### 2. transform source file

```ts
import { transformSourceFile } from 'ts-creator'

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

### 3. transform node

```ts
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: 

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

ts-creator generate TypeScript factory from given code to:

```ts
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: 

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

## Cli usage

Use `ts-creator` cli to generate code:

```sh
ts-creator <input> [options]
```

Simple usage:

```sh
# 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.

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

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

```sh
# 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

---
_Source: https://npm.io/package/ts-creator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
