Skip to main content

sane-fmt

Opinionated code formatter for TypeScript and JavaScript.

Usage in Deno

  • Deno.version.deno: 1.8.0
  • Deno.version.v8: 9.0.257.3
  • Deno.version.typescript: 4.2.2

Command Line Interface

Installation

deno install \
  --unstable \
  --allow-read \
  --allow-write \
  --allow-env \
  --name=sane-fmt \
  https://deno.land/x/sane_fmt@0.8.6/main.js

CLI Usage

Environment Variables:

  • SANE_FMT_DENO_PREOPENS: A list of preopened directories. Its delimiter is colon (:) on Linux/Unix and semicolon (;) in Windows.

Usage:

sane-fmt 0.8.6
Opinionated code formatter for TypeScript and JavaScript

USAGE:
    sane-fmt [FLAGS] [OPTIONS] [files]...

FLAGS:
    -h, --help
            Prints help information

        --hide-passed
            Do not log passed filenames

        --stdio
            Reads unformatted code from standard input, prints formatted code to standard output, then exits

    -V, --version
            Prints version information

    -w, --write
            Whether to write or check


OPTIONS:
        --color <color>
            When to use terminal color [default: auto]  [possible values: auto, never, always]

        --details <details>
            File diff detail [default: name]  [possible values: count, name, diff]

    -I, --include <list>
            Files whose contents contain paths to target files (`-` means stdin, other strings mean text file)

        --log-format <log-format>
            Format of log messages [default: human]  [possible values: human, github-actions]


ARGS:
    <files>...
            Files to process

            If none are provided, a default set of files will be assumed

Programming Interface

Example: Format a file

import Context from 'https://deno.land/std@0.91.0/wasi/snapshot_preview1.ts'
import { u8v } from 'https://deno.land/x/sane_fmt@0.8.6/index.ts'
const context = new Context({
  args: ['sane-fmt', 'example-directory/example-file.ts'],
  stdin: Deno.stdin.rid,
  stdout: Deno.stdout.rid,
  stderr: Deno.stderr.rid,
  preopens: {
    'example-directory': 'example-directory',
  },
})
const module = await WebAssembly.compile(u8v)
const instance = await WebAssembly.instantiate(module, {
  wasi_snapshot_preview1: context.exports,
})
const status = context.start(instance)
if (status === null) {
  throw new Error('Failed to execute sane-fmt')
}
if (status !== 0) {
  throw new Error(`Program exits with code ${status}`)
}

Example: Read unformatted input and print formatted output

import Context from 'https://deno.land/std@0.91.0/wasi/snapshot_preview1.ts'
import { u8v } from 'https://deno.land/x/sane_fmt@0.8.6/index.ts'
const context = new Context({
  args: ['sane-fmt', '--stdio'],
  stdin: Deno.stdin.rid,
  stdout: Deno.stdout.rid,
  stderr: Deno.stderr.rid,
})
const module = await WebAssembly.compile(u8v)
const instance = await WebAssembly.instantiate(module, {
  wasi_snapshot_preview1: context.exports,
})
const status = context.start(instance)
if (status === null) {
  throw new Error('Failed to execute sane-fmt')
}
if (status !== 0) {
  throw new Error(`Program exits with code ${status}`)
}

License

MIT © Hoàng Văn Khải