-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): add npm upgrade provider (#701)
- Loading branch information
Showing
3 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; |