
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
cli-driver
Advanced tools
Automate your complex CLI applications, like webdriver but for the command line
like web-driver but for the command line
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
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
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:
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()
})
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')
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.
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:
And this looks like in a Windows Power Shell:
And this is in Windows cmd.exe terminal
And this looks like in a Windows MINGW64 terminal:
FAQs
Automate your complex CLI applications, like webdriver but for the command line
The npm package cli-driver receives a total of 11 weekly downloads. As such, cli-driver popularity was classified as not popular.
We found that cli-driver demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.