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

fix: prevent urls from being cut short in http provider #390

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-papayas-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"jsrepo": patch
---

Ensure parsed urls from http provider end with a trailing slash.
5 changes: 5 additions & 0 deletions .changeset/lazy-coins-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"jsrepo": patch
---

Catch `JSON.parse` errors when fetching the manifest to provide a more clear error to the user.
16 changes: 2 additions & 14 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@
"bugs": {
"url": "https://github.com/ieedan/jsrepo/issues"
},
"keywords": [
"repo",
"cli",
"svelte",
"vue",
"typescript",
"javascript",
"shadcn",
"registry"
],
"keywords": ["repo", "cli", "svelte", "vue", "typescript", "javascript", "shadcn", "registry"],
"type": "module",
"exports": {
".": {
Expand All @@ -34,10 +25,7 @@
},
"bin": "./dist/index.js",
"main": "./dist/index.js",
"files": [
"./schemas/**/*",
"dist/**/*"
],
"files": ["./schemas/**/*", "dist/**/*"],
"scripts": {
"start": "tsup --silent && node ./dist/index.js",
"build": "tsup",
Expand Down
11 changes: 5 additions & 6 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { loadFormatterConfig } from '../utils/format';
import { json } from '../utils/language-support';
import * as persisted from '../utils/persisted';
import { type Task, intro, nextSteps, runTasks } from '../utils/prompts';
import { http, providers } from '../utils/registry-providers';
import * as registry from '../utils/registry-providers/internal';

const schema = v.object({
Expand Down Expand Up @@ -199,8 +198,8 @@ const _initProject = async (registries: string[], options: Options) => {
validate: (val) => {
if (val.trim().length === 0) return 'Please provide a value';

if (!providers.find((provider) => provider.matches(val))) {
return `Invalid provider! Valid providers (${providers.map((provider) => provider.name).join(', ')})`;
if (!registry.selectProvider(val)) {
return `Invalid provider! Valid providers (${registry.providers.map((provider) => provider.name).join(', ')})`;
}
},
});
Expand Down Expand Up @@ -290,12 +289,12 @@ const promptForProviderConfig = async (repo: string, paths: Paths): Promise<Path

const storage = persisted.get();

const provider = providers.find((p) => p.matches(repo));
const provider = registry.selectProvider(repo);

if (!provider) {
program.error(
color.red(
`Invalid provider! Valid providers (${providers.map((provider) => provider.name).join(', ')})`
`Invalid provider! Valid providers (${registry.providers.map((provider) => provider.name).join(', ')})`
)
);
}
Expand All @@ -305,7 +304,7 @@ const promptForProviderConfig = async (repo: string, paths: Paths): Promise<Path
const token = storage.get(tokenKey);

// don't ask if the provider is a custom domain
if (!token && provider.name !== http.name) {
if (!token && provider.name !== registry.http.name) {
const result = await confirm({
message: 'Would you like to add an auth token?',
initialValue: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/registry-providers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const parseUrl = (
}

return {
url: u.join(parsedUrl.origin, ...segments),
url: u.addTrailingSlash(u.join(parsedUrl.origin, ...segments)),
specifier,
};
};
10 changes: 9 additions & 1 deletion packages/cli/src/utils/registry-providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ export const fetchManifest = async (

if (manifest.isErr()) return Err(manifest.unwrapErr());

const categories = v.safeParse(v.array(categorySchema), JSON.parse(manifest.unwrap()));
let json: string;

try {
json = JSON.parse(manifest.unwrap());
} catch (err) {
return Err(`Error parsing categories: ${err}`);
}

const categories = v.safeParse(v.array(categorySchema), json);

if (!categories.success) {
return Err(`Error parsing categories: ${categories.issues}`);
Expand Down
44 changes: 34 additions & 10 deletions packages/cli/tests/providers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert, describe, expect, it } from 'vitest';
import { MANIFEST_FILE } from '../src/constants';
import * as registry from '../src/utils/registry-providers/internal';
import type { ParseOptions, ParseResult } from '../src/utils/registry-providers/types';

Expand All @@ -8,7 +9,7 @@ type ParseTestCase = {
expected: ParseResult;
};

type BaseUrlTestCase = {
type StringTestCase = {
url: string;
expected: string;
};
Expand Down Expand Up @@ -72,7 +73,7 @@ describe('github', () => {
});

it('correctly parses base url', () => {
const cases: BaseUrlTestCase[] = [
const cases: StringTestCase[] = [
{
url: 'github/ieedan/std',
expected: 'https://github.com/ieedan/std',
Expand Down Expand Up @@ -372,7 +373,7 @@ describe('gitlab', () => {
});

it('correctly parses base url', () => {
const cases: BaseUrlTestCase[] = [
const cases: StringTestCase[] = [
{
url: 'gitlab/ieedan/std',
expected: 'https://gitlab.com/ieedan/std',
Expand Down Expand Up @@ -648,7 +649,7 @@ describe('bitbucket', () => {
});

it('correctly parses base url', () => {
const cases: BaseUrlTestCase[] = [
const cases: StringTestCase[] = [
{
url: 'bitbucket/ieedan/std',
expected: 'https://bitbucket.org/ieedan/std',
Expand Down Expand Up @@ -916,7 +917,7 @@ describe('azure', () => {
});

it('correctly parses base url', () => {
const cases: BaseUrlTestCase[] = [
const cases: StringTestCase[] = [
{
url: 'azure/ieedan/std/std',
expected: 'https://dev.azure.com/ieedan/_git/std',
Expand Down Expand Up @@ -1144,31 +1145,31 @@ describe('http', () => {
url: 'https://example.com/',
opts: { fullyQualified: false },
expected: {
url: 'https://example.com',
url: 'https://example.com/',
specifier: undefined,
},
},
{
url: 'https://example.com/new-york',
opts: { fullyQualified: false },
expected: {
url: 'https://example.com/new-york',
url: 'https://example.com/new-york/',
specifier: undefined,
},
},
{
url: 'https://example.com/utils/math',
opts: { fullyQualified: true },
expected: {
url: 'https://example.com',
url: 'https://example.com/',
specifier: 'utils/math',
},
},
{
url: 'https://example.com/new-york/utils/math',
opts: { fullyQualified: true },
expected: {
url: 'https://example.com/new-york',
url: 'https://example.com/new-york/',
specifier: 'utils/math',
},
},
Expand All @@ -1179,8 +1180,31 @@ describe('http', () => {
}
});

it('correctly resolves url', async () => {
const cases: StringTestCase[] = [
{
url: 'https://example.com',
expected: 'https://example.com/jsrepo-manifest.json',
},
{
url: 'https://example.com/new-york',
expected: 'https://example.com/new-york/jsrepo-manifest.json',
},
];

for (const c of cases) {
const state = await registry.getProviderState(c.url);

assert(!state.isErr());

expect(
await state.unwrap().provider.resolveRaw(state.unwrap(), MANIFEST_FILE)
).toStrictEqual(new URL(c.expected));
}
});

it('correctly parses base url', () => {
const cases: BaseUrlTestCase[] = [
const cases: StringTestCase[] = [
{
url: 'https://example.com/',
expected: 'https://example.com',
Expand Down
Loading