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

A Node API to catch deps installation prompts and handle it on the user's own #4864

Closed
4 tasks done
Jinjiang opened this issue Jan 4, 2024 · 4 comments · Fixed by #4936
Closed
4 tasks done

A Node API to catch deps installation prompts and handle it on the user's own #4864

Jinjiang opened this issue Jan 4, 2024 · 4 comments · Fixed by #4936
Labels
enhancement New feature or request

Comments

@Jinjiang
Copy link

Jinjiang commented Jan 4, 2024

Clear and concise description of the problem

This issue is from discussion: #4772 (comment)

The background is we are integrating Vitest in Node APIs into Bit workflow. However, Bit CLI doesn't use npm/yarn/pnpm to install dependencies directly. Instead we have bit install.

In some cases, Vitest will prompt in terminal asking for installing dependencies like vitest ui, vitest coverage, jsdom etc. Then the logic would be out of control from Bit CLI.

Suggested solution

It would be great if we can catch them by a Node.js API and hand it over to bit install.

The new API could be an API, an option, or any kinds of event handlers which can be fired when any dependency is missing and let user decides whether to install it by Vitest or leave the job to user.

Pseudo code:

startVitest(..., async function (dep, installFn) {
  if (needInstallByBit(dep)) {
    // bit install the dep
  } else {
    await installFn(dep)
  }
}

Alternative

Just provide an option to fail the tests when any external deps not found.

Additional context

No response

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jan 4, 2024

I think this auto-install feature is provided from https://github.com/antfu/install-pkg, so it might be better to ask for bit cli support there?

await (await import('@antfu/install-pkg')).installPackage(dependency, { dev: true })

But, It might be still nice for Vitest to provide a way to opt-out of this auto-install via environment variable or something since currently this can be skipped only on CI or non-TTY stdout.

const promptInstall = !isCI && process.stdout.isTTY
process.stderr.write(c.red(`${c.inverse(c.red(' MISSING DEPENDENCY '))} Cannot find dependency '${dependency}'\n\n`))
if (!promptInstall)
return false

@sheremet-va sheremet-va added the enhancement New feature or request label Jan 9, 2024
@sheremet-va
Copy link
Member

It would be nice to provide both options - disabling prompt and providing a way to install a package via a custom method so users of Bit also had a nice experience of auto installation.

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jan 11, 2024

Maybe it's too dirty or it doesn't even work, but it just came to my mind.

Would it be a viable "API" to control auto installation by something like process.env.VITEST_INSTALL_PKG variable in a following way?

// somewhere in the current `ensurePackageInstalled` (packages/vitest/src/node/pkg.ts)
// ...

if (process.env.VITEST_INSTALL_PKG === "false") {
  return false;
}

if (process.env.VITEST_INSTALL_PKG) {
  // VITEST_INSTALL_PKG should point to import-able js module with { default: () => Promise<void> }
  // (probably it needs to be a full path since certain node_modules structure might not be visible from vitest package?)
  const customInstallPkg = await import(process.env.VITEST_INSTALL_PKG);
  await customInstallPkg.default(dependency)
  return true;
}

// otherwise fallback to current `import("@antfu/install-pkg")` 
// ...

@sheremet-va
Copy link
Member

sheremet-va commented Jan 11, 2024

Would it be a viable "API" to control auto installation by something like process.env.VITEST_INSTALL_PKG variable in a following way?

I would prefer updating startVitest

@github-actions github-actions bot locked and limited conversation to collaborators Jan 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants