Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/txthinking/jb
jb: write script in an easier way than bash
https://github.com/txthinking/jb
bash bun cli javascript nodejs shell
Last synced: about 2 months ago
JSON representation
jb: write script in an easier way than bash
- Host: GitHub
- URL: https://github.com/txthinking/jb
- Owner: txthinking
- Created: 2023-06-04T21:47:39.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-04T05:34:07.000Z (about 1 year ago)
- Last Synced: 2024-03-01T07:36:40.668Z (10 months ago)
- Topics: bash, bun, cli, javascript, nodejs, shell
- Language: Zig
- Homepage: https://www.txthinking.com
- Size: 117 MB
- Stars: 17
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# `jb` = `javascript` + `bash`
```
#!/usr/bin/env jb$`ls -l`
var output = $1`whoami`
$(`echo ${output}`)
```Bash is great, but when it comes to writing more complex scripts, many people prefer a more convenient programming language. JavaScript is a perfect choice. The jb provides useful wrappers. The jb is a [bun](https://github.com/oven-sh/bun) port of [zx](https://github.com/google/zx), the advantage is that you don't need to install node, and **jb is just a standalone binary**.
## Install
jb is just a standalone binary, so you can [download](https://github.com/txthinking/jb/releases) and put it in your PATH. Or install it via [nami](https://github.com/txthinking/nami)
```
nami install jb
```## Documentation
Just write your scripts in a file with an `.js`
Add the following shebang to the beginning of your `jb` scripts:
```bash
#!/usr/bin/env jb
```Now you will be able to run your script like so:
```bash
chmod +x ./script.js
./script.js
```Or via the `jb` executable:
```bash
jb ./script.js
```Or executes remote script
```bash
jb https://www.txthinking.com/script.js
```All functions (`$`, `cd`, `fetch`, etc) are available straight away without any imports.
### ``$`command` ``
Executes a given command, keep the default behavior of stdout and stderr like bash
```js
$`ls -l`
```Or put a variable in command
```js
var name = 'hello'
$(`mkdir ${name}`)
```You can pass an array of arguments if needed:
```js
var flags = [
'-l',
'-h',
]
$(`ls ${flags.join(' ')}`)
```If the executed program failed, error will be thrown.
```js
$`brook unknownsubcommand`
```### ``$1`command` ``
Same as ``$`command` ``, but will return stdout and trim space, as you know, 1 is STDOUT
```js
var count = $1`ls -l | wc -l`
$(`echo ${count}`)
```### `env()`
Set env
```js
env('HELLO', "JB")
$`echo $HELLO` // => JB
```### `cd()`
Changes the current working directory.
```js
cd('/tmp')
$`pwd` // => /tmp
```### `question()`
```js
var name = question('What is your name? ')
```### `confirm()`
```js
var ok = confirm('Do you really want to leave?');
```### `sleep()`
```js
sleep(1000)
```### `now()`
Current unix timestamp
```js
var t = now()
```### `echo()`
A `console.log()` alternative
```js
echo('hello')
```### `which()`
File path or null
```js
var bin = which('brook')
```### `exit()`
Exit the script
```js
exit()
```### `exists_file()`
file exists or not
```js
var yn = exists_file('path/to/file.txt')
```### `read_file()`
Read the text from local file
```js
var str = read_file('path/to/file.txt')
```### `write_file()`
Write text to local file
```js
write_file('path/to/file.txt', 'some text')
```### `append_file()`
Append text to local file
```js
append_file('path/to/file.txt', 'some text')
```### `cp()`
Copy file or http file from zip/tar.gz/tar.xz to local
```js
cp('https://github.com/txthinking/brook/releases/latest/download/brook_darwin_arm64', '/tmp/brook');
```Copy file from zip
```
7z l ~/Downloads/bun-darwin-aarch64.zip
```
```
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2023-06-03 15:54:41 D.... 0 0 bun-darwin-aarch64
2023-06-03 15:54:41 ..... 46584592 16787941 bun-darwin-aarch64/bun
------------------- ----- ------------ ------------ ------------------------
2023-06-03 15:54:41 46584592 16787941 1 files, 1 folders
``````js
cp('$HOME/Downloads/bun-darwin-aarch64.zip', 'bun-darwin-aarch64/bun', '/tmp/bun');
```Copy multiple files from tar.gz
```
tar ztf ~/Downloads/cowsay_2.0.4_macOS_arm64.tar.gz
```
```
LICENSE
doc/cowsay.1
cowsay
cowthink
```
```js
cp('$HOME/Downloads/cowsay_2.0.4_macOS_arm64.tar.gz', {
'cowsay': '/tmp/cowsay',
'cowthink': '/tmp/cowthink',
});
```
Copy multiple files from tar.xz
```
tar Jtf ~/Downloads/shadowsocks-v1.15.3.aarch64-apple-darwin.tar.xz
```
```
sslocal
ssserver
ssurl
ssmanager
ssservice
```
```js
cp('https://github.com/shadowsocks/shadowsocks-rust/releases/latest/download/shadowsocks-v1.15.3.aarch64-apple-darwin.tar.xz', {
'sslocal': '/tmp/sslocal',
'ssserver': '/tmp/ssserver',
});
```## Async Functions
### `stdin()`
Returns the stdin as a string.
```js
var s = await stdin()
echo(`got ${s} from pipe`);
```
```
echo hello | jb ./script.js
```### `retry()`
Will return after the first successful attempt, or will throw after specifies attempts count.
```js
var s = await retry(() => $1`curl https://www.txthinking.com`)// delay 1s
var s = await retry(() => $1`curl https://www.txthinking.com`, 1000)// delay 1s and max 3 times
var s = await retry(() => $1`curl https://www.txthinking.com`, 1000, 3)
```## Web, Node, Bun
### built-in [Web API](https://developer.mozilla.org/en-US/docs/Web/API) such as [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
```js
var res = await fetch('https://www.txthinking.com');
```### built-in [node](https://nodejs.org/api/)
```js
import os from 'node:os';echo(os.homedir());
```### built-in [bun](https://bun.sh/docs/api/http)
```js
Bun.serve({
port: 8080,
hostname: '127.0.0.1',
fetch(req) {
return new Response("jb!");
},
});
```## Executing commands on remote hosts
The `jb` uses [sshexec](https://github.com/txthinking/sshexec) to execute commands on remote hosts.
```js
$`sshexec -s 1.2.3.4:22 -u user -p pass -c 'ls -l'`
```
Download file from remote```js
$`sshexec -s 1.2.3.4:22 -u user -p pass --download /server/path/to/file --to /local/path/to/file`
```
Upload file to remote```js
$`sshexec -s 1.2.3.4:22 -u user -k path/to/private/key --upload /local/path/to/file --to /server/path/to/file`
```## License
jb itself is [MIT-licensed](LICENSE). Refer to the [dependencies license](https://bun.sh/docs/project/licensing) for information.