Skip to content

Commit

Permalink
fix: parse third-party org names
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 22, 2022
1 parent c8d0fa4 commit 00f03b0
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/astro/src/core/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ async function fetchPackageJson(
name: string,
tag: string
): Promise<object | Error> {
const packageName = `${scope ? `@${scope}/` : ''}${name}`;
const packageName = `${scope ? `${scope}/` : ''}${name}`;
const res = await fetch(`https://registry.npmjs.org/${packageName}/${tag}`);
if (res.status === 404) {
return new Error();
Expand All @@ -637,13 +637,14 @@ export async function validateIntegrations(integrations: string[]): Promise<Inte
if (!parsed) {
throw new Error(`${bold(integration)} does not appear to be a valid package name!`);
}

let { scope, name, tag } = parsed;
let pkgJson = null;
let pkgType: 'first-party' | 'third-party' = 'first-party';
let pkgJson;
let pkgType: 'first-party' | 'third-party';

if (!scope) {
const firstPartyPkgCheck = await fetchPackageJson('astrojs', name, tag);
if (scope && scope !== '@astrojs') {
pkgType = 'third-party';
} else {
const firstPartyPkgCheck = await fetchPackageJson('@astrojs', name, tag);
if (firstPartyPkgCheck instanceof Error) {
spinner.warn(
yellow(`${bold(integration)} is not an official Astro package. Use at your own risk!`)
Expand All @@ -664,6 +665,7 @@ export async function validateIntegrations(integrations: string[]): Promise<Inte
spinner.start('Resolving with third party packages...');
pkgType = 'third-party';
} else {
pkgType = 'first-party';
pkgJson = firstPartyPkgCheck as any;
}
}
Expand All @@ -676,8 +678,8 @@ export async function validateIntegrations(integrations: string[]): Promise<Inte
}
}

const resolvedScope = pkgType === 'first-party' ? 'astrojs' : scope;
const packageName = `${resolvedScope ? `@${resolvedScope}/` : ''}${name}`;
const resolvedScope = pkgType === 'first-party' ? '@astrojs' : scope;
const packageName = `${resolvedScope ? `${resolvedScope}/` : ''}${name}`;

let dependencies: IntegrationInfo['dependencies'] = [
[pkgJson['name'], `^${pkgJson['version']}`],
Expand Down

0 comments on commit 00f03b0

Please sign in to comment.