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

cli-driver

v0.2.7

Published

Automate your complex CLI applications, like webdriver but for the command line

Downloads

135

Readme

Build Status appveyor Build status codecov dependencies devDependencies

cli-driver

like web-driver but for the command line

Contents

Summart

  • A Node.js library that let's you emulate a terminal with an easy API.
  • Multi-platform support. Terminals tested:
    • Linux: xterm, gnome, Rxvt, terminator
    • MacOs: Terminal, iTerm2
    • Windows: PowerShell, cmd, git terminal, MINGW64, cmder
  • Demos !
  • Reference API. You should start in Driver class
  • TypeScript support
  • High level API and easy to use utilities for complex key combinations, waiting and asserting on output data

Install

npm install cli-driver

npm install requires some tools to be present in the system like Python and C++ compiler. Windows users can easily install them by running the following command in PowerShell as administrator. For more information see https://github.com/felixrieseberg/windows-build-tools:

npm install --global --production windows-build-tools

API Documentation

API Documentation

Usage

In the following example we instruct the driver to perform the ls command and wait until it prints package.json file that we know it should be in the output:

import {Driver} from 'cli-driver'
const client = new Driver()
client.start()
client.enter('ls')
// now we wait until package.json is printed in stdout
const data = await client.waitForData(data => data.includes('package.json'))
expect(data).toContain('package.json')
expect(data).toContain('tsconfig.json')
client.destroy()

Note you could also require() it like this: const Driver = require('cli-driver').Driver

See Driver class API docs

Companion tools

cli-driver focuses on alliviate the "driver" part of automating a task in the command line. There are other tools that complement it for writing text, entering keyboard input, mouse input, etc. These are some:

  • node-keys to simulate keyboard input with support for complex combination
  • strip-ansi Useful for removing all the escape characters of the output data when it contains styles and special escape sequences.
  • chalk
  • ansi-escape-sequences
  • ansi-regex
  • chalk
  • blessed

Example: Using async/await or good old promises

In the previous example you can notice we used await before client.waitForData() which allow us to write clean code to handle asynchrony. But if you can't or don't want to do that you can always use good old promises:

client.waitForData(data => data.includes('package.json'))
  .then(data => {
  expect(data).toContain('package.json')
  expect(data).toContain('tsconfig.json')
  client.destroy()
})

Example: Instrument npm init

The following example will create a folder, and execute npm init command answering all the questions:

import {Driver} from 'cli-driver'
import * as shell from 'shelljs'

const projectPath = 'my-cool-npm-project'
shell.mkdir('-p', projectPath)

const client = new Driver()
await client.start({
  cwd: projectPath
})
await client.enter('npm init')

// will wait until stdout prints 'package name:' and then enter the project name 'changed-my-mind-project'
await client.waitForDataAndEnter('package name:', 'changed-my-mind-project')
await client.waitForDataAndEnter('version:', '') // just press enter to use default version (1.0.0)
await client.waitForDataAndEnter('description:', 'cool description')
await client.waitForDataAndEnter('entry point:', 'src/index.js')
await client.waitForDataAndEnter('test command:', 'jasmine')
await client.waitForDataAndEnter('git repository:', '')
await client.waitForDataAndEnter('keywords:', '')
await client.waitForDataAndEnter('author:', '')
await client.waitForDataAndEnter('license:', '')
await client.waitForDataAndEnter('Is this ok?', '')

await client.wait(300) // give npm some time to write the file

const packageJson = JSON.parse(shell.cat(`${projectPath}/package.json`))
expect(packageJson.name).toBe('changed-my-mind-project')
expect(packageJson.version).toBe('1.0.0')
expect(packageJson.description).toBe('cool description')
expect(packageJson.main).toBe('src/index.js')

Why ?

I'm author of plenty packages that use interactive CLI, like yeoman generators and inquirer-based stuff and I would really like to implement integration tests, not just mocking the CLI, but test them in the real worl in different operating systems.

There is a similar node package, node-suppose that attack the same problem, but IMO the UNIX API and semantics is very limited for today days and I wanted an API more imperative, similar to webdriver.

Demo

This code automates an example program based on Inquirer.js. It not only test for validation and entering data but also that the output of the program is correct.

This looks like in Linux bash terminal:

cli-driver example in Linux bash terminal

And this looks like in a Windows Power Shell:

cli-driver example in Windows Power Shell And this is in Windows cmd.exe terminal

cli-driver example in Windows cmd.exe terminal

And this looks like in a Windows MINGW64 terminal:

cli-driver example in Windows MINGW64 terminal