ts-creator v1.2.0-c9e6aafe
👉 Try It! 👈
How to use it:
npm install ts-creator1. 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.jsInstallation
You can install ts-creator globally.
npm i -g ts-creator
# or yarn
yarn global add ts-creatorIf 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 -hCli options
| option | description | type | default |
|---|---|---|---|
| --color | colorful print | boolean | false |
| --output, -o | output to filepath | string | undefined |
| --version, -v | show ts-creator version | boolean | false |
| --help, -h | show help | boolean | false |
TODO:
- JSDoc
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago