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

@udes/shelljs-nodecli

v0.2.3

Published

ShellJS Node CLI Extension

Downloads

3

Readme

ShellJS Node CLI Extension (UdeS version)

An extension for ShellJS that makes it easy to find and execute Node.js CLIs.

The difference between the original package and this version is that the module will also searches inside the node_modules of your CLI.

So, this module will searches as the following way:

  • node_modules of the project where your CLI is installed or where you actually execute a command of your CLI;
  • global node_modules;
  • node_modules of your CLI if it's installed globally.

The Problem

ShellJS is a fantastic tool for interacting with the shell environment in a cross-platform way. It allows you to easily write scripts that would otherwise be written in bash without worrying about compatibility.

The only problem is that it's a real pain to execute Node binaries that are installed locally. Most end up manually looking into the node_modules directory to find the binary file to execute directly with Node, especially when working on Windows, where the files in node_modules/.bin tend not to work from scripting environments like make and ShellJS. Consequently, you end up seeing a lot of this:

import shell from 'shelljs'

const ESLINT = 'node_modules/eslint/bin/eslint.js'

shell.exec(`node ${ESLINT} myfile.js`)

The Solution

Since Node binaries are specified in their package.json files, it's actually pretty easy to look up the location of the runtime file and get the path. That's where the ShellJS Node CLI extension comes in:

import ShellJSNodeCLI from '@udes/shelljs-nodecli'

ShellJSNodeCLI.exec('eslint myfile.js')

The nodeCLI utility has its own exec() that is specifically for use when executing Node CLIs. It searches through the working directory's node_modules directory to find a locally installed utility. If it's not found there, then it searches globally. Finally, it will searches inside the node_modules of your CLI. If it's still not found, then an error is thrown.

You can pass in as many string arguments as you'd like, and they will automatically be concatenated together with a space in between, such as:

import ShellJSNodeCLI from '@udes/shelljs-nodecli'

ShellJSNodeCLI.exec("eslint -f compact myfile.js");

This ends up creating the following string:

eslint -f compact myfile.js

This frees you from needing to do tedious string concatenation to execute the command.

The exec() method otherwise behaves exactly the same as the default ShellJS exec() method, meaning you can use the same options and callback arguments, such as:

import ShellJSNodeCLI from '@udes/shelljs-nodecli'

const version = ShellJSNodeCLI.exec('eslint -v', {silent:true}).output;

const child = ShellJSNodeCLI.exec('some_long_running_process', {async:true})
child.stdout.on('data', (data) => {
    /* ... do something with data ... */
})

ShellJSNodeCLI.exec('some_long_running_process', (code, output) => {
    console.log('Exit code:', code)
    console.log('Program output:', output)
})

Copyright

  • Copyright 2014 Nicholas C. Zakas. All rights reserved.
  • Copyright 2017 Université de Sherbrooke. All rights reserved.

License

MIT License