We are in the process of transforming the functionality of PSRunner into a TypeScript function:
export function PSRunner(commands: string[]) {
const self: {
out: string[]
err: string[]
} = this
const results: { command: string; output: any; errors: any }[] = []
const child = spawn('powershell.exe', ['-Command', '-'])
child.stdout.on('data', function (data) {
self.out.push(data.toString())
})
child.stderr.on('data', function (data) {
self.err.push(data.toString())
})
commands.forEach(function (cmd) {
self.out = []
self.err = []
child.stdin.write(cmd + '\n')
results.push({ command: cmd, output: self.out, errors: self.err })
})
child.stdin.end()
return results
}
I find myself a bit perplexed by how this
is functioning here. A new object named self
is defined to hold data from child.stdout
and child.stderr
. However, within the foreach
, both this.out
and this.err
are reset to empty arrays. This raises the question of how results
can retain the values specific to each command. Perhaps using a fat arrow function to bypass the need for this
could be a solution, but it might be necessary in this case?
Additionally, there are some TypeScript errors due to the use of any
. Nevertheless, my primary focus is on grasping the mechanics of this code. Any clarification provided would be greatly appreciated.