1.0.2 • Published 1 year ago

systemcaller v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

SystemCaller

SystemCaller is a TypeScript library that provides a convenient way to execute system commands using Node.js. It simplifies the process of running commands and handling their output.

Features

  • Execute system commands with ease
  • Option to attach stdio for real-time output display
  • Capture the output of commands for further processing
  • Customize working directory and other execution options

Installation

You can install SystemCaller via npm or yarn:

npm install systemcaller

or

yarn add systemcaller

Usage

Here's how you can use SystemCaller in your TypeScript/JavaScript project:

import SystemCaller from 'systemcaller';

async function main() {
  const systemCaller = new SystemCaller();

  try {
    // Execute a command and capture the output
    const output = await systemCaller.call('echo', ['hello', 'world']);

    // Display the output
    console.log(output);
  } catch (error) {
    console.error('Error executing command:', error);
  }
}

main();

In the example above, we create a new instance of SystemCaller and use the call method to execute the echo command with the arguments hello and world. The output of the command is captured and displayed.

Attaching stdio

By default, stdio is not attached, which means the output of the command is captured and returned as a string. However, you can choose to attach stdio to display the output in real-time. Here's an example:

import SystemCaller from 'systemcaller';

async function main() {
  const systemCaller = new SystemCaller(true); // Attach stdio

  try {
    // Execute a command with stdio attached
    await systemCaller.call('ls', ['-l']);
  } catch (error) {
    console.error('Error executing command:', error);
  }
}

main();

In this example, we create a new instance of SystemCaller with true as the argument, indicating that we want to attach stdio. As a result, the output of the ls -l command will be displayed in the console.

Customizing execution options

You can customize various execution options when using SystemCaller. For example, you can specify a custom working directory or pass additional options supported by the spawn function from the child_process module. Here's an example:

import SystemCaller from 'systemcaller';

async function main() {
  const systemCaller = new SystemCaller();

  try {
    const options = {
      cwd: '/path/to/directory', // Custom working directory
      env: { DEBUG: 'true' }, // Additional environment variables
    };

    // Execute a command with custom options
    await systemCaller.call('npm', ['install'], options);
  } catch (error) {
    console.error('Error executing command:', error);
  }
}

main();

In this example, we specify a custom working directory (/path/to/directory) using the cwd option. We also pass additional environment variables (DEBUG: 'true') using the env option. You can refer to the spawn documentation for more information on available options.

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

When contributing, please ensure that your code adheres to the established coding conventions and that tests are included to validate the changes.

License

SystemCaller is MIT licensed.

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago