|
| 1 | +import dedent from 'dedent-js'; |
| 2 | +import kleur from 'kleur'; |
| 3 | + |
| 4 | +export const isCi = process.env.CI; |
| 5 | + |
| 6 | +/** |
| 7 | + * @param {any} [message] |
| 8 | + * @param {...any} params |
| 9 | + */ |
| 10 | +export function debug(message, ...params) { |
| 11 | + console.log(message, ...params); |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * @param {any} [message] |
| 16 | + * @param {...any} params |
| 17 | + */ |
| 18 | +export function warning(message, ...params) { |
| 19 | + console.warn(kleur.yellow().bold(`*** WARNING: ${message}`), ...params); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * @param {any} [message] |
| 24 | + * @param {...any} params |
| 25 | + */ |
| 26 | +export function error(message, ...params) { |
| 27 | + console.error(kleur.red().bold(`*** ERROR: ${message}`), ...params); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Dedents the given markdown and replaces single newlines with spaces, |
| 32 | + * while leaving new paragraphs intact. |
| 33 | + */ |
| 34 | +export function dedentMd(...markdown) { |
| 35 | + return dedent(...markdown).replace(/(\S)\n(?!\n)/g, '$1 '); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Formats the given `template` based on `count`, picking the correct plural |
| 40 | + * or singular form from the template. |
| 41 | + * |
| 42 | + * - If `count` is defined, it also prepends the template with `count`. |
| 43 | + * - Example: "broken link(s)" --> "1 broken link" / "2 broken links" |
| 44 | + * - If `count` is undefined, it capitalizes the first letter. |
| 45 | + * - Example: "broken link(s)" --> "Broken link" |
| 46 | + * |
| 47 | + * Supported template syntax: |
| 48 | + * - Use `(s)` to add a plural-only "s", e.g. "broken link(s)" |
| 49 | + * - Use `|` to provide separate templates, e.g. "issue was|issues were" |
| 50 | + * |
| 51 | + * @param {number} count |
| 52 | + * @param {string} template |
| 53 | + */ |
| 54 | +export function formatCount(count, template) { |
| 55 | + /** @param {string} text */ |
| 56 | + const wrapWithCount = (text) => { |
| 57 | + // If no count was given, we're outputting a single issue in annotations, |
| 58 | + // so omit count and capitalize the first letter of the issue type description |
| 59 | + if (count === undefined) return text[0].toUpperCase() + text.slice(1); |
| 60 | + |
| 61 | + // Otherwise, prefix the issue type description with count |
| 62 | + return `${count} ${text}`; |
| 63 | + }; |
| 64 | + |
| 65 | + const usePlural = count !== undefined && count !== 1; |
| 66 | + const templateParts = template.split('|'); |
| 67 | + const usedTemplate = |
| 68 | + templateParts.length === 2 ? templateParts[usePlural ? 1 : 0] : template; |
| 69 | + return wrapWithCount(usedTemplate.replace(/\(s\)/g, usePlural ? 's' : '')); |
| 70 | +} |
| 71 | + |
| 72 | +export default { |
| 73 | + debug, |
| 74 | + warning, |
| 75 | + error, |
| 76 | + dedentMd, |
| 77 | + formatCount, |
| 78 | + isCi, |
| 79 | +}; |
0 commit comments