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

sg-clihelp

v1.0.93

Published

Utilities for writing cli apps

Downloads

31

Readme

Cli Help

Utilities to help write command-line apps.

Usage

npm install sg-clihelp

Then, writing a short script or command-line app is easy.

The copy-and-paste version is below.

const sg          = require('sg-clihelp');      // Just for explanation, use require('sh-clihelp').all(), as below.
const {os,path}   = sg;
const {sh}        = sg;                         // https://github.com/shelljs/shelljs
const {test}      = sg.sh;
const {execa}     = sg;                         // https://github.com/sindresorhus/execa
const ARGV        = sg.ARGV();
const ENV         = sg.ENV();

// Do not be too eager if we are just being required
if (require.main === module) {
  sg.runTopAsync(main);
}

async function main() {
  const foo   = ARGV.foo;
  const bar   = ENV.at('BAR');

  const confDir = path.join(os.homedir(), 'quxxdir');

  if (!test('-d', confDir)) {
    return sg.dieAsync(`Need ${confDir}`);
  }

  const configFile = path.join(confDir, 'config.json');
  if (!test('-f', configFile)) {
    return sg.dieAsync(`Need ${configFile}`);
  }

  const battConfig = sg.from(confDir, 'config.json', 'foo.bar.batt');

  // ...

  const cmdStdout = await execa.stdout(sh.which('command').toString(), ['arg1', 'arg2']);
  console.log(sg.splitLn(cmdStdout));

  // ...
}

Copy-and-Paste

This is the version to start with. Uncomment as needed.

const {sg,fs,path,os,util,sh,die,dieAsync,grepLines,include,from,startupDone,runTopAsync,exec,execa,execz,exec_ez,find,grep,ls,mkdir,SgDir,test,tempdir,inspect,glob} = require('sg-clihelp').all();

//const tmp         = require('tmp');             // https://github.com/raszi/node-tmp
//const crypto      = require('crypto');

//require('loud-rejection/register');             // https://github.com/sindresorhus/loud-rejection
//require('hard-rejection/register');             // https://github.com/sindresorhus/hard-rejection
//require('exit-on-epipe');                       // https://github.com/SheetJS/node-exit-on-epipe

const ARGV        = sg.ARGV();
const ENV         = sg.ENV();

// Make sure we are being invoked, not just being `required()`.
if (require.main === module) {
  sg.runTopAsync(main);
}



//-----------------------------------------------------------------------------------------------------------------------------
async function main() {
  const foo   = ARGV.foo;
  const bar   = ENV.at('BAR');

  const confDir = path.join(os.homedir(), 'quxxdir');

  if (!test('-d', confDir)) {
    return sg.dieAsync(`Need ${confDir}`);
  }

  const configFile = path.join(confDir, 'config.json');
  if (!test('-f', configFile)) {
    return sg.dieAsync(`Need ${configFile}`);
  }

  const battConfig = sg.from(confDir, 'config.json', 'foo.bar.batt');

  // ...

  const cmdStdout = await execa.stdout(sh.which('command').toString(), ['arg1', 'arg2']);
  console.log(sg.splitLn(cmdStdout));

  // ...
}

Included

Included with sg-clihelp:

  • sg.die(msg, code =113) - shows msg, then process.exit(code)
  • sg.dieAsync(msg, code =113) - shows msg, then process.exit(code) - Use with runTopAsync.
  • sg.grepLines(regex, filename) - sends lines that match regex from filename to stdout
  • sg.include(dirname, filename) - Safe include.
  • sg.from(dirname, filename, key) - Get value from JSON file.
  • sg.runTopAsync(main, name='main') - If main is a top-level async function, use this to run it.
  • sg.startupDone(ARGV, modfilename, failed, msg) - messaging for --help and failed

From sg0:

  • sg.safeJSONParse(json)
  • sg.safeJSONStringify(obj)
  • sg.splitLn(str)
  • sg.deref(obj, keys)
  • sg.setOn(obj, keys, value)
  • sg.setOna(obj, keys, value)

Other modules bundled:

  • sg0
  • sg-argv
    • sg.ARGV()
  • sg-env
    • sg.ENV()
  • sg-exec
    • execa
    • exec from shelljs
    • exec_ez from sg-exec
  • sg.fs - Node.js fs module.
  • sg.path - Node.js path module.
  • sg.os - Node.js os module.
  • sg.util - Node.js util module.
  • sg.sh - shell.js module.