Skip to content

Commit

Permalink
feat(command): add support for npm package without scope to npm provi…
Browse files Browse the repository at this point in the history
…der (#748)
  • Loading branch information
c4spar authored Sep 28, 2024
1 parent 2d8aa19 commit f882fa0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
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

0 comments on commit f882fa0

Please sign in to comment.