0.0.9 • Published 5 months ago

@whop-cli/bash v0.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

@whop-cli/bash

The provided TypeScript code defines two functions, bash and make, that allow executing shell commands in a Node.js environment. These functions take advantage of tagged template literals to construct and execute shell commands. The module also provides asynchronous versions, bashAsync and makeAsync, which return an async iterator.

Prerequisites

Install:

npm install @whop-cli/bash

Function Overview

bash

The bash function is a tagged template literal function that executes shell commands. It takes a template string and evaluates it as a shell command. Any embedded variables inside the template string will be resolved and used as arguments to the shell command.

make

The make function is similar to bash, but it allows you to specify custom options for the child process created by the exec function from Node.js' child_process module. These options can control the behavior of the child process, such as the working directory, environment variables, etc.

bashAsync

The bashAsync function is the asynchronous version of bash. It returns an async iterator that yields the result of each executed shell command.

makeAsync

The makeAsync function is the asynchronous version of make. It also returns an async iterator that yields the result of each executed shell command.

Usage

To use the bash and make functions, follow these steps:

  1. Import the desired function from the module:

    import { bash, make, bashAsync, makeAsync } from '@whop-cli/bash';
  2. Use the function with a template string to execute shell commands:

    // Asynchronous version (non-iterator)
    const result = await bash`echo Hello, world!`;
    console.log(result[0].stdout);
    
    // Asynchronous version (iterator)
    for await (const { stdout } of bashAsync`echo Hello, async world!`) {
      console.log(stdout);
    }
  3. For make and makeAsync, you can specify additional options for the child process:

    // Asynchronous version (non-iterator)
    const options = { cwd: '/path/to/directory', env: { DEBUG: 'true' } };
    const result = await make(options)`ls -l`;
    console.log(result[0].stdout);
    
    // Asynchronous version (iterator) with options
    const options = { cwd: '/path/to/directory', env: { DEBUG: 'true' } };
    for await (const { stdout } of makeAsync(options)`ls -l`) {
      console.log(stdout);
    }

Function Parameters

Both the bash and make functions take the same parameters:

  • strings (TemplateStringsArray): A template string containing the shell command to be executed, along with any embedded variables.
  • ...vars (BashVars[]): An array of variables corresponding to the embedded variables in the template string. These variables can be strings, numbers, functions, or objects.

BashVars Type

The BashVars type is a union type that represents the allowed types for variables in the template string:

  • string: A simple string value.
  • number: A numeric value.
  • undefined: Represents an undefined value.
  • null: Represents a null value.
  • object: Represents a plain object (excluding functions).
  • (result: { stdout: string; stderr: string }[]) => unknown: A function that receives the previous results and returns a value.

ResultType

The ResultType type represents an array of objects with stdout and stderr properties. These objects contain the results of the executed shell commands.

Example

Here's an example of how you can use the bash function:

import { bash } from '@whop-cli/bash';

async function main() {
  try {
    // Execute a simple shell command and log the result
    const result = await bash`echo Hello, world!`;
    console.log(result[0].stdout);

    // Execute multiple commands and log their results
    const [result1, result2] = await bash`
      echo Hello from command 1!
      echo Hello from command 2!
    `;
    console.log(result1.stdout);
    console.log(result2.stdout);
  } catch (error) {
    console.error('An error occurred:', error);
  }
}

main();

Keep in mind that executing shell commands can be risky, especially if you are dealing with user input. Always sanitize and validate user input before including it in shell commands to prevent command injection attacks.

0.0.9-canary.0

5 months ago

0.0.9

5 months ago

0.0.8

9 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago

0.0.1-canary.10

9 months ago

0.0.1-canary.9

9 months ago

0.0.1-canary.8

10 months ago

0.0.1-canary.7

10 months ago

0.0.1-canary.6

10 months ago