<repo_name>alopatindev/scalafmt-server<file_sep>/README.md
HTTP server to format Scala code from text editors (like vim)

[![Build Status](https://api.travis-ci.org/alopatindev/scalafmt-server.svg?branch=master)](https://travis-ci.org/alopatindev/scalafmt-server)

# Why
`scalafmt` starts slowly (as any JVM-based app) and [`nailgun`](http://scalameta.org/scalafmt/#Nailgun) is buggy and unmaintaned (?)

<p align="center"><img src="https://raw.githubusercontent.com/alopatindev/assets/master/weird-ng-behavior.png" width="838" height="340"></p>

That's sad

# Usage
Unpack and run the server
```sh
sbt universal:packageZipTarball
tar xzf target/universal/scalafmt-server-*.tgz
scalafmt-server-*/scalafmt-server.sh 8899 &
```

If you're using [vim-autoformat](https://github.com/Chiel92/vim-autoformat):
```viml
let g:formatdef_scalafmt = "'path/to/scalafmt-client.sh 8899'"
let g:formatters_scala = ['scalafmt']
au BufWrite *.scala :Autoformat
```

# License
Licensed under the terms of MIT (read LICENSE.txt for details)
<file_sep>/scripts/scalafmt-server.sh
#!/bin/sh

set -e

PORT=$1
CURRENT_DIR=$(dirname `readlink -f $0`)

if [[ $PORT -eq '' ]]
then
    echo 'Port is not specified'
    exit 1
fi

"${CURRENT_DIR}/bin/scalafmt-server" -Dhttp.port=${PORT} &
sleep 3s && echo 'object WarmUp { }' | "${CURRENT_DIR}/scalafmt-client.sh" "${PORT}" >> /dev/null
<file_sep>/scripts/scalafmt-client.sh
#!/bin/sh

PORT=$1
HOST='localhost'
INPUT='/dev/stdin'

if [[ $PORT -eq '' ]]
then
    echo 'Port is not specified'
    exit 1
fi

curl -s --upload-file "${INPUT}" "http://${HOST}:${PORT}/" || cat "${INPUT}"
