-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vitest): allow overiding package installer with public API (#4936)
- Loading branch information
1 parent
463bee3
commit c2cceeb
Showing
14 changed files
with
96 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import url from 'node:url' | ||
import { createRequire } from 'node:module' | ||
import c from 'picocolors' | ||
import { isPackageExists } from 'local-pkg' | ||
import { EXIT_CODE_RESTART } from '../constants' | ||
import { isCI } from '../utils/env' | ||
|
||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) | ||
|
||
export class VitestPackageInstaller { | ||
async ensureInstalled(dependency: string, root: string) { | ||
if (process.env.VITEST_SKIP_INSTALL_CHECKS) | ||
return true | ||
|
||
if (process.versions.pnp) { | ||
const targetRequire = createRequire(__dirname) | ||
try { | ||
targetRequire.resolve(dependency, { paths: [root, __dirname] }) | ||
return true | ||
} | ||
catch (error) { | ||
} | ||
} | ||
|
||
if (isPackageExists(dependency, { paths: [root, __dirname] })) | ||
return true | ||
|
||
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 | ||
|
||
const prompts = await import('prompts') | ||
const { install } = await prompts.prompt({ | ||
type: 'confirm', | ||
name: 'install', | ||
message: c.reset(`Do you want to install ${c.green(dependency)}?`), | ||
}) | ||
|
||
if (install) { | ||
await (await import('@antfu/install-pkg')).installPackage(dependency, { dev: true }) | ||
// TODO: somehow it fails to load the package after installation, remove this when it's fixed | ||
process.stderr.write(c.yellow(`\nPackage ${dependency} installed, re-run the command to start.\n`)) | ||
process.exit(EXIT_CODE_RESTART) | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters