Skip to content

Commit

Permalink
support jsdelivr
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jul 17, 2024
1 parent fd97ba3 commit c60cb39
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/) and this
project adheres to [Semantic Versioning](https://semver.org/).

## [0.2.5] - Unreleased
### Added
- Support for themes from jsDelivr.

## [0.2.4] - 2024-06-14
### Fixed
- Upgrade Lume in an external import map file.
Expand Down Expand Up @@ -97,6 +101,7 @@ First version
[#1]: https://github.com/lumeland/init/issues/1
[#3]: https://github.com/lumeland/init/issues/3

[0.2.5]: https://github.com/lumeland/init/compare/v0.2.4...HEAD
[0.2.4]: https://github.com/lumeland/init/compare/v0.2.3...v0.2.4
[0.2.3]: https://github.com/lumeland/init/compare/v0.2.2...v0.2.3
[0.2.2]: https://github.com/lumeland/init/compare/v0.2.1...v0.2.2
Expand Down
33 changes: 27 additions & 6 deletions steps/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ export async function getLatestVersion(
return versions.latest;
}

/** Return the latest stable version from the deno.land/x repository */
export async function getLatestJsDelivrVersion(
name: string,
): Promise<string> {
const response = await fetch(
`https://data.jsdelivr.com/v1/package/gh/${name.toLowerCase()}`,
);
const versions = await response.json();
return versions.versions[0];
}

export async function getLatestGitHubTag(name: string): Promise<string> {
const response = await fetch(
`https://api.github.com/repos/${name}/tags`,
Expand All @@ -26,16 +37,26 @@ export async function getLatestGitHubCommit(name: string): Promise<string> {
}

export async function resolveOrigin(url: string): Promise<string> {
const results = url.match(/^https:\/\/deno.land\/x\/([^\/]+)$/);
const denoland = url.match(/^https:\/\/deno.land\/x\/([^\/]+)$/);

if (denoland) {
const [, name] = denoland;
const version = await getLatestVersion(name);

if (!results) {
throw new Error(`Invalid URL: ${url}`);
return `https://deno.land/x/${name}@${version}`;
}

const [, name] = results;
const version = await getLatestVersion(name);
const jsdelivr = url.match(
/^https:\/\/cdn\.jsdelivr\.net\/gh\/([^\/]+\/[^\/]+)$/,
);
if (jsdelivr) {
const [, name] = jsdelivr;
const version = await getLatestJsDelivrVersion(name);

return `https://cdn.jsdelivr.net/gh/${name}@${version}`;
}

return `https://deno.land/x/${name}@${version}`;
throw new Error(`Could not resolve origin for ${url}`);
}

export async function loadFile(url: string): Promise<Uint8Array> {
Expand Down

0 comments on commit c60cb39

Please sign in to comment.