child-shell icon indicating copy to clipboard operation
child-shell copied to clipboard

Piping commands?

Open favna opened this issue 4 years ago • 0 comments

Given the following PowerShell command

Get-ChildItem -Path $env:USERPROFILE\Documents\development -Filter node_modules -Directory -Recurse | Remove-Item -Force -Recurse

This would remove all node_modules folders in development, recursing into subdirectories.

How would I best write this in node-powershell? I can do it with

addCommand(`Get-ChildItem -Path $env:USERPROFILE\Documents\development -Filter node_modules -Directory -Recurse | Remove-Item -Force -Recurse`)

But I would prefer to be able to do it with something like

const ps = new shell({
	executionPolicy: 'Bypass',
	noProfile: true
});

await ps.addCommand('Get-ChildItem');

await ps.addParameters([
	{ Path: '$env:USERPROFILE\Documents\development' },
	{ Filter: 'node_modules' },
	{ Directory: '' },
	{ Recurse: '' }
]);

// Adds ` | Remove-Item`
await ps.addPipedCommand('Remove-Item');

// Adding parameters to `Remove-Item`
await ps.addParameters([
	{ Force: '' },
	{ Recurse: '' }
]);

favna avatar Jan 07 '21 16:01 favna