-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
70 lines (58 loc) · 1.61 KB
/
command.js
File metadata and controls
70 lines (58 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { EOL } from 'node:os'
export function issueCommand(command, properties, message) {
const cmd = new Command(command, properties, message)
process.stdout.write(cmd.toString() + EOL)
}
export function issue(name, message = '') {
issueCommand(name, {}, message)
}
const CMD_STRING = '::'
class Command {
#command
#message
#properties
constructor(command, properties, message) {
if (!command) command = 'missing.command'
this.#command = command
this.#properties = properties
this.#message = message
}
toString() {
let cmdStr = CMD_STRING + this.#command
if (this.#properties && Object.keys(this.#properties).length > 0) {
cmdStr += ' '
let first = true
for (const key in this.#properties) {
if (this.#properties.hasOwnProperty(key)) {
const val = this.#properties[key]
if (val) {
if (first) first = false
else cmdStr += ','
cmdStr += `${key}=${escapeProperty(val)}`
}
}
}
}
cmdStr += `${CMD_STRING}${escapeData(this.#message)}`
return cmdStr
}
}
function escapeData(s) {
return toCommandValue(s)
.replace(/%/g, '%25')
.replace(/\r/g, '%0D')
.replace(/\n/g, '%0A')
}
function escapeProperty(s) {
return toCommandValue(s)
.replace(/%/g, '%25')
.replace(/\r/g, '%0D')
.replace(/\n/g, '%0A')
.replace(/:/g, '%3A')
.replace(/,/g, '%2C')
}
export function toCommandValue(input) {
if (input === null || input === undefined) return ''
else if (typeof input === 'string' || input instanceof String) return input
return JSON.stringify(input)
}