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

scrolex

v0.0.27

Published

Execute commands, scroll back the stdout & stderr, prefix by user-defined categories

Downloads

129

Readme

Build Status

scrolex

It's like a ⌚ Rolex, except it has nothing to do with Rolexes and executes commands instead.

Scrolex executes commands, captures & scrolls back the output, optionally prefixing and overwriting each last line with the next. Here, a demo works better:

Demo

Install

yarn add scrolex || npm install scrolex --save

Use

First, require scrolex:

const scrolex = require('scrolex')

Execute a shell command:

scrolex.exe('ls -al', { }, (err, stdout) => {
  const lsOutput = stdout
})

Execute without a shell:

scrolex.exe(['/bin/sh', 'ls', '-al'], { }, (err, stdout) => {
  const lsOutput = stdout
})

Do not copy the command's output to the terminal:

scrolex.exe('ls -al', { mode: 'silent' }, (err, stdout) => {
  const lsOutput = stdout
})

Overrule environment

scrolex.exe('ls -al', { env: { YOUR_SECRET: 'not-safe-with-me' } }, (err, stdout) => {
  const lsOutput = stdout
})

Announce the command you are executing:

scrolex.exe('ls -al', { announce: true }, (err, stdout) => {
  const lsOutput = stdout
})

// Announce also leaves a sticky line: `Successfully executed: ls -al` or `Failed to execute: ls -al`

Prefix output with ✔ myapp ❯ prepare ❯:

scrolex.exe('ls -al', { components: 'myapp>prepare' }, (err, stdout) => {
  const lsOutput = stdout
})

Add some ephemeral output, respecting the currently set prefix:

scrolex.scroll('i will be overwritten by anything with the same prefix')

Add some sticky output, respecting the currently set prefix:

scrolex.stick('i will stick around, no matter what')

Use async/await for shell-scripting super-powers ⚡

const serialExecution = async () => {
  try {
    const cores = await scrolex.exe('getconf _NPROCESSORS_ONLN')
    if (cores > 10) {
      await scrolex.stick(`You have ${cores} cpu cores. Amazing!`)
    }
    const processTree = await scrolex.exe('ps auxf', { silent: true })
  } catch (err) {
    throw new Error(err)
  }  
}
serialExecution()

Use Promises for ... yeah why would you. But the important thing is you can!:

scrolex.exe('ls -al')
  .then((stdout) => {
    const lsOutput = stdout
  })
  .catch((err) => {
    throw new Error(err)
  })

Options

fatal

If set to true, whenever exe yields a non-zero exit code, the whole process stops with a dump of the combined stderr & stdout on-screen. Useful for shell-scripting tasks. Default: false.

env

Allows to specify the executed program's environment. If you want to add environment on top of the current environment use something like:

env: Object.assign({}, process.env, {
  YOUR_SECRET: 'not-safe-with-me',
}),

Default: process.env.

Global State (?!?!!!?‼️❓)

Yes, by default Scrolex uses global state (global.scrolex) within a Node process to keep track of output and so that options can be re-used across instances if you set them with persistOpts. This makes it so that consequent calls can be lightweight, as well as the output consistent looking.

If you'd rather ditch convenience in favor of strictness, or this causes a hard time testing, you are welcome to pass in your own state object, and Scrolex will happily use that instead:

const myLocalStateObject = {}
scrolex.exe('ls -al', { state: myLocalStateObject })

You can even pass a new state object each time to avoid any kind of magic inheritance:

scrolex.exe('ls -al', { state: {} })

Here's how to make all subsequent scrolex.exe() calls add their currently executing command to the prefix:

scrolex.persistOpts({
  addCommandAsComponent: true
})

You can also persist upon requiring:

const scrolex = require('./Scrolex').persistOpts({
  mode: 'silent',
})
scrolex.exe('ls -al', (err, out) => {
  
})

Todo

See CHANGELOG.md

Changelog

See CHANGELOG.md

Authors

Related Projects

  • listr: Nicer I'd say, but it does impose its structure on your code

License

Copyright (c) 2017 Kevin van Zonneveld. Licenses under MIT.