npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@universal-packages/sub-process

v1.15.10

Published

Process encapsulation for different exec technics

Downloads

52,122

Readme

Sub SubProcess

npm version Testing codecov

Sub process encapsulation for different exec technics.

Install

npm install @universal-packages/sub-process

SubProcess

SubProcess is the main interface to setup a process to run at whenever time and only once.

import { SubProcess } from '@universal-packages/sub-process'

const subProcess = new SubProcess({ command: 'echo', args: ['$VARIABLE'], env: { VARIABLE: 'value' } })

await subProcess.run()

console.log(subProcess.stdout.toString())

Options

  • args string[] Arguments to pass to the command.
  • command string Command to run.
  • engine Engine | 'spawn' | 'exec' | 'fork' | 'test' default: spawn Instance of the engine to be used to execute the process or a string identifying the engine adapter.
  • engineOptions Object Options to pass to the engine if resolved as adapter.
  • env Object Environment variables to pass to the process.
  • input string | Buffer | string[] | Buffer[] | Readable Input to pass to the process. For example when a process requires any kind of input like a yes/no question.
  • timeout number Time to wait in milliseconds before killing the process.
  • workingDirectory string Working directory to run the process in.

Instance methods

prepare() async

Initialize the internal engine in case it needs preparation.

release() async

Releases the engine resources in case they need to be disposed after finishing the process.

run() async

Runs the process and waits for it to finish.

kill([signal: string]) async

Kills the process if it is running.

skip([reason: string])

Skips the process if it has not been started yet to mark it as skipped and do not let it run.

waitForStatus(status: string) async

Waits for the process to reach a specific status or an status in the same level, for example success or failure will wait for the process to finish with any of those statuses.

Instance properties

stdout string

Buffer containing the stdout of the process.

stderr string

Buffer containing the stderr of the process.

exitCode number

Exit code of the process.

signal string

Signal that killed the process.

processId number

Process id of the process.

status idle | running | success | error | failure | killed | stopped | killing | stopping

Status of the process.

Events

SubProcess will emit events regarding execution status and output.

subProcess.on('*', (event) => console.log(event))
subProcess.on('running', (event) => console.log(event))
subProcess.on('stdout', (event) => console.log(event))
subProcess.on('stderr', (event) => console.log(event))
subProcess.on('success', (event) => console.log(event))
subProcess.on('failure', (event) => console.log(event))
subProcess.on('stopping', (event) => console.log(event))
subProcess.on('stopped', (event) => console.log(event))
subProcess.on('skipped', (event) => console.log(event))
subProcess.on('end', (event) => console.log(event))
subProcess.on('timeout', (event) => console.log(event))
subProcess.on('error', (event) => console.log(event))
subProcess.on('warning', (event) => console.log(event))

Engine

To create an engine that suits your requirements you just need to implement new classes and use them as the following:

import MyEngine from './MyEngine'

const subProcess = new SubProcess({ engine: new MyEngine() })

You need to implement a engine process representation by subclassify the EngineProcess class to provide a way to kill yur custom process.

import { EngineProcess } from '@universal-packages/sub-process'

export default class MyEngineProcess extends EngineProcess {
  killObject(signal) {
    this.object.sendKillSignal(signal)
  }
}

The run method of the engine will be called with the command, args, input and env to execute the process and return an EngineProcess instance.

export default class MyEngine {
  constructor(options) {
    // Options passed through the adapters sub system
  }

  prepare() {
    // Initialize any connection using options
  }

  release() {
    // Release any resources or close any connection
  }

  run(command, args, input, env) {
    const myExecutableObject = myExecutionMethod.exec(command, args, input, env)
    const engineProcess = new MyEngineProcess(myExecutableObject.processId, myExecutableObject)

    // Now the SubProcess instance knows how to kill the process when needed as well as the process id.
    return engineProcess
  }
}

EngineInterface

If you are using TypeScript just implement the EngineInterface in your class to ensure the right implementation.

import { EngineInterface } from '@universal-packages/sub-process'

export default class MyEngine implements EngineInterface {}

Typescript

This library is developed in TypeScript and shipped fully typed.

Contributing

The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.

License

MIT licensed.