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

pipe-to-powershell

v1.0.1

Published

Pipe commands to a Powershell instance and get results as JSON

Downloads

5

Readme

Pipe to Powershell

Pipe commands to a powershell instance and get results as JSON

Installation

npm i pipe-to-powershell

FNs

createPowershellStream(returnCallback|null, config? = CONFIG_DEFAULT): pipeControl

Initializes (spawns) the powershell.

returnCallback = ( pipeResults: {data: JSON|null, errors: string[]|null} ): void

  • If you do not want to use returnCallback (like only using get()), you may deliberately pass null.

pipeControl = {close, exec, get} ... see below

CONFIG_DEFAULT =

{
	bin: string = 'pwsh' /*executable binary*/, 
	stream: stream.Stream|null = stream.Readable /*input stream*/, 
	execMode: 'brackets'|null = 'brackets' /*see notes*/
}
  • In case powershell changes its prompts, additional config properties {delim = 'PS ', delimMultiline = '>> '} can be overwritten.

  • You might want to use get() and change to an older powershell:

    let {close, get} = createPowershellStream(null, {bin: 'powershell.exe'});

close(): void

Closes the pipes and Kills the spawned powershell.

exec(cmdStr: String): void

Trigger a command an let the resultCallback handle it. Fire and forget.

get(cmdStr: String): Promise<pipeResults>

Trigger a command and waits for its result, temporarely rerouting the resultCallback.

Should be awaited to mitigate race conditions. Running in paralell is not supported due to the nature of piping and rerouting the resultCallback.

Does not need returnCallback, during initialisation the param can be set to null. You should be using close(), when you are done.

// get result and all errors produced
let {data, error} = await get('ls');

// this will decunstruct pipeResults, extracting data, and provide it as variable 'files'
let {data: files} = await get('ls');

pushing to the input pipe

You can always use inputStream.push(string), but note, your command should end with \n

Debugging

Environment Parameter

DEBUG_PIPETOPOWERSHELL=true

Within code:

process.env.DEBUG_PIPETOPOWERSHELL = true;

Notes

  • Variable expressions should always be in brackets, which is automatically done by config.execMode: ($a = "Hello") - because the conversion to JSON for the result would save the JSON encoded version (with extra quotes).

Examples

Terminal input to powershell, try: npm start

import createPowershellStream from 'pipe-to-powershell';


process.stdout.write('press ^D to end \n:: ');

// create a piped commandline with custom prompt 
let {close, call, get} = createPowershellStream(({data, errors}) => {
	// show powershell result
	console.log('ERRORS:', errors, 'DATA:', data);

	// show a custom promt for next input
	process.stdout.write(':: ');
}, {stream: process.stdin}); // process.stdin or null (null == string pipe)

Trigger a few commands on powershell and get results, while piping to the same instance, try npm test

import createPowershellStream from 'pipe-to-powershell';


// create a piped commandline with custom prompt - no callback needed
let {close, get} = createPowershellStream(null);

(async _ => {

	let {data: files} = await get('ls');

	console.log('Files in this folder: ', files.length);

	let {data: ver} = await get('$PSVersionTable.PSVersion');

	console.log(`PS Version: ${ver.Major}.${ver.Minor}.${ver.Patch}`);

	close();
})();

Consecutive manipulates to the powershell environment: changing variables and relying on the previous changes, try npm run test3

import createPowershellStream from 'pipe-to-powershell';


// create a piped commandline with custom prompt - no callback needed
let {close, get} = createPowershellStream(null);

// we will NOT use exec(), since it would not wait

(async _ => {

	await get(`$a = "Hello"`);

	let ret = await get('$a');

	console.log('Value: ', ret.data);

	await get(`$a = "$($a) World!"`);

	ret = await get('$a');

	console.log('Value: ', ret.data);

	close();
})();