https://github.com/un-ts/sh-syntax
A WASM shell parser and formatter with bash support, based on `mvdan/sh`
https://github.com/un-ts/sh-syntax
go golang shell shell-script shellscript tinygo wasm
Last synced: 3 months ago
JSON representation
A WASM shell parser and formatter with bash support, based on `mvdan/sh`
- Host: GitHub
- URL: https://github.com/un-ts/sh-syntax
- Owner: un-ts
- License: mit
- Created: 2022-04-01T01:23:01.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-08T03:07:08.000Z (3 months ago)
- Last Synced: 2025-04-10T06:28:03.909Z (3 months ago)
- Topics: go, golang, shell, shell-script, shellscript, tinygo, wasm
- Language: TypeScript
- Homepage: https://npm.im/sh-syntax
- Size: 6.35 MB
- Stars: 16
- Watchers: 0
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# sh-syntax
[](https://github.com/un-ts/sh-syntax/actions/workflows/ci.yml?query=branch%3Amain)
[](https://codecov.io/gh/un-ts/sh-syntax)
[](https://github.com/plantain-00/type-coverage)
[](https://coderabbit.ai)
[](https://www.npmjs.com/package/sh-syntax)
[](https://github.com/un-ts/sh-syntax/releases)[](https://conventionalcommits.org)
[](https://renovatebot.com)
[](https://standardjs.com)
[](https://github.com/prettier/prettier)
[](https://github.com/changesets/changesets)A WASM shell parser and formatter with bash support, based on [mvdan/sh](https://github.com/mvdan/sh)
## TOC
- [Usage](#usage)
- [Install](#install)
- [API](#api)
- [node](#node)
- [browser](#browser)
- [Benchmark](#benchmark)
- [Sponsors](#sponsors)
- [Backers](#backers)
- [Changelog](#changelog)
- [License](#license)## Usage
### Install
```sh
# yarn
yarn add sh-syntax# npm
npm i sh-syntax
```### API
#### node
```js
import { parse, print } from 'sh-syntax'const text = "echo 'Hello World!'"
const ast = await parse(text)
const newText = await print(ast, {
// `originalText` is required for now, hope we will find better solution later
originalText: text,
})
```#### browser
```js
import { getProcessor } from 'sh-syntax'// choose your own way to load the WASM file
// with `fetch`
const processor = getProcessor(() =>
fetch('http://cdn.jsdelivr.net/npm/sh-syntax@latest/main.wasm'),
)// with `wasm?init` via `vite`
import initWasm from 'sh-syntax/wasm?init'
const processor = getProcessor(initWasm)const parse = (text, options) => processor(text, options)
const print = (textOrAst, options) => {
if (typeof textOrAst === 'string') {
return processor(textOrAst, {
...options,
print: true,
})
}
return processor(textOrAst, options)
}// just like node again
const text = "echo 'Hello World!'"
const ast = await parse(text)
const newText = await print(ast, { originalText: text })
```## Benchmark
```console
clk: ~2.81 GHz
cpu: Apple M1 Max
runtime: node 18.20.8 (arm64-darwin)benchmark avg (min … max) p75 / p99 (min … top 1%)
------------------------------------------- -------------------------------
sh-syntax 18.33 ms/iter 18.56 ms ▃ ▃█
(17.48 ms … 20.83 ms) 19.09 ms ▂▂█ ▂ ██▂ ▂ ▂
( 28.52 kb … 9.82 mb) 890.46 kb ███▆▆▁▁█▁▁▆███▁▆█▁█▆▆
5.07 ipc ( 1.22% stalls) 99.36% L1 data cache
56.96M cycles 288.57M instructions 39.27% retired LD/ST (113.32M)sh-syntax (synckit) 18.38 ms/iter 18.82 ms ▂▂▂ ▂▂ █▂
(17.63 ms … 19.06 ms) 19.04 ms ███▅ ██ ██▅▅
(291.31 kb … 312.45 kb) 292.09 kb ████▇▇▇▇▁▁▁▁▇▁██▇████
1.11 ipc ( 11.51% stalls) 40.34% L1 data cache
434.86k cycles 484.78k instructions 19.58% retired LD/ST ( 94.92k)mvdan-sh 79.47 ms/iter 78.95 ms █
(71.22 ms … 126.09 ms) 80.84 ms ▅█ ▅▅▅ ▅ ▅▅▅ ▅
( 8.18 mb … 9.70 mb) 8.83 mb ██▁███▁▁▁▁█▁▁▁▁███▁▁█
5.18 ipc ( 1.05% stalls) 99.04% L1 data cache
249.41M cycles 1.29G instructions 34.41% retired LD/ST (444.42M)summary
sh-syntax
1x faster than sh-syntax (synckit)
4.34x faster than mvdan-sh
```See [benchmark](./benchmark/benchmark.txt) for more details.
You can try it with running `node benchmark` by yourself. [Here](./benchmark/index.js) is the benchmark source code.
[](https://github.com/sponsors/JounQin)
## Sponsors
| 1stG | RxTS | UnTS |
| ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| [](https://opencollective.com/1stG) | [](https://opencollective.com/rxts) | [](https://opencollective.com/unts) |## Backers
| 1stG | RxTS | UnTS |
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| [](https://opencollective.com/1stG) | [](https://opencollective.com/rxts) | [](https://opencollective.com/unts) |## Changelog
Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.md).
## License
[MIT][] © [JounQin][]@[1stG.me][]
[1stG.me]: https://www.1stg.me
[JounQin]: https://GitHub.com/JounQin
[MIT]: http://opensource.org/licenses/MIT