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

@jrc03c/better-exec

v0.0.3

Published

a version of `child_process.exec` that prints output in realtime

Downloads

2

Readme

Intro

better-exec is just a version of child_process.exec that prints output in (almost) realtime rather than all at once at the end of the execution. It also offers the option to run multiple commands, optionally interspersed with JS function calls.

Installation

npm install --save https://github.com/jrc03c/better-exec

Usage

const exec = require("@jrc03c/better-exec")

// callback style
exec("wget https://example.com/path/to/some-file", results => {
  const { stdout, stderr, error } = results
  if (error) throw error
  // do something with stdout & stderr
  console.log("Done!")
})

// Promise style
exec("wget https://example.com/path/to/some-file").then(results => {
  const { stdout, stderr, error } = results
  if (error) throw error
  // do something with stdout & stderr
  console.log("Done!")
})

// run multiple commands
const commands = [
  "cd /my/favorite/directory",
  "npm install --save my-favorite-package",
]

exec(commands).then(() => {
  console.log("Done!")
})

// mix commands and function calls
const fs = require("fs")
const path = require("path")
const dir = "/my/favorite/directory"

const commands = [
  `cd "${dir}"`,
  () =>
    fs.writeFileSync(
      path.join(dir, "comments.txt"),
      "Here are my comments!",
      "utf8"
    ),
  "cat comments.txt",
]

exec(commands).then(...)

In the above examples, you can do something with stdout and stderr (from the results object) if you want, but do note that they'll already have been printed to the command line by the time you see them in the callback function.

Silence

By default, better-exec prints all output to the command line. If you'd prefer to disable this behavior, though, then simply pass true as the second parameter, like this:

const exec = require("@jrc03c/better-exec")
const silent = true

exec("wget https://example.com/path/to/some-file", silent, results => {
  const { stdout, stderr, error } = results
  // ...
})

Caveats

Running multiple commands

One major caveat is that you shouldn't try to run multiple commands in single string. For example, this is a bad idea:

exec("cd /my/favorite/directory && npm install --save my-favorite-package")

Instead, it should be broken into multiple commands, like this:

const commands = [
  "cd /my/favorite/directory",
  "npm install --save my-favorite-package",
]

exec(commands).then(...)

Changing directories

There's only one command that isn't literally run as given: cd. Instead of literally spawning a process that'll run the cd command, the current process's working directory is changed via process.chdir("/some/path"). I don't think this'll cause many problems so long as you're doing pretty basic stuff. But if you try to get fancy — like, for example, by changing to a directory value that's computed at runtime in a nested command — then I don't know what'll happen. If you need to compute something at runtime, maybe interject a JS function call to compute the value, and then use that value in the next command. For example:

const root = "/my/dir"
let id

const commands = [
  `cd "${root}"`,
  () => (id = Math.random().toString().split(".")[1]),
  `mkdir -p "${id}"`,
  // etc.
]

exec(commands)