Skip to content

Commit

Permalink
feat(command): add npm upgrade provider (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar authored May 15, 2024
1 parent 6d08b9a commit 45dbe30
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
3 changes: 2 additions & 1 deletion command/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"./upgrade/provider/deno-land": "./upgrade/provider/deno_land.ts",
"./upgrade/provider/github": "./upgrade/provider/github.ts",
"./upgrade/provider/jsr": "./upgrade/provider/jsr.ts",
"./upgrade/provider/nest-land": "./upgrade/provider/nest_land.ts"
"./upgrade/provider/nest-land": "./upgrade/provider/nest_land.ts",
"./upgrade/provider/npm": "./upgrade/provider/npm.ts"
},
"lock": false
}
7 changes: 4 additions & 3 deletions command/upgrade/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ export abstract class Provider {
}

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

const cmdArgs = ["install"];

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

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

export class NpmProvider extends Provider {
name = "npm";
private readonly repositoryUrl = "https://registry.npmjs.org/";
private readonly packageName?: string;
private readonly packageScope: string;

constructor(options: NpmProviderOptions) {
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}`,
);
if (!response.ok) {
throw new Error(
"couldn't fetch the latest version - try again after sometime",
);
}

const { "dist-tags": { latest }, versions } = await response
.json() as NpmApiPackageMetadata;

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

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

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

type NpmApiPackageMetadata = {
"dist-tags": {
latest: string;
};
versions: {
[version: string]: unknown;
};
};

0 comments on commit 45dbe30

Please sign in to comment.