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

@fnet/shell

v0.1.15

Published

The `@fnet/shell` project is a useful tool designed to help users execute shell commands within a Node.js environment. It provides a convenient way to run commands both synchronously and asynchronously, with the flexibility to configure command execution

Downloads

136

Readme

@fnet/shell

The @fnet/shell project is a useful tool designed to help users execute shell commands within a Node.js environment. It provides a convenient way to run commands both synchronously and asynchronously, with the flexibility to configure command execution options. This tool is particularly handy for users who need to perform shell operations as part of their Node.js applications effortlessly.

How It Works

@fnet/shell functions by allowing you to execute shell commands through JavaScript code. You can pass in either a simple command string or an object specifying the command and various options. The shell command is executed, and the results, such as the exit code, standard output, and standard error, are returned as a promise, providing a straightforward way to handle asynchronous command execution.

Key Features

  • Command Execution: Easily execute shell commands through JavaScript within a Node.js environment.
  • Asynchronous Support: Enable asynchronous execution to run commands without blocking the Node.js main thread.
  • Configurable Options: Customize the execution environment and output handling, including working directory and silent mode.
  • Promise-based Results: Receive command results asynchronously, including the exit code, standard output, and standard error.

Conclusion

The @fnet/shell project provides a simple and efficient solution for integrating shell command execution into your Node.js applications. It offers flexibility in command execution and result handling, making it a practical tool for developers needing such functionality.

Developer Guide for @fnet/shell

Overview

The @fnet/shell library simplifies the execution of shell commands from within a Node.js application. The primary functionality of this library is to execute shell commands asynchronously or synchronously, handling standard output and errors with ease. This allows developers to seamlessly integrate shell command execution into their applications without dealing with low-level process management.

Installation

To install the @fnet/shell package, you can use either npm or yarn. Here are the commands:

Using npm:

npm install @fnet/shell

Using yarn:

yarn add @fnet/shell

Usage

The library provides a single exported function which allows you to execute shell commands with specified options.

Basic Usage

To execute a shell command, you can directly pass it as a string. This will execute the command and return a promise that resolves with the result of the command execution.

import shell from '@fnet/shell';

shell('ls -l')
  .then(result => {
    console.log('Command executed successfully:');
    console.log(result.stdout);
  })
  .catch(error => {
    console.error('Command execution failed:', error);
  });

Advanced Usage

If you need more control over the execution, such as specifying options or handling command execution asynchronously, you can pass an object with a cmd property and options.

import shell from '@fnet/shell';

// Execute a command with specific options
shell({
  cmd: 'echo "Hello, World!"',
  options: { silent: true }
})
  .then(result => {
    console.log('Command Output:', result.stdout);
  })
  .catch(error => {
    console.error('Error:', error.stderr);
  });

// Running a command asynchronously
shell({
  cmd: 'long-running-command',
  options: { async: true }
})
  .then(result => {
    console.log(result.stdout); // "Command is running in the background."
  });

Examples

Here are some examples to demonstrate typical use cases of the library:

Example 1: Synchronous Command Execution

Execute a simple shell command to list files in a directory:

shell('ls').then(result => {
  console.log(result.stdout);
});

Example 2: Asynchronous Command Execution

Run a shell command in the background:

shell({ cmd: 'sleep 10', options: { async: true } })
  .then(result => {
    console.log(result.stdout); // Outputs: "Command is running in the background."
  });

Example 3: Error Handling

Handle errors if the shell command fails:

shell('invalid-command')
  .catch(error => {
    console.error(`Error executing command: ${error.stderr}`);
  });

Acknowledgement

@fnet/shell leverages the shelljs library to execute shell commands, which provides a set of portable Unix shell commands within Node.js environments.

This guide delivers a practical understanding of the @fnet/shell library, allowing developers to effectively execute shell commands within their Node.js applications.

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
type:
  - object
  - string
properties:
  cmd:
    type: string
    description: The shell command to be executed.
  options:
    type: object
    properties:
      silent:
        type: boolean
        default: false
        description: If true, suppresses the output of the command.
      cwd:
        type: string
        description: Sets the current working directory before executing the command.
      async:
        type: boolean
        description: If true, executes the command asynchronously.
  wait:
    type: boolean
    default: true
    description: If false and options.async is true, the function will not wait for
      the command to complete.