github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@actions/core/lib/summary.js (about) 1 "use strict"; 2 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 4 return new (P || (P = Promise))(function (resolve, reject) { 5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } 6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } 7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } 8 step((generator = generator.apply(thisArg, _arguments || [])).next()); 9 }); 10 }; 11 Object.defineProperty(exports, "__esModule", { value: true }); 12 exports.summary = exports.markdownSummary = exports.SUMMARY_DOCS_URL = exports.SUMMARY_ENV_VAR = void 0; 13 const os_1 = require("os"); 14 const fs_1 = require("fs"); 15 const { access, appendFile, writeFile } = fs_1.promises; 16 exports.SUMMARY_ENV_VAR = 'GITHUB_STEP_SUMMARY'; 17 exports.SUMMARY_DOCS_URL = 'https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary'; 18 class Summary { 19 constructor() { 20 this._buffer = ''; 21 } 22 /** 23 * Finds the summary file path from the environment, rejects if env var is not found or file does not exist 24 * Also checks r/w permissions. 25 * 26 * @returns step summary file path 27 */ 28 filePath() { 29 return __awaiter(this, void 0, void 0, function* () { 30 if (this._filePath) { 31 return this._filePath; 32 } 33 const pathFromEnv = process.env[exports.SUMMARY_ENV_VAR]; 34 if (!pathFromEnv) { 35 throw new Error(`Unable to find environment variable for $${exports.SUMMARY_ENV_VAR}. Check if your runtime environment supports job summaries.`); 36 } 37 try { 38 yield access(pathFromEnv, fs_1.constants.R_OK | fs_1.constants.W_OK); 39 } 40 catch (_a) { 41 throw new Error(`Unable to access summary file: '${pathFromEnv}'. Check if the file has correct read/write permissions.`); 42 } 43 this._filePath = pathFromEnv; 44 return this._filePath; 45 }); 46 } 47 /** 48 * Wraps content in an HTML tag, adding any HTML attributes 49 * 50 * @param {string} tag HTML tag to wrap 51 * @param {string | null} content content within the tag 52 * @param {[attribute: string]: string} attrs key-value list of HTML attributes to add 53 * 54 * @returns {string} content wrapped in HTML element 55 */ 56 wrap(tag, content, attrs = {}) { 57 const htmlAttrs = Object.entries(attrs) 58 .map(([key, value]) => ` ${key}="${value}"`) 59 .join(''); 60 if (!content) { 61 return `<${tag}${htmlAttrs}>`; 62 } 63 return `<${tag}${htmlAttrs}>${content}</${tag}>`; 64 } 65 /** 66 * Writes text in the buffer to the summary buffer file and empties buffer. Will append by default. 67 * 68 * @param {SummaryWriteOptions} [options] (optional) options for write operation 69 * 70 * @returns {Promise<Summary>} summary instance 71 */ 72 write(options) { 73 return __awaiter(this, void 0, void 0, function* () { 74 const overwrite = !!(options === null || options === void 0 ? void 0 : options.overwrite); 75 const filePath = yield this.filePath(); 76 const writeFunc = overwrite ? writeFile : appendFile; 77 yield writeFunc(filePath, this._buffer, { encoding: 'utf8' }); 78 return this.emptyBuffer(); 79 }); 80 } 81 /** 82 * Clears the summary buffer and wipes the summary file 83 * 84 * @returns {Summary} summary instance 85 */ 86 clear() { 87 return __awaiter(this, void 0, void 0, function* () { 88 return this.emptyBuffer().write({ overwrite: true }); 89 }); 90 } 91 /** 92 * Returns the current summary buffer as a string 93 * 94 * @returns {string} string of summary buffer 95 */ 96 stringify() { 97 return this._buffer; 98 } 99 /** 100 * If the summary buffer is empty 101 * 102 * @returns {boolen} true if the buffer is empty 103 */ 104 isEmptyBuffer() { 105 return this._buffer.length === 0; 106 } 107 /** 108 * Resets the summary buffer without writing to summary file 109 * 110 * @returns {Summary} summary instance 111 */ 112 emptyBuffer() { 113 this._buffer = ''; 114 return this; 115 } 116 /** 117 * Adds raw text to the summary buffer 118 * 119 * @param {string} text content to add 120 * @param {boolean} [addEOL=false] (optional) append an EOL to the raw text (default: false) 121 * 122 * @returns {Summary} summary instance 123 */ 124 addRaw(text, addEOL = false) { 125 this._buffer += text; 126 return addEOL ? this.addEOL() : this; 127 } 128 /** 129 * Adds the operating system-specific end-of-line marker to the buffer 130 * 131 * @returns {Summary} summary instance 132 */ 133 addEOL() { 134 return this.addRaw(os_1.EOL); 135 } 136 /** 137 * Adds an HTML codeblock to the summary buffer 138 * 139 * @param {string} code content to render within fenced code block 140 * @param {string} lang (optional) language to syntax highlight code 141 * 142 * @returns {Summary} summary instance 143 */ 144 addCodeBlock(code, lang) { 145 const attrs = Object.assign({}, (lang && { lang })); 146 const element = this.wrap('pre', this.wrap('code', code), attrs); 147 return this.addRaw(element).addEOL(); 148 } 149 /** 150 * Adds an HTML list to the summary buffer 151 * 152 * @param {string[]} items list of items to render 153 * @param {boolean} [ordered=false] (optional) if the rendered list should be ordered or not (default: false) 154 * 155 * @returns {Summary} summary instance 156 */ 157 addList(items, ordered = false) { 158 const tag = ordered ? 'ol' : 'ul'; 159 const listItems = items.map(item => this.wrap('li', item)).join(''); 160 const element = this.wrap(tag, listItems); 161 return this.addRaw(element).addEOL(); 162 } 163 /** 164 * Adds an HTML table to the summary buffer 165 * 166 * @param {SummaryTableCell[]} rows table rows 167 * 168 * @returns {Summary} summary instance 169 */ 170 addTable(rows) { 171 const tableBody = rows 172 .map(row => { 173 const cells = row 174 .map(cell => { 175 if (typeof cell === 'string') { 176 return this.wrap('td', cell); 177 } 178 const { header, data, colspan, rowspan } = cell; 179 const tag = header ? 'th' : 'td'; 180 const attrs = Object.assign(Object.assign({}, (colspan && { colspan })), (rowspan && { rowspan })); 181 return this.wrap(tag, data, attrs); 182 }) 183 .join(''); 184 return this.wrap('tr', cells); 185 }) 186 .join(''); 187 const element = this.wrap('table', tableBody); 188 return this.addRaw(element).addEOL(); 189 } 190 /** 191 * Adds a collapsable HTML details element to the summary buffer 192 * 193 * @param {string} label text for the closed state 194 * @param {string} content collapsable content 195 * 196 * @returns {Summary} summary instance 197 */ 198 addDetails(label, content) { 199 const element = this.wrap('details', this.wrap('summary', label) + content); 200 return this.addRaw(element).addEOL(); 201 } 202 /** 203 * Adds an HTML image tag to the summary buffer 204 * 205 * @param {string} src path to the image you to embed 206 * @param {string} alt text description of the image 207 * @param {SummaryImageOptions} options (optional) addition image attributes 208 * 209 * @returns {Summary} summary instance 210 */ 211 addImage(src, alt, options) { 212 const { width, height } = options || {}; 213 const attrs = Object.assign(Object.assign({}, (width && { width })), (height && { height })); 214 const element = this.wrap('img', null, Object.assign({ src, alt }, attrs)); 215 return this.addRaw(element).addEOL(); 216 } 217 /** 218 * Adds an HTML section heading element 219 * 220 * @param {string} text heading text 221 * @param {number | string} [level=1] (optional) the heading level, default: 1 222 * 223 * @returns {Summary} summary instance 224 */ 225 addHeading(text, level) { 226 const tag = `h${level}`; 227 const allowedTag = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(tag) 228 ? tag 229 : 'h1'; 230 const element = this.wrap(allowedTag, text); 231 return this.addRaw(element).addEOL(); 232 } 233 /** 234 * Adds an HTML thematic break (<hr>) to the summary buffer 235 * 236 * @returns {Summary} summary instance 237 */ 238 addSeparator() { 239 const element = this.wrap('hr', null); 240 return this.addRaw(element).addEOL(); 241 } 242 /** 243 * Adds an HTML line break (<br>) to the summary buffer 244 * 245 * @returns {Summary} summary instance 246 */ 247 addBreak() { 248 const element = this.wrap('br', null); 249 return this.addRaw(element).addEOL(); 250 } 251 /** 252 * Adds an HTML blockquote to the summary buffer 253 * 254 * @param {string} text quote text 255 * @param {string} cite (optional) citation url 256 * 257 * @returns {Summary} summary instance 258 */ 259 addQuote(text, cite) { 260 const attrs = Object.assign({}, (cite && { cite })); 261 const element = this.wrap('blockquote', text, attrs); 262 return this.addRaw(element).addEOL(); 263 } 264 /** 265 * Adds an HTML anchor tag to the summary buffer 266 * 267 * @param {string} text link text/content 268 * @param {string} href hyperlink 269 * 270 * @returns {Summary} summary instance 271 */ 272 addLink(text, href) { 273 const element = this.wrap('a', text, { href }); 274 return this.addRaw(element).addEOL(); 275 } 276 } 277 const _summary = new Summary(); 278 /** 279 * @deprecated use `core.summary` 280 */ 281 exports.markdownSummary = _summary; 282 exports.summary = _summary; 283 //# sourceMappingURL=summary.js.map