Skip to content

Latest commit

 

History

33 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Test

Install

npm

npm install @wasm-fmt/shfmt

jsr.io

npx jsr add @fmt/shfmt

Usage

Node.js / Deno / Bun / Bundler

import { format } from "@wasm-fmt/shfmt";

const source = `#!/bin/bash
echo "hello world"
`;
const formatted = format(source);
console.log(formatted);

With options:

const formatted = format(source, "script.sh", {
	indent: 2,
	binaryNextLine: true,
	switchCaseIndent: false,
	spaceRedirects: true,
	funcNextLine: false,
	minify: false,
	singleLine: false,
	simplify: true,
});

Without a file path:

const formatted = format(source, {
	indent: 2,
	simplify: true,
});

With a reusable config handle:

import { createConfig, format, releaseConfig } from "@wasm-fmt/shfmt";

const config = createConfig({
	indent: 2,
	simplify: true,
});

try {
	const formatted = format(source, "script.sh", config);
	console.log(formatted);
} finally {
	releaseConfig(config);
}

Web

For web environments, you need to initialize WASM module manually:

import init, { format } from "@wasm-fmt/shfmt/web";

await init();

const source = `#!/bin/bash
echo "hello world"
`;

const formatted = format(source);
console.log(formatted);

Vite

import init, { format } from "@wasm-fmt/shfmt/vite";

await init();
// ...

Or use the ./bundler entry with vite-plugin-wasm

import { format } from "@wasm-fmt/shfmt/bundler";

Entry Points

  • . - Auto-detects environment (Node.js uses node, Webpack uses bundler, default is ESM)
  • ./node - Node.js environment (no init required)
  • ./esm - ESM environments like Deno (no init required)
  • ./bundler - Bundlers like Webpack (no init required)
  • ./web - Web browsers (requires manual init)
  • ./vite - Vite bundler (requires manual init)

Build from source

# 1. install Go https://go.dev/doc/install

# 2. install TinyGo https://tinygo.org/getting-started/install/

# 3. clone this repo
git clone https://github.com/wasm-fmt/shfmt.git

# 4. install dependencies inside the repo
npm

# 5. build
npm run build

# 6. test
npm run test:node

Credits

Thanks to:

About

A WASM Based Shell Script Formatter

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages