bashscript

2.0.26 • Public • Published

bashscript

A small and minimalist REPL shell, and a very lightweight JavaScript library meant to convert Operating System commands to functions.

TLDR

import { cmd } from 'bashscript';
await cmd.echo("Grr Grr Meow!").exe; // Grr Grr Meow!

Installation

npm i bashscript

REPL Shell

npm i -g bashscript # install bashscript REPL shell
$ bashscript # enter shell
> process.title
node

> process.uptime()
22.950190303

> cmd.echo('Meow!').value()
Promise { <pending> }

> await cmd.echo('Meow!').value()
'Meow!'

Advanced REPL

> const command = cmd.echo('Foo');
undefined

> command
Shell {}

> await command.value();
'Foo'

> command.result
{
  command: 'echo Foo',
  exitCode: 0,
  stdout: 'Foo',
  stderr: '',
  all: undefined,
  failed: false,
  timedOut: false,
  isCanceled: false,
  killed: false
}

Usage

import { cmd, exe } from 'bashscript';
const { echo } = cmd;
const result = await exe( echo("Meow!") );
assert.equal(result, "Meow!");
import { cmd } from 'bashscript';
const result = await cmd.echo("Meow!").value();
assert.equal(result, "Meow!");
import { cmd } from 'bashscript';
const result = await cmd.echo("Meow!").exe;
assert.equal(result, "Meow!");

Pipeline Example

import { cmd } from 'bashscript';
const { pipeline, cat, echo, tr, grep } = cmd;
const result = await pipeline(
  cat( echo('package.json') ),
  tr( '"[a-z]"', '"[A-Z]"'),
  grep('NAME')
).value();
assert.equal(result, '  "NAME": "BASHSCRIPT",')
import { cmd } from 'bashscript';
const { pipeline, cat, printf, dirname, readlink, which, grep, head } = cmd;
const result = await exe( pipeline(
  cat(
    printf(
      "%s",
      dirname(
        readlink(
          '-f',
          which('npm')
        )
      ),
      "/../package.json"
    )
  ),
  grep('name'),
  head('-n', 1)
) );
assert.equal(result, '  "name": "npm",')
import { cmd } from 'bashscript';
const { pipeline, cat, printf, dirname, readlink, which, grep, head } = cmd;
const result = await pipeline( cat( printf( "%s", dirname( readlink( '-f', which('npm') ) ), "/../package.json" ) ), grep('name'), head('-n', 1) ).value();
assert.equal(result, '  "name": "npm",')
import { cmd, pipeline } from 'bashscript'; // a pipeline helper can also be imported directly from the bashscript module.
const { cat, printf, dirname, readlink, which, grep, head } = cmd;
const result = await pipeline(cat( printf("%s", dirname(readlink('-f', which('npm'))),"/../package.json" )), grep('name'), head('-n', 1) ).value();
debug(result);
assert.equal(result, '  "name": "npm",')

Debugging Example

#!/usr/bin/env -S node --trace-uncaught
import { inspect } from 'util';
import { cmd } from '../index.js';
const { montage } = cmd;
const files = ['a.png', 'b.png', 'c.png'];
const tile = 2;
const filePath = '/tmp/test.jpg';
const command = montage(
  {'-background': '"#212529"'},
  ...files,
  {
    '-geometry': '320x230',
    '-tile':`${tile}x`
  },
  filePath
);

console.log(await command.string);
// await command.exe; // to execute;

prints

montage -background "#212529" a.png b.png c.png -geometry 320x230 -tile 2x /tmp/test.jpg

Namage

Not related to Bash the GNU Project's shell (the Bourne Again SHell), this is a JavaScript library that automagically converts system commands to JavaScript functions (Think "bash, verb: strike hard and violently");

History

Need for OS command integration arose from development of https://catpea.com, I was unable to locate small enough solutions that worked well for me. I came across proxy-www and ended up creating munchhausen and later bashscript.

Readme

Keywords

none

Package Sidebar

Install

npm i bashscript

Weekly Downloads

2

Version

2.0.26

License

ISC

Unpacked Size

51.7 kB

Total Files

16

Last publish

Collaborators

  • catpea