0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-03 22:29:08 -05:00

fix getPackage for db package

This commit is contained in:
Fred K. Schott 2024-01-23 21:16:33 -08:00 committed by Nate Moore
parent 2a105adf52
commit 7a5876034e
2 changed files with 18 additions and 10 deletions

View file

@ -6,6 +6,9 @@ import prompts from 'prompts';
import resolvePackage from 'resolve'; import resolvePackage from 'resolve';
import whichPm from 'which-pm'; import whichPm from 'which-pm';
import { type Logger } from '../core/logger/core.js'; import { type Logger } from '../core/logger/core.js';
import { createRequire } from 'node:module';
import { sep } from 'node:path';
const require = createRequire(import.meta.url);
type GetPackageOptions = { type GetPackageOptions = {
skipAsk?: boolean; skipAsk?: boolean;
@ -18,13 +21,18 @@ export async function getPackage<T>(
options: GetPackageOptions, options: GetPackageOptions,
otherDeps: string[] = [] otherDeps: string[] = []
): Promise<T | undefined> { ): Promise<T | undefined> {
let packageImport;
try { try {
// TODO: uncomment this when `@astrojs/db` works // Custom resolution logic for @astrojs/db. Since it lives in our monorepo,
// await tryResolve(packageName, options.cwd ?? process.cwd()); // the generic tryResolve() method doesn't work.
if (packageName === '@astrojs/db') {
// The `require.resolve` is required as to avoid Node caching the failed `import` const packageJsonLoc = require.resolve(packageName + '/package.json', {paths: [options.cwd ?? process.cwd()]});
packageImport = await import(packageName); const packageLoc = packageJsonLoc.replace(`package.json`, 'dist/index.js');
const packageImport = await import(packageLoc);
return packageImport as T;
}
await tryResolve(packageName, options.cwd ?? process.cwd());
const packageImport = await import(packageName);
return packageImport as T;
} catch (e) { } catch (e) {
logger.info( logger.info(
null, null,
@ -33,13 +41,12 @@ export async function getPackage<T>(
const result = await installPackage([packageName, ...otherDeps], options, logger); const result = await installPackage([packageName, ...otherDeps], options, logger);
if (result) { if (result) {
packageImport = await import(packageName); const packageImport = await import(packageName);
return packageImport;
} else { } else {
return undefined; return undefined;
} }
} }
return packageImport as T;
} }
function tryResolve(packageName: string, cwd: string) { function tryResolve(packageName: string, cwd: string) {

View file

@ -20,7 +20,8 @@
"./internal-drizzle": { "./internal-drizzle": {
"types": "./dist/internal-drizzle.d.ts", "types": "./dist/internal-drizzle.d.ts",
"import": "./dist/internal-drizzle.js" "import": "./dist/internal-drizzle.js"
} },
"./package.json": "./package.json"
}, },
"typesVersions": { "typesVersions": {
"*": { "*": {