# TypeScript Formatter (tsfmt) [![Build Status](https://travis-ci.org/vvakame/typescript-formatter.svg)](https://travis-ci.org/vvakame/typescript-formatter) [![Dependency Status](https://david-dm.org/vvakame/typescript-formatter.svg?theme=shields.io)](https://david-dm.org/vvakame/typescript-formatter)

A TypeScript code formatter powered by TypeScript Compiler Service.

```bash
$ tsfmt

  Usage: tsfmt [options] <file ...>

  Options:

    -h, --help         output usage information
    -V, --version      output the version number
    -r, --replace      replace .ts file
    --no-tslint        don't read a tslint.json
    --no-editorconfig  don't read a .editorconfig
    --no-tsfmt         don't read a tsfmt.json
    --verbose          makes output more verbose
```

## Installation

```npm install -g typescript-formatter```

## Usage

```bash
$ cat sample.ts
class Sample {hello(word="world"){return "Hello, "+word;}}
new Sample().hello("TypeScript");
```

```bash
# basic. read file, output to stdout.
$ tsfmt sample.ts
class Sample { hello(word= "world") { return "Hello, " + word; } }
new Sample().hello("TypeScript");
```

```bash
# replace. read file, and replace file.
$ tsfmt -r sample.ts
replaced sample.ts
$ cat sample.ts
class Sample { hello(word= "world") { return "Hello, " + word; } }
new Sample().hello("TypeScript");
```

## Read Settings From Files

1st. Read settings from tsfmt.json.

```json
{
  "indentSize": 4,
  "tabSize": 4,
  "newLineCharacter": "\r\n",
  "convertTabsToSpaces": true,
  "insertSpaceAfterCommaDelimiter": true,
  "insertSpaceAfterSemicolonInForStatements": true,
  "insertSpaceBeforeAndAfterBinaryOperators": true,
  "insertSpaceAfterKeywordsInControlFlowStatements": true,
  "insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
  "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
  "placeOpenBraceOnNewLineForFunctions": false,
  "placeOpenBraceOnNewLineForControlBlocks": false
}

```

2nd. Read settings from .editoconfig ([editorconfig](http://editorconfig.org/))

```text
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
indent_style = tab
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
```

3rd. Read settings from tslint.json ([tslint](https://www.npmjs.org/package/tslint))

```json
{
  "rules": {
    "indent": [true, 4]
    "whitespace": [true,
      "check-branch",
      "check-operator",
      "check-separator"
    ]
  }
}
```

### Read Settings Rules

```
$ tree -a                                                                                       [~/Dropbox/work/typescript-formatter/example]
.
├── foo
│   ├── bar
│   │   ├── .editorconfig
│   │   └── buzz.ts
│   ├── fuga
│   │   ├── piyo.ts
│   │   └── tsfmt.json
│   └── tsfmt.json
└── tslint.json

3 directories, 6 files
```

1. exec `$ tsfmt -r foo/bar/buzz.ts foo/fuga/piyo.ts`
2. for foo/bar/buzz.ts, read foo/tsfmt.json and foo/bar/.editorcondig and ./tslint.json
3. for foo/fuga/piyo.ts, read foo/fuga/tsfmt.json and ./tslint.json
