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

@j-o-r/sh

v1.0.6

Published

Execute shell commands on Linux-based systems from javascript

Downloads

57

Readme

@j-o-r/sh

Execute shell commands from JavaScript.

Introduction

@j-o-r/sh is a Node.js module that simplifies the execution of shell commands within JavaScript applications. It provides a range of utilities to handle shell scripts and manage their output efficiently.

This project draws inspiration from the exceptional zx library. The core functionality of zx, particularly the shell execution method, has been extracted and forms the foundation of this project.

Installation

Install the module using npm:

npm install @j-o-r/sh

Usage

Basic Usage

To execute a shell command, use the SH function:

import { SH, cd, within, sleep, retry, expBackoff } from '@j-o-r/sh';

SH`your_shell_command`.run()
  .then(output => {
    console.log('Output:', output);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Advanced Usage

const res = await SH`ls -FLa | grep package.json | wc -l`.run();
console.log(res);

const ar = within(async () => {
  const res = await Promise.all([
    SH`sleep 1; echo 1`.run(),
    SH`sleep 2; echo 2`.run(),
    sleep(2),
    SH`sleep 3; echo 3`.run()
  ]);
});
const p = await retry(3, expBackoff(), () => SH`curl -s https://unreachable`.run());

The SH method accepts a template literal string enclosed in backticks as its argument. It returns an SHDispatch object.

Additional Utilities

The module also provides additional utilities for common tasks:

  • parseArgs(process.args): Transform an array of strings into an object
  • cd(dir): Change the working directory.
  • sleep(duration): Pause execution for a specified duration.
  • retry(count, interval, callback): Retry a command a specified number of times with an optional interval.
  • readIn(): Read from standard input.
  • within(callback): Create an async context in a sync block.
  • expBackoff(max, rand): Generate intervals for exponential backoff.

SHDispatch

This class is returned by the SH function. Here's a summary of its methods and properties:

Methods

  • options(options): Sets options for the command execution.
  • run(payload?): Executes the command and returns a promise that resolves with the command's output.
  • runSync(payload?): Executes the command synchronously and returns a SpawnSyncResponse.
  • kill(): Sends a kill signal to the child process.

Examples

  • Elementary usages, piped:

    const res = await SH`ls -FLa | grep package.json | wc -l`.run();
    console.log(res);
  • Feed command with content:

    const res = await SH`wc -l`.run(`one\ntwo\n`);
    console.log(res);
  • Create a command from a string

      const command = "uname -r";
      const content = await SH`${command}`.run();
      console.log(content);
  • Async context with multiple commands and sleep:

    within(async () => {
      const res = await Promise.all([
        SH`sleep 1; echo 1`.run(),
        SH`sleep 2; echo 2`.run(),
        sleep(2),
        SH`sleep 3; echo 3`.run()
      ]);
      console.log(res);
    });
  • Retry with exponential backoff:

    try {
      const p = await retry(3, expBackoff(), () => SH`curl -s https://flipwrsi`.run());
    } catch (e) {
      console.error('Retry failed:', e);
    }
  • Method for copying data to the clipboard:

    /**
    * Copy text to the clipboard
    * @param {string} text
    * @retruns {Promise<string>}
    */
    const copyToClipboard = async (text) => {
    	const prams = [
    		'-selection',
    		'clipboard'
    	]
    	return SH`xclip ${prams}`.options({stdio: 'inherit'}).run(text);
    }
  • Open the 'vim' editor

    SH`vim`.options({stdio: 'inherit'}).runSync();

License

This project is licensed under the Apache License, Version 2.0.