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

View file

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