All files / src/models utils.js

87.17% Statements 34/39
80% Branches 4/5
66.66% Functions 2/3
87.17% Lines 34/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 40193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 11x 11x 11x 11x 11x 193x 193x 193x 193x 193x 193x 193x 193x 193x 4x 4x 4x 4x 4x 4x 4x 193x 193x            
import openPage from 'open';
import { config } from '../config/config.js';
import { Logger } from '../logger.js';
 
// Inspirations:
// https://github.com/sindresorhus/p-defer/blob/master/index.js
// https://github.com/ljharb/promise-deferred/blob/master/index.js
 
// When you mix async/await APIs with event emitters callbacks, it's hard to keep a proper error flow without a good old deferred.
export class Deferred {
  constructor() {
    this.promise = new Promise((resolve, reject) => {
      this.resolve = resolve;
      this.reject = reject;
    });
  }
}
 
/**
 * Open an absolute URL or a console path in the default browser
 * @param {string} urlOrPath The URL to open
 * @param {string} message The message to display before opening the URL
 * @returns {Promise<void>} A promise that resolves when the URL is opened
 */
export function openBrowser(urlOrPath, message) {
  const url = urlOrPath.startsWith('/') ? `${config.CONSOLE_URL}${urlOrPath}` : urlOrPath;
 
  Logger.debug(`Opening URL "${url}" in browser`);
  Logger.println(`🌐 ${message}`);
 
  return openPage(url, { wait: false });
}
 
export function truncateWithEllipsis(length, string) {
  if (string.length > length - 1) {
    return string.substring(0, length - 1) + '…';
  }
  return string;
}