How to use @lhci/utils - 10 common examples

To help you get started, we’ve selected a few @lhci/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GoogleChrome / lighthouse-ci / packages / cli / src / assert / assert.js View on Github external
async function runCommand(options) {
  const {budgetsFile, assertions, assertMatrix, preset} = options;
  const areAssertionsSet = Boolean(assertions || assertMatrix || preset);
  if (!areAssertionsSet && !budgetsFile) throw new Error('No assertions to use');
  if (budgetsFile && areAssertionsSet) throw new Error('Cannot use both budgets AND assertions');
  // If we have a budgets file, convert it to our assertions format.
  if (budgetsFile) options = convertBudgetsToAssertions(readBudgets(budgetsFile));

  const lhrs = loadSavedLHRs().map(json => JSON.parse(json));
  const uniqueUrls = new Set(lhrs.map(lhr => lhr.finalUrl));
  const allResults = getAllAssertionResults(options, lhrs);
  const groupedResults = _.groupBy(allResults, result => result.url);

  process.stderr.write(
    `Checking assertions against ${uniqueUrls.size} URL(s), ${lhrs.length} total run(s)\n\n`
  );

  let hasFailure = false;
  for (const results of groupedResults) {
    const url = results[0].url;
    const sortedResults = results.sort((a, b) => {
      const {level: levelA = 'error', auditId: auditIdA = 'unknown'} = a;
      const {level: levelB = 'error', auditId: auditIdB = 'unknown'} = b;
      return levelA.localeCompare(levelB) || auditIdA.localeCompare(auditIdB);
    });

    process.stderr.write(`${sortedResults.length} result(s) for ${log.bold}${url}${log.reset}\n`);
github GoogleChrome / lighthouse-ci / packages / cli / src / wizard / wizard.js View on Github external
},
    {
      type: 'input',
      name: 'projectName',
      message: 'What would you like to name the project?',
      validate: input => !!input.length,
    },
    {
      type: 'input',
      name: 'projectExternalUrl',
      message: "Where is the project's code hosted?",
      default: 'https://github.com//',
    },
  ]);

  const api = new ApiClient({rootURL: responses.serverBaseUrl || options.serverBaseUrl});
  const project = await api.createProject({
    name: responses.projectName,
    externalUrl: responses.projectExternalUrl,
    slug: '', // this property is dynamically generated server-side
  });

  const token = project.token;
  process.stdout.write(`Created project ${project.name} (${project.id})!\n`);
  process.stdout.write(`Use token ${log.bold}${token}${log.reset} to connect.\n`);
}
github GoogleChrome / lighthouse-ci / packages / cli / src / assert / assert.js View on Github external
async function runCommand(options) {
  const {budgetsFile, assertions, assertMatrix, preset} = options;
  const areAssertionsSet = Boolean(assertions || assertMatrix || preset);
  if (!areAssertionsSet && !budgetsFile) throw new Error('No assertions to use');
  if (budgetsFile && areAssertionsSet) throw new Error('Cannot use both budgets AND assertions');
  // If we have a budgets file, convert it to our assertions format.
  if (budgetsFile) options = convertBudgetsToAssertions(readBudgets(budgetsFile));

  const lhrs = loadSavedLHRs().map(json => JSON.parse(json));
  const uniqueUrls = new Set(lhrs.map(lhr => lhr.finalUrl));
  const allResults = getAllAssertionResults(options, lhrs);
  const groupedResults = _.groupBy(allResults, result => result.url);

  process.stderr.write(
    `Checking assertions against ${uniqueUrls.size} URL(s), ${lhrs.length} total run(s)\n\n`
  );

  let hasFailure = false;
  for (const results of groupedResults) {
    const url = results[0].url;
    const sortedResults = results.sort((a, b) => {
      const {level: levelA = 'error', auditId: auditIdA = 'unknown'} = a;
      const {level: levelB = 'error', auditId: auditIdB = 'unknown'} = b;
      return levelA.localeCompare(levelB) || auditIdA.localeCompare(auditIdB);
    });
github GoogleChrome / lighthouse-ci / packages / server / src / ui / components / gauge.jsx View on Github external
export const Gauge = props => {
  const score = Math.round(props.score * 100);
  const rawBaseScore = props.diff ? props.diff.baseValue : props.score;
  const baseScore = Math.round(rawBaseScore * 100);
  const label = props.diff ? getDiffLabel(props.diff) : 'neutral';

  // 352 is ~= 2 * Math.PI * gauge radius (56)
  // https://codepen.io/xgad/post/svg-radial-progress-meters
  // score of 50: `stroke-dasharray: 176 352`;
  // The roundcap on the arc makes it extend slightly past where it should, so we need to adjust it by a few pts.
  const baseDasharrayScore = Math.max(0, props.score * 352 - 2);
  const baseStrokeDasharray = `${baseDasharrayScore} 352`;
  const delta = Math.abs(baseScore - score);
  const deltaDasharrayScore = Math.max(0, (delta / 100) * 352 - 2);
  const deltaStrokeDasharray = `${deltaDasharrayScore} 352`;
  const deltaTransform = `rotate(${(Math.min(score, baseScore) / 100) * 360}deg)`;
  const indicatorTransform = `rotate(${props.score * 360}deg)`;

  return (
    <div>
      <div></div></div>
github GoogleChrome / lighthouse-ci / packages / cli / src / cli.js View on Github external
function createYargsConfigArguments() {
  const simpleArgv = yargsParser(process.argv.slice(2), {envPrefix: 'LHCI'});
  /** @type {[string, (path: string) => LHCI.YargsOptions]} */
  const configOption = ['config', loadAndParseRcFile];
  // If they're using the config option or opting out of auto-detection, use the config option.
  if (simpleArgv.config || hasOptedOutOfRcDetection()) return configOption;
  const rcFile = findRcFile();
  // If they don't currently have an rc file, use the config option for awareness.
  if (!rcFile) return configOption;
  return [loadAndParseRcFile(rcFile)];
}
github GoogleChrome / lighthouse-ci / packages / cli / src / cli.js View on Github external
function createYargsConfigArguments() {
  const simpleArgv = yargsParser(process.argv.slice(2), {envPrefix: 'LHCI'});
  /** @type {[string, (path: string) => LHCI.YargsOptions]} */
  const configOption = ['config', loadAndParseRcFile];
  // If they're using the config option or opting out of auto-detection, use the config option.
  if (simpleArgv.config || hasOptedOutOfRcDetection()) return configOption;
  const rcFile = findRcFile();
  // If they don't currently have an rc file, use the config option for awareness.
  if (!rcFile) return configOption;
  return [loadAndParseRcFile(rcFile)];
}
github GoogleChrome / lighthouse-ci / packages / cli / src / cli.js View on Github external
function createYargsConfigArguments() {
  const simpleArgv = yargsParser(process.argv.slice(2), {envPrefix: 'LHCI'});
  /** @type {[string, (path: string) => LHCI.YargsOptions]} */
  const configOption = ['config', loadAndParseRcFile];
  // If they're using the config option or opting out of auto-detection, use the config option.
  if (simpleArgv.config || hasOptedOutOfRcDetection()) return configOption;
  const rcFile = findRcFile();
  // If they don't currently have an rc file, use the config option for awareness.
  if (!rcFile) return configOption;
  return [loadAndParseRcFile(rcFile)];
}
github GoogleChrome / lighthouse-ci / packages / server / src / ui / routes / build-view / audit-detail / table-details.jsx View on Github external
{sortedItems.map(({base, compare, diffs}) =&gt; {
            const definedItem = compare || base;
            // This should never be true, but make tsc happy
            if (!definedItem) return null;

            const key = `${base &amp;&amp; base.index}-${compare &amp;&amp; compare.index}`;
            const state = getRowLabelForIndex(
              allDiffs,
              compare &amp;&amp; compare.index,
              base &amp;&amp; base.index
            );

            return (
              
                {headings.map((heading, j) =&gt; {
                  const itemType = heading.valueType || heading.itemType || 'unknown';
                  const diff = diffs.find(
                    /** @return {diff is LHCI.NumericItemAuditDiff} */
                    diff =&gt; diff.type === 'itemDelta' &amp;&amp; diff.itemKey === heading.key
                  );

                  return (
github GoogleChrome / lighthouse-ci / scripts / ci-dogfood-get-urls.js View on Github external
async function main() {
  const rootURL = process.env.LHCI_ROOT_URL;
  if (!rootURL) throw new Error(`Missing LHCI_ROOT_URL environment variable`);

  const client = new ApiClient({rootURL});

  const projects = await client.getProjects();
  const project = projects.find(project => project.name.includes('Viewer')) || projects[0];
  const builds = await client.getBuilds(project.id);
  const build = builds.find(build => build.branch.includes('test_1')) || builds[0];

  process.stdout.write(
    [
      new URL(`/app`, rootURL),
      new URL(`/app/projects/${project.slug}`, rootURL),
      new URL(`/app/projects/${project.slug}/compare/${_.shortId(build.id)}`, rootURL),
    ].join('\n')
  );

  process.exit(0);
}
github GoogleChrome / lighthouse-ci / scripts / ci-dogfood-get-urls.js View on Github external
async function main() {
  const rootURL = process.env.LHCI_ROOT_URL;
  if (!rootURL) throw new Error(`Missing LHCI_ROOT_URL environment variable`);

  const client = new ApiClient({rootURL});

  const projects = await client.getProjects();
  const project = projects.find(project => project.name.includes('Viewer')) || projects[0];
  const builds = await client.getBuilds(project.id);
  const build = builds.find(build => build.branch.includes('test_1')) || builds[0];

  process.stdout.write(
    [
      new URL(`/app`, rootURL),
      new URL(`/app/projects/${project.slug}`, rootURL),
      new URL(`/app/projects/${project.slug}/compare/${_.shortId(build.id)}`, rootURL),
    ].join('\n')
  );

  process.exit(0);
}