Top Related Projects
Process execution for humans
node.js command-line interfaces made easy
yargs the modern, pirate-themed successor to optimist.
:shell: Portable Unix shell commands for Node.js
Node's framework for interactive CLIs
Quick Overview
Google's zx is a tool for writing simple command-line scripts using JavaScript. It provides a more convenient and powerful alternative to bash scripting, allowing developers to leverage their JavaScript knowledge while interacting with the command line and filesystem.
Pros
- Simplifies shell scripting with JavaScript syntax and Node.js ecosystem
- Built-in support for common operations like file manipulation and command execution
- Automatic error handling and improved readability compared to traditional bash scripts
- Easy integration with existing JavaScript/Node.js projects
Cons
- Requires Node.js to be installed on the system
- May have a steeper learning curve for those unfamiliar with JavaScript
- Not as lightweight as native bash scripts
- Limited platform support compared to universal shell scripting
Code Examples
- Basic command execution:
#!/usr/bin/env zx
await $`echo "Hello, World!"`
- File manipulation and command chaining:
#!/usr/bin/env zx
await $`mkdir -p temp`
await $`echo "Test content" > temp/test.txt`
const content = await $`cat temp/test.txt`
console.log(`File content: ${content}`)
- Using JavaScript features with shell commands:
#!/usr/bin/env zx
const files = await glob('*.js')
for (const file of files) {
await $`eslint ${file}`
}
Getting Started
To get started with zx, follow these steps:
- Install Node.js if not already installed
- Install zx globally:
npm install -g zx
- Create a new script file with a
.mjs
extension, e.g.,script.mjs
- Add the shebang at the top of your script:
#!/usr/bin/env zx
- Write your script using JavaScript and zx features
- Make the script executable:
chmod +x script.mjs
- Run the script:
./script.mjs
Competitor Comparisons
Process execution for humans
Pros of execa
- More mature and stable project with a longer history
- Provides a more comprehensive set of features for process execution
- Better error handling and cross-platform compatibility
Cons of execa
- Requires more setup and configuration for complex tasks
- Less intuitive for quick, simple shell operations
- Doesn't provide built-in utility functions for common shell operations
Code Comparison
execa:
const execa = require('execa');
(async () => {
const {stdout} = await execa('echo', ['Hello world']);
console.log(stdout);
})();
zx:
#!/usr/bin/env zx
await $`echo Hello world`;
Summary
execa is a robust solution for process execution in Node.js, offering advanced features and better error handling. It's ideal for complex projects requiring fine-grained control over child processes.
zx, on the other hand, provides a more straightforward approach to shell scripting in JavaScript. It's designed for quick scripts and prototyping, with a syntax that closely resembles shell commands.
The choice between execa and zx depends on the project's requirements. execa is better suited for larger applications needing precise process management, while zx excels in scenarios where rapid development of shell-like scripts is prioritized.
node.js command-line interfaces made easy
Pros of Commander.js
- More mature and widely adopted, with a larger ecosystem of plugins and extensions
- Provides a robust and flexible API for building complex command-line interfaces
- Supports command-specific options and nested subcommands
Cons of Commander.js
- Requires more boilerplate code to set up commands and options
- Less intuitive for simple scripts or quick prototyping
- Steeper learning curve for beginners
Code Comparison
Commander.js:
const program = require('commander');
program
.version('0.1.0')
.option('-p, --peppers', 'Add peppers')
.option('-c, --cheese <type>', 'Add cheese')
.parse(process.argv);
zx:
#!/usr/bin/env zx
const hasPeppers = argv.peppers || argv.p;
const cheeseType = argv.cheese || argv.c;
if (hasPeppers) {
console.log('Adding peppers');
}
Key Differences
- Commander.js focuses on building structured CLIs with a declarative approach
- zx emphasizes simplicity and ease of use for scripting tasks
- Commander.js requires explicit command and option definitions
- zx leverages modern JavaScript features and provides built-in utilities for common tasks
Use Cases
- Commander.js: Complex CLI applications with multiple commands and options
- zx: Quick scripts, automation tasks, and prototyping
yargs the modern, pirate-themed successor to optimist.
Pros of yargs
- More mature and widely adopted project with a larger ecosystem
- Offers extensive command-line argument parsing capabilities
- Provides built-in support for generating help messages and version information
Cons of yargs
- Steeper learning curve due to its comprehensive feature set
- Requires more boilerplate code for basic CLI applications
- Less focused on shell scripting and command execution
Code Comparison
yargs:
#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
yargs(hideBin(process.argv))
.command('greet <name>', 'Greet a person', {}, (argv) => {
console.log(`Hello, ${argv.name}!`)
})
.parse()
zx:
#!/usr/bin/env zx
const name = await question('What is your name? ')
console.log(`Hello, ${name}!`)
Summary
yargs is a powerful and flexible command-line argument parsing library, ideal for complex CLI applications. It offers extensive features but requires more setup. zx, on the other hand, focuses on simplifying shell scripting and command execution in Node.js, providing a more straightforward approach for quick scripts and automation tasks. The choice between the two depends on the specific requirements of your project and the level of complexity you need in handling command-line arguments and interactions.
:shell: Portable Unix shell commands for Node.js
Pros of shelljs
- More mature and established project with a larger user base
- Provides a cross-platform solution for shell commands in Node.js
- Offers a wider range of built-in shell commands and utilities
Cons of shelljs
- Requires more setup and configuration compared to zx
- Less intuitive syntax for developers familiar with shell scripting
- Limited support for modern JavaScript features and async operations
Code Comparison
shelljs:
require('shelljs/global');
if (!which('git')) {
echo('Sorry, this script requires git');
exit(1);
}
git('clone https://github.com/user/repo.git');
zx:
#!/usr/bin/env zx
if (!(await $`which git`)) {
console.log('Sorry, this script requires git');
process.exit(1);
}
await $`git clone https://github.com/user/repo.git`;
The zx example demonstrates a more concise and shell-like syntax, leveraging modern JavaScript features like async/await. shelljs, on the other hand, provides a more traditional Node.js approach with a global import and synchronous execution.
While shelljs offers broader functionality and cross-platform compatibility, zx provides a more intuitive and modern scripting experience for developers familiar with shell commands and ES6+ JavaScript.
Node's framework for interactive CLIs
Pros of Vorpal
- More mature and established project with a larger community
- Extensive documentation and examples available
- Built-in support for command-line argument parsing and validation
Cons of Vorpal
- Heavier and more complex setup compared to zx
- Less focus on modern JavaScript features and async/await syntax
- Steeper learning curve for beginners
Code Comparison
Vorpal:
const vorpal = require('vorpal')();
vorpal
.command('hello <name>')
.action(function(args) {
this.log(`Hello, ${args.name}!`);
});
vorpal.delimiter('myapp$').show();
zx:
#!/usr/bin/env zx
let name = await question('What is your name? ');
console.log(`Hello, ${name}!`);
Vorpal is designed for building full-featured CLI applications with complex command structures, while zx focuses on simplifying shell scripting with JavaScript. Vorpal requires more setup but offers robust features for large-scale CLI tools. zx, on the other hand, provides a more straightforward approach for quick scripts and automation tasks, leveraging modern JavaScript syntax and built-in shell command execution.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
zx
#!/usr/bin/env zx
await $`cat package.json | grep name`
const branch = await $`git branch --show-current`
await $`dep deploy --branch=${branch}`
await Promise.all([
$`sleep 1; echo 1`,
$`sleep 2; echo 2`,
$`sleep 3; echo 3`,
])
const name = 'foo bar'
await $`mkdir /tmp/${name}`
Bash is great, but when it comes to writing more complex scripts,
many people prefer a more convenient programming language.
JavaScript is a perfect choice, but the Node.js standard library
requires additional hassle before using. The zx
package provides
useful wrappers around child_process
, escapes arguments and
gives sensible defaults.
Install
npm install zx
Documentation
Read documentation on google.github.io/zx.
License
Disclaimer: This is not an officially supported Google product.
Top Related Projects
Process execution for humans
node.js command-line interfaces made easy
yargs the modern, pirate-themed successor to optimist.
:shell: Portable Unix shell commands for Node.js
Node's framework for interactive CLIs
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot