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

@kshehadeh/cmdq

v0.0.13

Published

A tool for chaining shell commands

Downloads

21

Readme

CmdQ

CmdQ is a chained command queue for typescript. It allows for the chaining of commands and the execution of those commands in a sequential order. Additionally, with easy to use hooks, you can apply custom logic to conditionally execute commands and also mutate output from previous commands using typescript. Through a mix of shell commands and typescript, you can create powerful command queues that can be used in a variety of applications.

Installation

npm install @kshehadeh/cmdq

Usage

Simple Example

import { q } from 'cmdq';

q()
  .cmd('echo "Hello World"')  
  .run();

// Output: Hello World

Chaining

import { q } from 'cmdq';

q()
  .cmd('echo "Hello World"')
  .cmd('echo "Goodbye World"')
  .run();

// Output: Hello World
//         Goodbye World

Conditional Execution

import { q } from 'cmdq';

q()
  .cmd('echo "Hello World"', {
    pre: () => false
  })
  .cmd('echo "Goodbye World"')
  .run();

// Output: Goodbye World

Mutating Output

import { q } from 'cmdq';

q()
  .cmd('echo "Hello World"', {
    post: (result) => {
      result.out = 'Goodbye World';
      return result;
    }
  })
  .run();

// Output: Goodbye World

Piping Output

import { q } from 'cmdq';

q()
  .cmd('echo "Hello World"')
  .pipe('wc -w')
  .run();

// Output: 2

Note: If you a command is to be piped to but the target command is skipped, the previous command will not be run.

Note: You can chain multiple commands to be piped to by calling pipe multiple times. For example:

import { q } from 'cmdq';

q()
  .cmd('echo "{ \"name\": \"John\" }"')
  .pipe('jq .name')
  .pipe('tr -d \'"\'')
  .run();

API

q([config])

Creates a new command queue.

config:

  • workingDirectory - The working directory from which to run the commands.
  • enableTrace - Outputs information about the commands being run using the configured tracer
  • continueOnError - If set to true, the command queue will continue to run commands even if a command fails.
  • dryRun - If set to true, the command queue will not run any commands, but will output the commands that would have been run.
  • dryRunResultsFile - If set, the command queue will write the results of each command to the specified file. If dryRun is true, then this file will be used as input and any commands that have matching entries in the results file will use the output from that file.

q.cmd(command, [options])

Adds a command to the queue to be executed when run is called.

command - The command to run.

options:

  • continueOnError - If set to true, the queue will continue to run even if this command fails - ignores the global continueOnError setting.
  • pipe - This can be one of stdout, stderr, both or none. If set, the output of the previous command will be piped to stdin of the current command.
  • pre - (result: CommandQueueResult | null) => boolean - A function that will be called before the command is run. If the function returns false, the command will be skipped.
  • post - (result: CommandQueueResult) => CommandQueueResult - A function that will be called after the command is run. The result of the command will be passed to this function and the return value will be used as the result of the command.

q.run()

Runs the command queue in the order that commands were added taking into account any hooks that were set.

q.first()

Runs the first command result in the queue - meant to be used after run has been called.

q.last()

Runs the last command result in the queue - meant to be used after run has been called.

q.results

An array of the results of each command that was run.

r()

A convenience function that executes a single command and returns the result. Meant to be used in cases where you don't need to chain commands but want the convenience of the hooks and other features of the command queue.

Contributing

If you would like to contribute to this project, please open an issue or a pull request.

License

MIT