Referência rápida

ShellJS: Comandos Unix portáteis para scripts Node.js Guia

Guia de referência rápida para ShellJS: comandos shell Unix portáteis para scripts Node.js no Windows, macOS e Linux.

· Publicado

Basic Commands

const shell = require('shelljs');

// Check binary availability
if (!shell.which('git')) {
  shell.echo('Sorry, this script requires git');
  shell.exit(1);
}

// Copy files
shell.cp('-R', 'source/', 'dest/');

// Remove files and directories
shell.rm('-rf', 'build/');

// Create directories recursively
shell.mkdir('-p', 'dist/assets/css');

Navigation & Executing Shell Binaries

// Navigation
shell.cd('dist');
console.log('Current directory:', shell.pwd().toString());

// Execute command synchronously
const result = shell.exec('git rev-parse HEAD');
if (result.code !== 0) {
  shell.echo('Git command failed');
} else {
  shell.echo(`Commit hash: ${result.stdout.trim()}`);
}

File Inspections & Text Processing

// Search text matching regex in files (like grep)
const matches = shell.grep('TODO', 'src/*.js');

// Pipe commands together
shell.cat('input.txt').grep('ERROR').to('errors.log');

References

Temas relacionados