UNPKG

teamcity-service-messages

Version:
86 lines (85 loc) 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = void 0; const settings_1 = require("./settings"); /** * Constructs a message formatted for consumption by TeamCity. */ class Message { constructor(type, args) { this.type = type; this.single = false; // Message is a 'multiple attribute message'. if (typeof args === 'object' || typeof args === 'undefined') { this.args = args || {}; } // Message is a 'single attribute message'. else { this.single = true; this.args = args; } if (!this.single) { const currentArgs = this.args; if (currentArgs.flowId) { this.arg('flowId', currentArgs.flowId); } else if (settings_1.settings.autoFlowId) { this.arg('flowId', Message.flowId); } } } /** * Add a keyword argument to the message. */ arg(key, value) { if (this.single) { throw new Error('Cannot add arguments to a single attribute message.'); } this.args[key] = value; return this; } /** * Escape a string for TeamCity output. * @see https://www.jetbrains.com/help/teamcity/service-messages.html */ escape(str) { if (str == null) { return ''; } return String(str) .replace(/\|/g, '||') .replace(/\n/g, '|n') .replace(/\r/g, '|r') .replace(/\[/g, '|[') .replace(/\]/g, '|]') .replace(/\u0085/g, '|x') // next line .replace(/\u2028/g, '|l') // line separator .replace(/\u2029/g, '|p') // paragraph separator .replace(/'/g, "|'"); } /** * Format keyword arguments for use in a message. */ formatArgs() { const args = this.args; return Object.keys(args) .map((key) => `${key}='${this.escape(args[key])}'`) .join(' '); } /** * Format the message as a string. */ toString() { if (this.single) { return `##teamcity[${this.type} '${this.escape(this.args)}']`; } const args = this.args; if (!args.timestamp) { // TeamCity does not fully support ISO 8601 (see TW-36173) so we cut the trailing 'Z'. this.arg('timestamp', new Date().toISOString().slice(0, -1)); } return `##teamcity[${this.type} ${this.formatArgs()}]`; } } exports.Message = Message; Message.flowId = Math.floor(Math.random() * (1e10 - 1e6 + 1)) + 1e6;