Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
luketomlinson committed May 6, 2021
1 parent f888302 commit a02735d
Showing 1 changed file with 109 additions and 107 deletions.
216 changes: 109 additions & 107 deletions packages/exec/src/toolrunner.ts
Expand Up @@ -415,134 +415,136 @@ export class ToolRunner extends events.EventEmitter {
this.toolPath = await io.which(this.toolPath, true)

return new Promise<number>(async (resolve, reject) => {
this._debug(`exec tool: ${this.toolPath}`)
this._debug('arguments:')
for (const arg of this.args) {
this._debug(` ${arg}`)
}

this.on('error', (error: Error) => {
reject(error)
})

this._debug(`exec tool: ${this.toolPath}`)
this._debug('arguments:')
for (const arg of this.args) {
this._debug(` ${arg}`)
}

const optionsNonNull = this._cloneExecOptions(this.options)
if (!optionsNonNull.silent && optionsNonNull.outStream) {
optionsNonNull.outStream.write(
this._getCommandString(optionsNonNull) + os.EOL
)
}
this.on('error', (error: Error) => {
reject(error)
})

const state = new ExecState(optionsNonNull, this.toolPath)
state.on('debug', (message: string) => {
this._debug(message)
})
const optionsNonNull = this._cloneExecOptions(this.options)
if (!optionsNonNull.silent && optionsNonNull.outStream) {
optionsNonNull.outStream.write(
this._getCommandString(optionsNonNull) + os.EOL
)
}

if (this.options.cwd && !(await ioUtil.exists(this.options.cwd))) {
this.emit('error', new Error(`The cwd: ${this.options.cwd} does not exist!`))
return
}
const state = new ExecState(optionsNonNull, this.toolPath)
state.on('debug', (message: string) => {
this._debug(message)
})

const fileName = this._getSpawnFileName()
const cp = child.spawn(
fileName,
this._getSpawnArgs(optionsNonNull),
this._getSpawnOptions(this.options, fileName)
if (this.options.cwd && !(await ioUtil.exists(this.options.cwd))) {
this.emit(
'error',
new Error(`The cwd: ${this.options.cwd} does not exist!`)
)
return
}

const stdbuffer = ''
if (cp.stdout) {
cp.stdout.on('data', (data: Buffer) => {
if (this.options.listeners && this.options.listeners.stdout) {
this.options.listeners.stdout(data)
}

if (!optionsNonNull.silent && optionsNonNull.outStream) {
optionsNonNull.outStream.write(data)
}
const fileName = this._getSpawnFileName()
const cp = child.spawn(
fileName,
this._getSpawnArgs(optionsNonNull),
this._getSpawnOptions(this.options, fileName)
)

this._processLineBuffer(data, stdbuffer, (line: string) => {
if (this.options.listeners && this.options.listeners.stdline) {
this.options.listeners.stdline(line)
}
})
})
}
const stdbuffer = ''
if (cp.stdout) {
cp.stdout.on('data', (data: Buffer) => {
if (this.options.listeners && this.options.listeners.stdout) {
this.options.listeners.stdout(data)
}

const errbuffer = ''
if (cp.stderr) {
cp.stderr.on('data', (data: Buffer) => {
state.processStderr = true
if (this.options.listeners && this.options.listeners.stderr) {
this.options.listeners.stderr(data)
}
if (!optionsNonNull.silent && optionsNonNull.outStream) {
optionsNonNull.outStream.write(data)
}

if (
!optionsNonNull.silent &&
optionsNonNull.errStream &&
optionsNonNull.outStream
) {
const s = optionsNonNull.failOnStdErr
? optionsNonNull.errStream
: optionsNonNull.outStream
s.write(data)
this._processLineBuffer(data, stdbuffer, (line: string) => {
if (this.options.listeners && this.options.listeners.stdline) {
this.options.listeners.stdline(line)
}

this._processLineBuffer(data, errbuffer, (line: string) => {
if (this.options.listeners && this.options.listeners.errline) {
this.options.listeners.errline(line)
}
})
})
}

cp.on('error', (err: Error) => {
state.processError = err.message
state.processExited = true
state.processClosed = true
state.CheckComplete()
})

cp.on('exit', (code: number) => {
state.processExitCode = code
state.processExited = true
this._debug(`Exit code ${code} received from tool '${this.toolPath}'`)
state.CheckComplete()
})

cp.on('close', (code: number) => {
state.processExitCode = code
state.processExited = true
state.processClosed = true
this._debug(`STDIO streams have closed for tool '${this.toolPath}'`)
state.CheckComplete()
})
}

state.on('done', (error: Error, exitCode: number) => {
if (stdbuffer.length > 0) {
this.emit('stdline', stdbuffer)
const errbuffer = ''
if (cp.stderr) {
cp.stderr.on('data', (data: Buffer) => {
state.processStderr = true
if (this.options.listeners && this.options.listeners.stderr) {
this.options.listeners.stderr(data)
}

if (errbuffer.length > 0) {
this.emit('errline', errbuffer)
if (
!optionsNonNull.silent &&
optionsNonNull.errStream &&
optionsNonNull.outStream
) {
const s = optionsNonNull.failOnStdErr
? optionsNonNull.errStream
: optionsNonNull.outStream
s.write(data)
}

cp.removeAllListeners()

if (error) {
reject(error)
} else {
resolve(exitCode)
}
this._processLineBuffer(data, errbuffer, (line: string) => {
if (this.options.listeners && this.options.listeners.errline) {
this.options.listeners.errline(line)
}
})
})
}

if (this.options.input) {
if (!cp.stdin) {
throw new Error('child process missing stdin')
}
cp.on('error', (err: Error) => {
state.processError = err.message
state.processExited = true
state.processClosed = true
state.CheckComplete()
})

cp.on('exit', (code: number) => {
state.processExitCode = code
state.processExited = true
this._debug(`Exit code ${code} received from tool '${this.toolPath}'`)
state.CheckComplete()
})

cp.on('close', (code: number) => {
state.processExitCode = code
state.processExited = true
state.processClosed = true
this._debug(`STDIO streams have closed for tool '${this.toolPath}'`)
state.CheckComplete()
})

state.on('done', (error: Error, exitCode: number) => {
if (stdbuffer.length > 0) {
this.emit('stdline', stdbuffer)
}

if (errbuffer.length > 0) {
this.emit('errline', errbuffer)
}

cp.removeAllListeners()

cp.stdin.end(this.options.input)
if (error) {
reject(error)
} else {
resolve(exitCode)
}
})

if (this.options.input) {
if (!cp.stdin) {
throw new Error('child process missing stdin')
}

cp.stdin.end(this.options.input)
}
})
}
}
Expand Down

0 comments on commit a02735d

Please sign in to comment.