2019-05-21 18:38:29 +00:00
|
|
|
import * as os from 'os'
|
2020-09-23 15:19:20 +00:00
|
|
|
import {toCommandValue} from './utils'
|
2019-05-21 18:38:29 +00:00
|
|
|
|
2019-05-21 18:44:37 +00:00
|
|
|
// For internal use, subject to change.
|
|
|
|
|
2020-04-09 21:00:53 +00:00
|
|
|
// We use any as a valid input type
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
2021-07-28 21:34:31 +00:00
|
|
|
export interface CommandProperties {
|
2020-04-09 21:00:53 +00:00
|
|
|
[key: string]: any
|
2019-05-21 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 18:38:29 +00:00
|
|
|
/**
|
|
|
|
* Commands
|
|
|
|
*
|
|
|
|
* Command Format:
|
2020-01-19 04:52:44 +00:00
|
|
|
* ::name key=value,key=value::message
|
2019-05-21 18:38:29 +00:00
|
|
|
*
|
|
|
|
* Examples:
|
2020-01-19 04:52:44 +00:00
|
|
|
* ::warning::This is the message
|
|
|
|
* ::set-env name=MY_VAR::some value
|
2019-05-21 18:38:29 +00:00
|
|
|
*/
|
|
|
|
export function issueCommand(
|
|
|
|
command: string,
|
2019-05-21 21:08:25 +00:00
|
|
|
properties: CommandProperties,
|
2020-04-09 21:00:53 +00:00
|
|
|
message: any
|
2019-05-21 21:08:25 +00:00
|
|
|
): void {
|
2019-05-21 18:38:29 +00:00
|
|
|
const cmd = new Command(command, properties, message)
|
|
|
|
process.stdout.write(cmd.toString() + os.EOL)
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:19:40 +00:00
|
|
|
export function issue(name: string, message = ''): void {
|
2019-05-21 18:38:29 +00:00
|
|
|
issueCommand(name, {}, message)
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:46:17 +00:00
|
|
|
const CMD_STRING = '::'
|
2019-05-21 18:38:29 +00:00
|
|
|
|
|
|
|
class Command {
|
2019-05-21 21:08:25 +00:00
|
|
|
private readonly command: string
|
|
|
|
private readonly message: string
|
|
|
|
private readonly properties: CommandProperties
|
|
|
|
|
|
|
|
constructor(command: string, properties: CommandProperties, message: string) {
|
2019-05-21 18:38:29 +00:00
|
|
|
if (!command) {
|
|
|
|
command = 'missing.command'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.command = command
|
|
|
|
this.properties = properties
|
|
|
|
this.message = message
|
|
|
|
}
|
|
|
|
|
2019-05-21 21:08:25 +00:00
|
|
|
toString(): string {
|
2019-09-09 15:46:17 +00:00
|
|
|
let cmdStr = CMD_STRING + this.command
|
2019-05-21 18:38:29 +00:00
|
|
|
|
|
|
|
if (this.properties && Object.keys(this.properties).length > 0) {
|
|
|
|
cmdStr += ' '
|
2019-12-18 18:23:16 +00:00
|
|
|
let first = true
|
2019-05-21 18:38:29 +00:00
|
|
|
for (const key in this.properties) {
|
|
|
|
if (this.properties.hasOwnProperty(key)) {
|
|
|
|
const val = this.properties[key]
|
|
|
|
if (val) {
|
2019-12-18 18:23:16 +00:00
|
|
|
if (first) {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
cmdStr += ','
|
|
|
|
}
|
|
|
|
|
2020-01-19 04:52:44 +00:00
|
|
|
cmdStr += `${key}=${escapeProperty(val)}`
|
2019-05-21 18:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 04:52:44 +00:00
|
|
|
cmdStr += `${CMD_STRING}${escapeData(this.message)}`
|
2019-05-21 18:38:29 +00:00
|
|
|
return cmdStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:00:53 +00:00
|
|
|
function escapeData(s: any): string {
|
|
|
|
return toCommandValue(s)
|
2020-01-19 04:52:44 +00:00
|
|
|
.replace(/%/g, '%25')
|
|
|
|
.replace(/\r/g, '%0D')
|
|
|
|
.replace(/\n/g, '%0A')
|
2019-05-21 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 21:00:53 +00:00
|
|
|
function escapeProperty(s: any): string {
|
|
|
|
return toCommandValue(s)
|
2020-01-19 04:52:44 +00:00
|
|
|
.replace(/%/g, '%25')
|
2019-05-21 18:38:29 +00:00
|
|
|
.replace(/\r/g, '%0D')
|
|
|
|
.replace(/\n/g, '%0A')
|
2020-01-19 04:52:44 +00:00
|
|
|
.replace(/:/g, '%3A')
|
|
|
|
.replace(/,/g, '%2C')
|
2019-05-21 18:45:27 +00:00
|
|
|
}
|