A library for executing shell commands using promises.
Go to file
2024-04-14 18:17:36 +02:00
.vscode fix(core): cosmetics 2021-11-26 15:17:52 +01:00
test fix(core): update 2024-03-16 11:18:53 +01:00
ts fix(core): update 2024-03-16 11:18:53 +01:00
.gitignore fix(core): update 2021-07-26 21:24:13 +02:00
.gitlab-ci.yml BREAKING CHANGE(core): switched to ES syntax and added support for interactivity 2023-06-22 11:51:44 +02:00
npmextra.json update tsconfig 2024-04-14 18:17:36 +02:00
package.json update tsconfig 2024-04-14 18:17:36 +02:00
pnpm-lock.yaml fix(core): update 2024-03-16 11:18:53 +01:00
readme.hints.md update tsconfig 2024-04-14 18:17:36 +02:00
readme.md update tsconfig 2024-04-14 18:17:36 +02:00
tsconfig.json fix(core): update 2024-03-16 11:18:53 +01:00

@push.rocks/smartshell

shell actions designed as promises

Install

To install @push.rocks/smartshell, use npm:

npm install @push.rocks/smartshell --save

Or if you prefer using Yarn:

yarn add @push.rocks/smartshell

Ensure that you have TypeScript and the related dependencies installed as well since @push.rocks/smartshell is designed to work with TypeScript.

Usage

The @push.rocks/smartshell package simplifies running shell commands within Node.js applications by wrapping these commands within promises. This approach enhances the readability and maintainability of code that relies on shell execution, making it particularly useful in automation scripts, build processes, and any scenario where interaction with the system shell is required.

Getting Started with @push.rocks/smartshell

First, ensure that you import Smartshell from @push.rocks/smartshell using ESM syntax in your TypeScript file:

import { Smartshell } from '@push.rocks/smartshell';

Creating a Smartshell Instance

Before executing any shell command, you need to create an instance of Smartshell. The constructor accepts configuration options such as the shell executor (bash or sh), and optionally, paths to source files and directories to include in the shells environment.

const smartShellInstance = new Smartshell({
  executor: 'bash', // or 'sh'
});

Executing Commands

Basic Execution

To execute a shell command, use the exec method. This method returns a promise that resolves with an execution result object containing exitCode and stdout.

(async () => {
  const result = await smartShellInstance.exec('echo "Hello, SmartShell"');
  console.log(result.stdout); // Outputs: Hello, SmartShell
})();

Silent Execution

If you prefer not to display the output in the console, use execSilent:

(async () => {
  const result = await smartShellInstance.execSilent('ls');
  console.log(result.stdout); // Outputs the list of files and directories
})();

Strict Execution

For scenarios where an execution error should immediately throw an exception, use execStrict:

(async () => {
  try {
    const result = await smartShellInstance.execStrict('exit 1');
  } catch (error) {
    console.error('Command execution failed');
  }
})();

Streaming Output

Some commands benefit from streaming output as they execute, especially long-running tasks. For these cases, use execStreaming:

(async () => {
  const execStreamingResult = await smartShellInstance.execStreaming('tail -f /var/log/system.log');
  
  execStreamingResult.childProcess.stdout.on('data', (data) => {
    console.log(data.toString());
  });

  // Remember to handle the process termination as necessary.
})();

Advanced Usage

Executing With Custom Environment Variables

smartshell allows for the execution of commands within a modified environment, facilitating the use of custom variables or altered PATH values:

(async () => {
  smartShellInstance.shellEnv.addSourceFiles(['/path/to/envFile']);
  smartShellInstance.shellEnv.pathDirArray.push('/custom/bin');
  
  const result = await smartShellInstance.exec('echo $CUSTOM_VAR');
  console.log(result.stdout); // Outputs the value of CUSTOM_VAR
})();

Interactive Mode

For commands that require interactive terminal input (not typically recommended for automated scripts), you can use execInteractive:

(async () => {
  await smartShellInstance.execInteractive('npm init');
})();

Waiting for Specific Output

To wait for a specific line before proceeding, you might use execAndWaitForLine. This is useful for waiting on a process to log a certain message:

(async () => {
  await smartShellInstance.execAndWaitForLine('npm run watch', /Compilation complete./);
  console.log('The watch process has finished compiling.');
})();

Given the vast array of features offered by @push.rocks/smartshell, integrating shell operations into your TypeScript applications becomes both straightforward and powerful. By harnessing promises and async/await syntax, smartshell effectively streamlines shell interactions, making your code cleaner and more intuitive.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.