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

@jobandtalent/gabbs

v0.0.3

Published

Library for building Node.js command line tools

Downloads

3

Readme

Gabbs Build Status npm version

npm

Library for building command line tools

Tutorial

Let's create a command line utility called test-gabbs. First we need to create a bootstrap file:

test-gabbs.js

const runner = require('@jobandtalent/gabbs').runner;

runner.start().then((result) => {
  console.log(result);
});

Now we'll add our first parameter. We want to call it hello-world so we'll add a file with that name in the ./command folder.

./command/hello-world.js

const BaseCommand = require('@jobandtalent/gabbs').BaseCommand;

const HelloWorld = function(args) {
  BaseCommand.call(this, args);
};

HelloWorld.prototype = new HelloWorld();

HelloWorld.prototype.run = function() {
  return new Promise((resolve) => {
    resolve('Hello world!');
  });
};

module.exports = HelloWorld;

Notice how in the method run we are returning a Promise, that's the way gabbs processes asynchronous operations.

Now we can run our first parameter:

./test-gabbs --hello-world # => Will output 'Hello world!'

Arguments

You can access to the command line arguments by using the private property _args.

Let's use that for printing a customized message:

HelloWorld.prototype.run = function() {
  const self = this;
  return new Promise((resolve) => {
    resolve(self._args[1]);
  });
};
./test-gabbs --hello-world "My custom hello world!" # => Will output 'My custom hello world!'

Logger

gabbs incorportes a logger utility in order to pretty-print output:

const logger = require('@jobandtalent/gabbs').logger;

HelloWorld.prototype.run = function() {
  return new Promise((resolve, reject) => {
    asyncTask((err, data) => {
      if (err) {
        logger.error('Something bad happened!');
        reject();
      } else {
        logger.log('Processed finished');
        resolve(data);
      }
    });
  });
};

Running processes

In order to implement more complex commands you can use the Spawn object that will let you to run external tasks.

The Spawn object provides a layer of Promises so you can return it directly in your run method.

const Spawn = require('@jobandtalent/gabbs').Spawn;

HelloWorld.prototype.run = function() {
  const process = new Spawn();
  return process.run('sh', ['./script.sh', 'first-parameter']);
};

You can chain subprocess execution by using Promise.all:

HelloWorld.prototype.run = function() {
  ...
  return Promise.all([
    process.run('sh', ['./script.sh', 'first-parameter']),
    process.run('npm', ['install', '--dev'])
  ]);
};

Accessing to the github api

Another utility is the GHApi wrapper that encapsulates the gh library in order to provide a layer of Promises for accessing to the github api.

Suppose that we want to add a new parameter to our tool that reads a file from github:

./command/github-test.js

const GHApi = require('@jobandtalent/gabbs').GHApi;

GithubTest.prototype.run = function() {
  const api = new GHApi();
  // Here you will need to create a github application token
  // @see https://help.github.com/articles/creating-an-access-token-for-command-line-use/
  api.connect('username', 'github-token');

  return new Promise((resolve, reject) => {
    api.getFileAtRevision(self._args[1], self._args[2], self._args[3])
    .then((file) => {
       resolve(file.content);
     });
  });
};

Now, with this command we have a way of reading from command line any file from github:

# Output package.json file from the jobandtalent/gabbs repository
./test-gabbs --github-test jobandtalent gabbs package.json

The GHApi object provides the following methods:

connect = function(username, token) {};
getFilesFromPullRequest = function(user, repo, id) {};
getFileAtRevision = function(user, repo, path, ref) {};
extractRepoInfoFromUrl = function(url) {};
createRelease = function(owner, repo, tag) {};

License

Apache 2

Maintainer

Jobandtalent Frontend Guild