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

feat(command): add support for npm package without scope to npm provider #748

Merged
merged 1 commit into from
Sep 28, 2024
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
35 changes: 22 additions & 13 deletions command/upgrade/provider/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type NpmProviderOptions =
& ({
package: string;
} | {
scope: string;
scope?: string;
name?: string;
});

Expand All @@ -14,23 +14,28 @@ export class NpmProvider extends Provider {
private readonly repositoryUrl = "https://npmjs.org/";
private readonly apiUrl = "https://registry.npmjs.org/";
private readonly packageName?: string;
private readonly packageScope: string;
private readonly packageScope?: string;

constructor({ main, logger, ...options }: NpmProviderOptions) {
super({ main, logger });
this.packageScope = "package" in options
? options.package.split("/")[0].slice(1)
: options.scope;
this.packageName = "package" in options
? options.package.split("/")[1]
: options.name;
if ("package" in options) {
if (options.package.startsWith("@")) {
this.packageScope = options.package.split("/")[0].slice(1);
this.packageName = options.package.split("/")[1];
} else {
this.packageName = options.package;
}
} else {
this.packageScope = options.scope;
this.packageName = options.name;
}
}

async getVersions(
name: string,
): Promise<Versions> {
const response = await fetch(
new URL(`@${this.packageScope}/${this.packageName ?? name}`, this.apiUrl),
new URL(this.#getPackageName(name), this.apiUrl),
);
if (!response.ok) {
throw new Error(
Expand All @@ -49,15 +54,19 @@ export class NpmProvider extends Provider {

getRepositoryUrl(name: string, version?: string): string {
return new URL(
`package/@${this.packageScope}/${this.packageName ?? name}${
version ? `/v/${version}` : ""
}`,
`package/${this.#getPackageName(name)}${version ? `/v/${version}` : ""}`,
this.repositoryUrl,
).href;
}

getRegistryUrl(name: string, version: string): string {
return `npm:@${this.packageScope}/${this.packageName ?? name}@${version}`;
return `npm:${this.#getPackageName(name)}@${version}`;
}

#getPackageName(name: string): string {
return `${this.packageScope ? `@${this.packageScope}/` : ""}${
this.packageName ?? name
}`;
}
}

Expand Down
28 changes: 28 additions & 0 deletions command/upgrade/provider/npm_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ test({
},
});

await ctx.step({
name: "should return registry url with package option",
fn() {
const provider = new NpmProvider({
package: "example",
});

assertEquals(
provider.getRegistryUrl("foo", "1.0.0"),
"npm:example@1.0.0",
);
},
});

await ctx.step({
name: "should return registry url with packageName option",
fn() {
const provider = new NpmProvider({
package: "example",
});

assertEquals(
provider.getRegistryUrl("foo", "1.0.0"),
"npm:example@1.0.0",
);
},
});

await ctx.step({
name: "should return repository url",
fn() {
Expand Down