Add pipes to write pretty PowerShell #53
This commit introduces two pipes: `inlinePowerShell`, `escapeDoubleQuotes`. The types when used together allows writing adding clean and real PowerShell scripts as they are (without inlinining or escaping them), removing the need to have hard-coded inlining/escaping. It enables writing better PowerShell, makes it easier to maintain and extend PowerShell scripts. Also allows writing more stable code with less "unseen" bugs due to manual escaping/inlining. This commit naturally reveals and fixes double quotes not being escaped in "Empty trash bin" script. This is solved by unifying the use of RunPowerShell function by all scripts using PowerShell. The function inlines and escapes the scripts as compile time to be send them to PowerShell.exe as an argument and then invokes PowerShell.exe with generated ugly code.
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import { IPipe } from '../IPipe';
|
||||
|
||||
export class EscapeDoubleQuotes implements IPipe {
|
||||
public readonly name: string = 'escapeDoubleQuotes';
|
||||
public apply(raw: string): string {
|
||||
return raw?.replaceAll('"', '\\"');
|
||||
/*
|
||||
In batch, it also works with 4 double quotes but looks bloated
|
||||
An easy test: PowerShell -Command "Write-Host '\"Hello World\"'"
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { IPipe } from '../IPipe';
|
||||
|
||||
export class InlinePowerShell implements IPipe {
|
||||
public readonly name: string = 'inlinePowerShell';
|
||||
public apply(raw: string): string {
|
||||
return raw
|
||||
?.split(/\r\n|\r|\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.join('; ');
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
import { IPipe } from './IPipe';
|
||||
import { InlinePowerShell } from './PipeDefinitions/InlinePowerShell';
|
||||
import { EscapeDoubleQuotes } from './PipeDefinitions/EscapeDoubleQuotes';
|
||||
|
||||
const RegisteredPipes = [ ];
|
||||
const RegisteredPipes = [
|
||||
new EscapeDoubleQuotes(),
|
||||
new InlinePowerShell(),
|
||||
];
|
||||
|
||||
export interface IPipeFactory {
|
||||
get(pipeName: string): IPipe;
|
||||
|
||||
Reference in New Issue
Block a user