Краткий справочник

ShellJS шпаргалка: Портативные команды Unix для Node.js

Краткий справочник по ShellJS: кроссплатформенные команды Unix для скриптов Node.js на Windows, macOS и Linux.

· Опубликовано

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

Связанные темы