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
Labels
good first issue Good for newcomers and first-timer contributors Pkg: @tunnckocore/utils Priority: Medium This issue may be useful, and needs some attention. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: In Progress This issue is being worked on, and has someone assigned. Status: Proposal Type: Enhancement Most issues will probably be for additions or changes. Expected that this will result in a PR.

Comments

@tunnckoCore
Copy link
Owner

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}`,
        },
      }),
    };
  }
}
@tunnckoCore tunnckoCore added good first issue Good for newcomers and first-timer contributors Priority: Medium This issue may be useful, and needs some attention. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: In Progress This issue is being worked on, and has someone assigned. Status: Proposal Type: Enhancement Most issues will probably be for additions or changes. Expected that this will result in a PR. Pkg: @tunnckocore/utils labels Nov 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers and first-timer contributors Pkg: @tunnckocore/utils Priority: Medium This issue may be useful, and needs some attention. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: In Progress This issue is being worked on, and has someone assigned. Status: Proposal Type: Enhancement Most issues will probably be for additions or changes. Expected that this will result in a PR.
Projects
None yet
Development

No branches or pull requests

1 participant