Skip to content

utils: add tryExtensions and async tryCatch #77

Open
@tunnckoCore

Description

@tunnckoCore
Owner

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}`,
        },
      }),
    };
  }
}

Activity

added
good first issueGood for newcomers and first-timer contributors
Priority: MediumThis issue may be useful, and needs some attention.
Status: AcceptedIt's clear what the subject of the issue is about, and what the resolution should be.
Status: In ProgressThis issue is being worked on, and has someone assigned.
Type: EnhancementMost issues will probably be for additions or changes. Expected that this will result in a PR.
on Nov 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pkg: @tunnckocore/utilsPriority: MediumThis issue may be useful, and needs some attention.Status: AcceptedIt's clear what the subject of the issue is about, and what the resolution should be.Status: In ProgressThis issue is being worked on, and has someone assigned.Status: ProposalType: EnhancementMost issues will probably be for additions or changes. Expected that this will result in a PR.good first issueGood for newcomers and first-timer contributors

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @tunnckoCore

        Issue actions

          utils: add `tryExtensions` and `async tryCatch` · Issue #77 · tunnckoCore/opensource