Skip to content

Commit

Permalink
feat: add built-in jsr upgrade provider (rework) (#692)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Fischer <[email protected]>
  • Loading branch information
JOTSR and c4spar authored May 13, 2024
1 parent 2bc19f7 commit b976d26
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions command/upgrade/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
type GithubProviderOptions,
type GithubVersions,
} from "./provider/github.ts";
export { JsrProvider, type JsrProviderOptions } from "./provider/jsr.ts";
export {
NestLandProvider,
type NestLandProviderOptions,
Expand Down
9 changes: 6 additions & 3 deletions command/upgrade/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ export abstract class Provider {
}

async upgrade(
{ name, from, to, importMap, main = `${name}.ts`, args = [] }:
UpgradeOptions,
{ name, from, to, importMap, main, args = [] }: UpgradeOptions,
): Promise<void> {
if (to === "latest") {
const { latest } = await this.getVersions(name);
to = latest;
}
const registry: string = new URL(main, this.getRegistryUrl(name, to)).href;

const registryUrl = this.getRegistryUrl(name, to);
const registry: string = registryUrl.startsWith("jsr:")
? registryUrl
: new URL(main || `${name}.ts`, registryUrl).href;

const cmdArgs = ["install"];

Expand Down
72 changes: 72 additions & 0 deletions command/upgrade/provider/jsr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Provider, Versions } from "../provider.ts";

type Semver =
| `${number}.${number}.${number}`
| `${number}.${number}.${number}-${string}`;
type Package = `@${string}/${string}`;

type JsrApiPackageMetadata = {
scope: string;
name: string;
latest: Semver;
versions: {
[version: Semver]: { yanked?: boolean };
};
};

export type JsrProviderOptions = {
package: Package;
} | {
scope: string;
name?: string;
};

export class JsrProvider extends Provider {
name = "jsr";
private readonly repositoryUrl = "https://jsr.io/";
private readonly packageName?: string;
private readonly packageScope: string;

constructor(options: JsrProviderOptions) {
super();
this.packageScope = "package" in options
? options.package.split("/")[0].slice(1)
: options.scope;
this.packageName = "package" in options
? options.package.split("/")[1]
: options.name;
}

async getVersions(
name: string,
): Promise<Versions> {
const response = await fetch(
`${this.repositoryUrl}/@${this.packageScope}/${
this.packageName ?? name
}/meta.json`,
);
if (!response.ok) {
throw new Error(
"couldn't fetch the latest version - try again after sometime",
);
}

const { latest, versions } = await response.json() as JsrApiPackageMetadata;

return {
latest,
versions: Object.keys(versions),
};
}

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

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

0 comments on commit b976d26

Please sign in to comment.