Устали писать и поддерживать несколько сценариев командной строки (оболочка / пакет) для различных платформ (Ubuntu / Windows / Mac)?
Вот универсальное решение, реализованное в Node.js для облегчения вашей жизни - Shelljs.
Вы не только убедитесь, что одна копия скрипта работает на всех платформах, но и не будете беспокоиться о приобретении опыта работы с несколькими языками скриптов.
Предпосылки -
Знания -
- Базовые знания JavaScript.
- Знание сценариев оболочки.
Инструменты -
- Node.js
- Npm (поставляется в комплекте с Node.js)
Настраивать -
- Создайте каталог для проекта скриптов
- Перейдите в каталог проекта скриптов.
- Инициализируйте npm в данном проекте с помощью npm init. Примите все значения по умолчанию, нажимая Enter на каждом шаге
- Установите shelljs как зависимость npm
mkdir shell-scripts cd shell-scripts/ npm install --save shelljs npm init
- В каталоге будет создан файл package.json.
{ "name": "shell-scripts", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "shelljs": "^0.8.3" } }
- Структура каталогов будет выглядеть так -
Сценарии -
- Основные команды (pwd, cd, echo, mkdir). Вот программа, показывающая часто используемые команды.
Выход -
shell-scripts harshitsinha$ node test_script.js Current Directory /Users/harshitsinha/Desktop/random_tests/shell-scripts Navigating to temp folder inside current directory New Current Directory /Users/harshitsinha/Desktop/random_tests/shell-scripts/temp Navigating back to project root Current Directory /Users/harshitsinha/Desktop/random_tests/shell-scripts Files/Folders in Directory /Users/harshitsinha/Desktop/random_tests/shell-scripts - node_modules - package.json - temp - test_script.js Making a directory name tempDir
- Дополнительные команды (grep, cp, cat, sed)
Выход -
shell-scripts$ node test_script2.js Grepped lines with word total Line - total is used for testing purposes Line - We will again drop a total here Line - Copying file ./temp/file1.txt to ./temp/file1-copy.txt Replacing in place all occurences of 'if' with 'but' in file Displaying text of file using cat Hi this is a sample text file total is used for testing purposes We will again drop a total here if we can keep on continuing this difficult process we will have a good test example. Grep lines with word 'total' from ./temp/file1.txt, then replace 'total' with 'totalReplaced' then output final file to ./temp/out.txt
Дополнительные примеры см.