The Wayback Machine - https://web.archive.org/web/20201204194556/https://github.com/tunnckoCore/opensource/issues/77
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: add `tryExtensions` and `async tryCatch` #77

Open
tunnckoCore opened this issue Nov 1, 2019 · 0 comments
Open

utils: add `tryExtensions` and `async tryCatch` #77

tunnckoCore opened this issue Nov 1, 2019 · 0 comments

Comments

@tunnckoCore
Copy link
Owner

@tunnckoCore tunnckoCore commented Nov 1, 2019

Repeated in couple of Jest Runners.

the tryExtensions

function tryExtensions(filepath, config) {
  const { extensions } = getWorkspacesAndExtensions(config.cwd);
  const hasExtension = path.extname(filepath).length > 0;

  if (hasExtension) {
    return filepath;
  }

  const extension = extensions.find((ext) => fs.existsSync(filepath + ext));
  if (!extension) {
    throw new Error(`Cannot find input file: ${filepath}`);
  }

  return filepath + extension;
}

the tryCatch (notice that it doesn't have hasError now) and better signature.

function isObject(val) {
  return val && typeof val === 'object' && !Array.isArray(val);
}
async function tryCatch(fn, onError) {
  try {
    return await fn();
  } catch (err) {
    if (typeof onError === 'function') {
      return {
        error: onError(err),
      };
    }

    if (isObject(onError)) {
      return {
        error: fail({
          start: onError.start,
          end: new Date(),
          test: {
            path: onError.testPath,
            title: 'Rollup',
            errorMessage: `jest-runner-rollup: ${err.stack || err.message}`,
          },
        }),
      };
    }

    const now = new Date();
    return {
      error: fail({
        start: now,
        end: now,
        test: {
          path: '___rollup-runner-failing.js',
          title: 'Rollup',
          errorMessage: `jest-runner-rollup: ${err.stack || err.message}`,
        },
      }),
    };
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.