import { performance } from 'perf_hooks';

import Logger from './Logger';
import constructReport from './constructReport';
import loadCSSFile from './loadCSSFile';

export default async function remoteRunner(
  { apiKey, apiSecret, endpoint, targets, plugins, stylesheets },
  { generateStaticPackage, isAsync },
) {
  const logger = new Logger();
  try {
    logger.info('Generating static package...');
    const staticPackage = await generateStaticPackage();
    const targetNames = Object.keys(targets);
    const tl = targetNames.length;
    const cssBlocks = await Promise.all(stylesheets.map(loadCSSFile));
    plugins.forEach(({ css }) => cssBlocks.push(css || ''));
    logger.info(`Generating screenshots in ${tl} target${tl > 1 ? 's' : ''}...`);
    const outerStartTime = performance.now();
    const results = await Promise.all(
      targetNames.map(async (name) => {
        const startTime = performance.now();
        const result = await targets[name].execute({
          targetName: name,
          asyncResults: isAsync,
          staticPackage,
          apiKey,
          apiSecret,
          endpoint,
          globalCSS: cssBlocks.join('').replace(/\n/g, ''),
        });
        logger.start(`  - ${name}`, { startTime });
        logger.success();
        return { name, result };
      }),
    );
    logger.start(undefined, { startTime: outerStartTime });
    logger.success();
    if (isAsync) {
      return results;
    }
    return constructReport(results);
  } catch (e) {
    logger.fail();
    throw e;
  }
}

import { performance } from 'perf_hooks';

import Logger from './Logger';
import constructReport from './constructReport';
import loadCSSFile from './loadCSSFile';

export default async function remoteRunner(
  { apiKey, apiSecret, endpoint, targets, plugins, stylesheets },
  { generateStaticPackage },
  { isAsync },
) {
  const logger = new Logger();
  try {
    logger.info('Generating static package...');
    const staticPackage = await generateStaticPackage();
    const targetNames = Object.keys(targets);
    const tl = targetNames.length;
    const cssBlocks = await Promise.all(stylesheets.map(loadCSSFile));
    plugins.forEach(({ css }) => cssBlocks.push(css || ''));
    logger.info(`Generating screenshots in ${tl} target${tl > 1 ? 's' : ''}...`);
    const outerStartTime = performance.now();
    const results = await Promise.all(
      targetNames.map(async (name) => {
        const startTime = performance.now();
        const result = await targets[name].execute({
          targetName: name,
          asyncResults: isAsync,
          staticPackage,
          apiKey,
          apiSecret,
          endpoint,
          globalCSS: cssBlocks.join('').replace(/\n/g, ''),
        });
        logger.start(`  - ${name}`, { startTime });
        logger.success();
        return { name, result };
      }),
    );
    logger.start(undefined, { startTime: outerStartTime });
    logger.success();
    if (isAsync) {
      return results;
    }
    return constructReport(results);
  } catch (e) {
    logger.fail();
    throw e;
  }
}

Fix using HAPPO_IS_ASYNC with Storybook plugin
Fix using HAPPO_IS_ASYNC with Storybook plugin

We weren't passing the right things to the remoteRunner, causing async
report rendering to fail with a cryptic "is not iterable (cannot read
property Symbol(Symbol.iterator))" error.
