npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@whop-cli/bash

v0.0.9

Published

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 provid

Downloads

18

Readme

@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.